Skip to content

Commit

Permalink
cs
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 26, 2023
1 parent 54a435c commit 38d3044
Show file tree
Hide file tree
Showing 17 changed files with 46 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/Utils/ArrayHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Utils/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}


Expand Down Expand Up @@ -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);
}


Expand All @@ -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);
}


Expand Down
2 changes: 1 addition & 1 deletion src/Utils/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
12 changes: 10 additions & 2 deletions src/Utils/ObjectHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Utils/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand All @@ -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 ? '|' : '&',
);

Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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)
)
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/Validators.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}


Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/ArrayHash.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ test('', function () {
'children' => [
'c' => 'John',
],
], false);
], recursive: false);
Assert::type(Nette\Utils\ArrayHash::class, $list);
Assert::type('array', $list['children']);
});
Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/Arrays.flatten().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ $res = Arrays::flatten([
],
'y' => 'd',
'z' => 'e',
], true);
], preserveKeys: true);

Assert::same([
5 => 'a',
Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/Arrays.normalize.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ Assert::same(
Arrays::normalize([
1 => 'first',
'' => 'second',
], true),
], filling: true),
);
6 changes: 3 additions & 3 deletions tests/Utils/Callback.check.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
);
Expand All @@ -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.',
);
6 changes: 3 additions & 3 deletions tests/Utils/FileSystem.copy.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
);
Expand All @@ -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.",
);
Expand Down
4 changes: 2 additions & 2 deletions tests/Utils/FileSystem.rename.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
);
Expand All @@ -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.",
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/Finder.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});


Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/Strings.compare().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
2 changes: 1 addition & 1 deletion tests/Utils/Strings.normalize().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 8 additions & 3 deletions tests/Utils/Strings.webalize().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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} !", '!'));
}
Expand Down

0 comments on commit 38d3044

Please sign in to comment.