From 38d304429159c704ee039daf83e6b8ab58f28cd7 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 26 Sep 2023 15:13:04 +0200 Subject: [PATCH] cs --- src/Utils/ArrayHash.php | 2 +- src/Utils/Arrays.php | 10 +++++----- src/Utils/FileSystem.php | 2 +- src/Utils/Html.php | 2 +- src/Utils/ObjectHelpers.php | 12 ++++++++++-- src/Utils/Type.php | 8 ++++---- src/Utils/Validators.php | 4 ++-- tests/Utils/ArrayHash.phpt | 2 +- tests/Utils/Arrays.flatten().phpt | 2 +- tests/Utils/Arrays.normalize.phpt | 2 +- tests/Utils/Callback.check.phpt | 6 +++--- tests/Utils/FileSystem.copy.phpt | 6 +++--- tests/Utils/FileSystem.rename.phpt | 4 ++-- tests/Utils/Finder.basic.phpt | 2 +- tests/Utils/Strings.compare().phpt | 2 +- tests/Utils/Strings.normalize().phpt | 2 +- tests/Utils/Strings.webalize().phpt | 11 ++++++++--- 17 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/Utils/ArrayHash.php b/src/Utils/ArrayHash.php index 3ef9f1f4f..349ffe0ef 100644 --- a/src/Utils/ArrayHash.php +++ b/src/Utils/ArrayHash.php @@ -29,7 +29,7 @@ public static function from(array $array, bool $recursive = true): static $obj = new static; foreach ($array as $key => $value) { $obj->$key = $recursive && is_array($value) - ? static::from($value, true) + ? static::from($value) : $value; } diff --git a/src/Utils/Arrays.php b/src/Utils/Arrays.php index 7204e3bdc..6938dce95 100644 --- a/src/Utils/Arrays.php +++ b/src/Utils/Arrays.php @@ -98,7 +98,7 @@ public static function mergeTree(array $array1, array $array2): array */ public static function getKeyOffset(array $array, string|int $key): ?int { - return Helpers::falseToNull(array_search(self::toKey($key), array_keys($array), true)); + return Helpers::falseToNull(array_search(self::toKey($key), array_keys($array), strict: true)); } @@ -151,9 +151,9 @@ public static function last(array $array): mixed public static function insertBefore(array &$array, string|int|null $key, array $inserted): void { $offset = $key === null ? 0 : (int) self::getKeyOffset($array, $key); - $array = array_slice($array, 0, $offset, true) + $array = array_slice($array, 0, $offset, preserve_keys: true) + $inserted - + array_slice($array, $offset, count($array), true); + + array_slice($array, $offset, count($array), preserve_keys: true); } @@ -167,9 +167,9 @@ public static function insertAfter(array &$array, string|int|null $key, array $i $offset = count($array) - 1; } - $array = array_slice($array, 0, $offset + 1, true) + $array = array_slice($array, 0, $offset + 1, preserve_keys: true) + $inserted - + array_slice($array, $offset + 1, count($array), true); + + array_slice($array, $offset + 1, count($array), preserve_keys: true); } diff --git a/src/Utils/FileSystem.php b/src/Utils/FileSystem.php index edc58c3ed..ab9a7e878 100644 --- a/src/Utils/FileSystem.php +++ b/src/Utils/FileSystem.php @@ -23,7 +23,7 @@ final class FileSystem */ public static function createDir(string $dir, int $mode = 0777): void { - if (!is_dir($dir) && !@mkdir($dir, $mode, true) && !is_dir($dir)) { // @ - dir may already exist + if (!is_dir($dir) && !@mkdir($dir, $mode, recursive: true) && !is_dir($dir)) { // @ - dir may already exist throw new Nette\IOException(sprintf( "Unable to create directory '%s' with mode %s. %s", self::normalizePath($dir), diff --git a/src/Utils/Html.php b/src/Utils/Html.php index 7e401b8fa..fc0e3ef2a 100644 --- a/src/Utils/Html.php +++ b/src/Utils/Html.php @@ -628,7 +628,7 @@ public function insert(?int $index, HtmlStringable|string $child, bool $replace */ final public function offsetSet($index, $child): void { - $this->insert($index, $child, true); + $this->insert($index, $child, replace: true); } diff --git a/src/Utils/ObjectHelpers.php b/src/Utils/ObjectHelpers.php index f4bd55f2b..6598c8485 100644 --- a/src/Utils/ObjectHelpers.php +++ b/src/Utils/ObjectHelpers.php @@ -62,7 +62,11 @@ public static function strictCall(string $class, string $method, array $addition ? ($trace[2]['class'] ?? null) : null; - if ($context && is_a($class, $context, true) && method_exists($context, $method)) { // called parent::$method() + if ( + $context + && is_a($class, $context, allow_string: true) + && method_exists($context, $method) + ) { // called parent::$method() $class = get_parent_class($context); } @@ -95,7 +99,11 @@ public static function strictStaticCall(string $class, string $method): void ? ($trace[2]['class'] ?? null) : null; - if ($context && is_a($class, $context, true) && method_exists($context, $method)) { // called parent::$method() + if ( + $context + && is_a($class, $context, allow_string: true) + && method_exists($context, $method) + ) { // called parent::$method() $class = get_parent_class($context); } diff --git a/src/Utils/Type.php b/src/Utils/Type.php index 0eaf90e48..7a1788126 100644 --- a/src/Utils/Type.php +++ b/src/Utils/Type.php @@ -35,7 +35,7 @@ public static function fromReflection( ? $reflection->getReturnType() ?? (PHP_VERSION_ID >= 80100 && $reflection instanceof \ReflectionMethod ? $reflection->getTentativeReturnType() : null) : $reflection->getType(); - return $type ? self::fromReflectionType($type, $reflection, true) : null; + return $type ? self::fromReflectionType($type, $reflection, asObject: true) : null; } @@ -49,7 +49,7 @@ private static function fromReflectionType(\ReflectionType $type, $of, bool $asO } elseif ($type instanceof \ReflectionUnionType || $type instanceof \ReflectionIntersectionType) { return new self( - array_map(fn($t) => self::fromReflectionType($t, $of, false), $type->getTypes()), + array_map(fn($t) => self::fromReflectionType($t, $of, asObject: false), $type->getTypes()), $type instanceof \ReflectionUnionType ? '|' : '&', ); @@ -109,7 +109,7 @@ public static function resolve( private function __construct(array $types, string $kind = '|') { - $o = array_search('null', $types, true); + $o = array_search('null', $types, strict: true); if ($o !== false) { // null as last array_splice($types, $o, 1); $types[] = 'null'; @@ -260,7 +260,7 @@ private function allows3(array $types, array $subtypes): bool $subtypes, fn($subtype) => Validators::isBuiltinType($type) ? strcasecmp($type, $subtype) === 0 - : is_a($subtype, $type, true) + : is_a($subtype, $type, allow_string: true) ) ); } diff --git a/src/Utils/Validators.php b/src/Utils/Validators.php index 6f86b515a..61ccf091a 100644 --- a/src/Utils/Validators.php +++ b/src/Utils/Validators.php @@ -102,7 +102,7 @@ public static function assert(mixed $value, string $expected, string $label = 'v $translate = ['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'NULL' => 'null']; $type = $translate[gettype($value)] ?? gettype($value); if (is_int($value) || is_float($value) || (is_string($value) && strlen($value) < 40)) { - $type .= ' ' . var_export($value, true); + $type .= ' ' . var_export($value, return: true); } elseif (is_object($value)) { $type .= ' ' . $value::class; } @@ -245,7 +245,7 @@ public static function isNumeric(mixed $value): bool */ public static function isCallable(mixed $value): bool { - return $value && is_callable($value, true); + return $value && is_callable($value, syntax_only: true); } diff --git a/tests/Utils/ArrayHash.phpt b/tests/Utils/ArrayHash.phpt index fa6e60c5e..9416a9896 100644 --- a/tests/Utils/ArrayHash.phpt +++ b/tests/Utils/ArrayHash.phpt @@ -87,7 +87,7 @@ test('', function () { 'children' => [ 'c' => 'John', ], - ], false); + ], recursive: false); Assert::type(Nette\Utils\ArrayHash::class, $list); Assert::type('array', $list['children']); }); diff --git a/tests/Utils/Arrays.flatten().phpt b/tests/Utils/Arrays.flatten().phpt index 25e982f7c..e0773b550 100644 --- a/tests/Utils/Arrays.flatten().phpt +++ b/tests/Utils/Arrays.flatten().phpt @@ -35,7 +35,7 @@ $res = Arrays::flatten([ ], 'y' => 'd', 'z' => 'e', -], true); +], preserveKeys: true); Assert::same([ 5 => 'a', diff --git a/tests/Utils/Arrays.normalize.phpt b/tests/Utils/Arrays.normalize.phpt index 44eab09f7..578a548f3 100644 --- a/tests/Utils/Arrays.normalize.phpt +++ b/tests/Utils/Arrays.normalize.phpt @@ -37,5 +37,5 @@ Assert::same( Arrays::normalize([ 1 => 'first', '' => 'second', - ], true), + ], filling: true), ); diff --git a/tests/Utils/Callback.check.phpt b/tests/Utils/Callback.check.phpt index 8205baa5c..aaf62851b 100644 --- a/tests/Utils/Callback.check.phpt +++ b/tests/Utils/Callback.check.phpt @@ -15,11 +15,11 @@ require __DIR__ . '/../bootstrap.php'; Assert::same('trim', Callback::check('trim')); -Assert::same('undefined', Callback::check('undefined', true)); +Assert::same('undefined', Callback::check('undefined', syntax: true)); Assert::exception( - fn() => Callback::check(123, true), + fn() => Callback::check(123, syntax: true), Nette\InvalidArgumentException::class, 'Given value is not a callable type.', ); @@ -40,7 +40,7 @@ Assert::exception( ); Assert::exception( - fn() => Callback::check(new stdClass, true), + fn() => Callback::check(new stdClass, syntax: true), Nette\InvalidArgumentException::class, 'Given value is not a callable type.', ); diff --git a/tests/Utils/FileSystem.copy.phpt b/tests/Utils/FileSystem.copy.phpt index f9f4a80d9..f627c9b09 100644 --- a/tests/Utils/FileSystem.copy.phpt +++ b/tests/Utils/FileSystem.copy.phpt @@ -52,14 +52,14 @@ test('copy', function () { FileSystem::write(getTempDir() . '/5/newfile', 'World'); Assert::exception( - fn() => FileSystem::copy(getTempDir() . '/5/newfile', getTempDir() . '/3/x/file', false), + fn() => FileSystem::copy(getTempDir() . '/5/newfile', getTempDir() . '/3/x/file', overwrite: false), Nette\InvalidStateException::class, "File or directory '%a%' already exists.", ); Assert::same('Hello', FileSystem::read(getTempDir() . '/3/x/file')); Assert::exception( - fn() => FileSystem::copy('remote://example.com', getTempDir() . '/3/x/file', false), + fn() => FileSystem::copy('remote://example.com', getTempDir() . '/3/x/file', overwrite: false), Nette\InvalidStateException::class, "File or directory '%a%' already exists.", ); @@ -69,7 +69,7 @@ test('copy', function () { Assert::same('World', FileSystem::read(getTempDir() . '/3/x/file')); Assert::exception( - fn() => FileSystem::copy(getTempDir() . '/5', getTempDir() . '/3', false), + fn() => FileSystem::copy(getTempDir() . '/5', getTempDir() . '/3', overwrite: false), Nette\InvalidStateException::class, "File or directory '%a%' already exists.", ); diff --git a/tests/Utils/FileSystem.rename.phpt b/tests/Utils/FileSystem.rename.phpt index c84546dd4..56f503b6d 100644 --- a/tests/Utils/FileSystem.rename.phpt +++ b/tests/Utils/FileSystem.rename.phpt @@ -22,7 +22,7 @@ test('rename file & dir', function () { test('overwrite file', function () { FileSystem::write(getTempDir() . '/8/newfile', 'World'); Assert::exception( - fn() => FileSystem::rename(getTempDir() . '/8/newfile', getTempDir() . '/9/x/file', false), + fn() => FileSystem::rename(getTempDir() . '/8/newfile', getTempDir() . '/9/x/file', overwrite: false), Nette\InvalidStateException::class, "File or directory '%a%' already exists.", ); @@ -35,7 +35,7 @@ test('overwrite file', function () { test('overwrite dir', function () { FileSystem::createDir(getTempDir() . '/10/'); Assert::exception( - fn() => FileSystem::rename(getTempDir() . '/10', getTempDir() . '/9', false), + fn() => FileSystem::rename(getTempDir() . '/10', getTempDir() . '/9', overwrite: false), Nette\InvalidStateException::class, "File or directory '%a%' already exists.", ); diff --git a/tests/Utils/Finder.basic.phpt b/tests/Utils/Finder.basic.phpt index 5a7483072..789472600 100644 --- a/tests/Utils/Finder.basic.phpt +++ b/tests/Utils/Finder.basic.phpt @@ -120,7 +120,7 @@ test('recursive file & directory search in child-first order', function () { Assert::same([ 'fixtures.finder/subdir/subdir2', 'fixtures.finder/subdir', - ], export($finder, false)); + ], export($finder, sort: false)); }); diff --git a/tests/Utils/Strings.compare().phpt b/tests/Utils/Strings.compare().phpt index 78dbf6f72..3aa78fb9a 100644 --- a/tests/Utils/Strings.compare().phpt +++ b/tests/Utils/Strings.compare().phpt @@ -26,7 +26,7 @@ Assert::true(Strings::compare('xy', 'yy', -1)); Assert::true(Strings::compare("I\u{F1}t\u{EB}rn\u{E2}ti\u{F4}n\u{E0}liz\u{E6}ti\u{F8}n", "I\u{D1}T\u{CB}RN\u{C2}TI\u{D4}N\u{C0}LIZ\u{C6}TI\u{D8}N")); // Iñtërnâtiônàlizætiøn Assert::true(Strings::compare("I\u{F1}t\u{EB}rn\u{E2}ti\u{F4}n\u{E0}liz\u{E6}ti\u{F8}n", "I\u{D1}T\u{CB}RN\u{C2}TI\u{D4}N\u{C0}LIZ\u{C6}TI\u{D8}N", 10)); -if (class_exists('Normalizer', false)) { +if (class_exists('Normalizer')) { Assert::true(Strings::compare("\xC3\x85", "A\xCC\x8A"), 'comparing NFC with NFD form'); Assert::true(Strings::compare("A\xCC\x8A", "\xC3\x85"), 'comparing NFD with NFC form'); } diff --git a/tests/Utils/Strings.normalize().phpt b/tests/Utils/Strings.normalize().phpt index bf6a72ec9..3142d2350 100644 --- a/tests/Utils/Strings.normalize().phpt +++ b/tests/Utils/Strings.normalize().phpt @@ -24,7 +24,7 @@ Assert::same('Hello World', Strings::normalize("Hello \u{80} World")); Assert::same('Hello World', Strings::normalize("Hello \u{9F} World")); Assert::same("Hello \u{A0} World", Strings::normalize("Hello \u{A0} World")); -if (class_exists('Normalizer', false)) { +if (class_exists('Normalizer')) { Assert::same("\xC3\x85", Strings::normalize("\xC3\x85")); // NFC -> NFC form Assert::same("\xC3\x85", Strings::normalize("A\xCC\x8A")); // NFD -> NFC form } diff --git a/tests/Utils/Strings.webalize().phpt b/tests/Utils/Strings.webalize().phpt index 995f44fc0..8d52c286b 100644 --- a/tests/Utils/Strings.webalize().phpt +++ b/tests/Utils/Strings.webalize().phpt @@ -9,12 +9,17 @@ declare(strict_types=1); use Nette\Utils\Strings; use Tester\Assert; - require __DIR__ . '/../bootstrap.php'; -Assert::same('zlutoucky-kun-oeooo', Strings::webalize("&\u{17D}LU\u{164}OU\u{10C}K\u{DD} K\u{16E}\u{147} \u{F6}\u{151}\u{F4}o!")); // &ŽLUŤOUČKÝ KŮŇ öőôo! -Assert::same('ZLUTOUCKY-KUN-oeooo', Strings::webalize("&\u{17D}LU\u{164}OU\u{10C}K\u{DD} K\u{16E}\u{147} \u{F6}\u{151}\u{F4}o!", null, false)); // &ŽLUŤOUČKÝ KŮŇ öőôo! +Assert::same( + 'zlutoucky-kun-oeooo', + Strings::webalize('&ŽLUŤOUČKÝ KŮŇ öőôo!'), +); +Assert::same( + 'ZLUTOUCKY-KUN-oeooo', + Strings::webalize('&ŽLUŤOUČKÝ KŮŇ öőôo!', lower: false), +); if (class_exists('Transliterator') && Transliterator::create('Any-Latin; Latin-ASCII')) { Assert::same('1-4-!', Strings::webalize("\u{BC} !", '!')); }