Skip to content

Commit 49268f6

Browse files
committed
coding style
1 parent 059a93b commit 49268f6

27 files changed

+178
-227
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static function from($class, bool $withBodies = false, bool $materializeT
112112
public static function withBodiesFrom($class): self
113113
{
114114
return (new Factory)
115-
->fromClassReflection(new \ReflectionClass($class), true);
115+
->fromClassReflection(new \ReflectionClass($class), withBodies: true);
116116
}
117117

118118

@@ -368,9 +368,7 @@ public function addTrait(string $name, $resolutions = [])
368368
return $trait;
369369
}
370370

371-
array_map(function ($item) use ($trait) {
372-
$trait->addResolution($item);
373-
}, $resolutions);
371+
array_map(fn($item) => $trait->addResolution($item), $resolutions);
374372
return $this;
375373
}
376374

@@ -389,25 +387,14 @@ public function removeTrait(string $name): self
389387
*/
390388
public function addMember($member): self
391389
{
392-
if ($member instanceof Method) {
393-
$this->methods[strtolower($member->getName())] = $member;
394-
395-
} elseif ($member instanceof Property) {
396-
$this->properties[$member->getName()] = $member;
397-
398-
} elseif ($member instanceof Constant) {
399-
$this->consts[$member->getName()] = $member;
400-
401-
} elseif ($member instanceof EnumCase) {
402-
$this->cases[$member->getName()] = $member;
403-
404-
} elseif ($member instanceof TraitUse) {
405-
$this->traits[$member->getName()] = $member;
406-
407-
} else {
408-
throw new Nette\InvalidArgumentException('Argument must be Method|Property|Constant|EnumCase|TraitUse.');
409-
}
410-
390+
match (true) {
391+
$member instanceof Method => $this->methods[strtolower($member->getName())] = $member,
392+
$member instanceof Property => $this->properties[$member->getName()] = $member,
393+
$member instanceof Constant => $this->consts[$member->getName()] = $member,
394+
$member instanceof EnumCase => $this->cases[$member->getName()] = $member,
395+
$member instanceof TraitUse => $this->traits[$member->getName()] = $member,
396+
default => throw new Nette\InvalidArgumentException('Argument must be Method|Property|Constant|EnumCase|TraitUse.'),
397+
};
411398
return $this;
412399
}
413400

@@ -637,7 +624,7 @@ public function validate(): void
637624
private function validateNames(array $names): void
638625
{
639626
foreach ($names as $name) {
640-
if (!Helpers::isNamespaceIdentifier($name, true)) {
627+
if (!Helpers::isNamespaceIdentifier($name, allowLeadingSlash: true)) {
641628
throw new Nette\InvalidArgumentException("Value '$name' is not valid class name.");
642629
}
643630
}
@@ -646,7 +633,7 @@ private function validateNames(array $names): void
646633

647634
public function __clone()
648635
{
649-
$clone = function ($item) { return clone $item; };
636+
$clone = fn($item) => clone $item;
650637
$this->cases = array_map($clone, $this->cases);
651638
$this->consts = array_map($clone, $this->consts);
652639
$this->properties = array_map($clone, $this->properties);

src/PhpGenerator/Dumper.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,10 @@ private function dumpString(string $s): string
7777
$utf8 = preg_match('##u', $s);
7878
$escaped = preg_replace_callback(
7979
$utf8 ? '#[\p{C}\\\\]#u' : '#[\x00-\x1F\x7F-\xFF\\\\]#',
80-
function ($m) use ($special) {
81-
return $special[$m[0]] ?? (strlen($m[0]) === 1
80+
fn($m) => $special[$m[0]] ?? (strlen($m[0]) === 1
8281
? '\x' . str_pad(strtoupper(dechex(ord($m[0]))), 2, '0', STR_PAD_LEFT) . ''
83-
: '\u{' . strtoupper(ltrim(dechex(self::utf8Ord($m[0])), '0')) . '}');
84-
},
85-
$s
82+
: '\u{' . strtoupper(ltrim(dechex(self::utf8Ord($m[0])), '0')) . '}'),
83+
$s,
8684
);
8785
return $s === str_replace('\\\\', '\\', $escaped)
8886
? "'" . preg_replace('#\'|\\\\(?=[\'\\\\]|$)#D', '\\\\$0', $s) . "'"
@@ -93,15 +91,12 @@ function ($m) use ($special) {
9391
private static function utf8Ord(string $c): int
9492
{
9593
$ord0 = ord($c[0]);
96-
if ($ord0 < 0x80) {
97-
return $ord0;
98-
} elseif ($ord0 < 0xE0) {
99-
return ($ord0 << 6) + ord($c[1]) - 0x3080;
100-
} elseif ($ord0 < 0xF0) {
101-
return ($ord0 << 12) + (ord($c[1]) << 6) + ord($c[2]) - 0xE2080;
102-
} else {
103-
return ($ord0 << 18) + (ord($c[1]) << 12) + (ord($c[2]) << 6) + ord($c[3]) - 0x3C82080;
104-
}
94+
return match (true) {
95+
$ord0 < 0x80 => $ord0,
96+
$ord0 < 0xE0 => ($ord0 << 6) + ord($c[1]) - 0x3080,
97+
$ord0 < 0xF0 => ($ord0 << 12) + (ord($c[1]) << 6) + ord($c[2]) - 0xE2080,
98+
default => ($ord0 << 18) + (ord($c[1]) << 12) + (ord($c[2]) << 6) + ord($c[3]) - 0x3C82080,
99+
};
105100
}
106101

107102

@@ -146,7 +141,7 @@ private function dumpObject($var, array $parents, int $level): string
146141
return 'unserialize(' . $this->dumpString(serialize($var)) . ')';
147142

148143
} elseif ($var instanceof \UnitEnum) {
149-
return '\\' . get_class($var) . '::' . $var->name;
144+
return '\\' . $var::class . '::' . $var->name;
150145

151146
} elseif ($var instanceof \Closure) {
152147
$inner = Nette\Utils\Callback::unwrap($var);
@@ -159,7 +154,7 @@ private function dumpObject($var, array $parents, int $level): string
159154
throw new Nette\InvalidArgumentException('Cannot dump closure.');
160155
}
161156

162-
$class = get_class($var);
157+
$class = $var::class;
163158
if ((new \ReflectionObject($var))->isAnonymous()) {
164159
throw new Nette\InvalidArgumentException('Cannot dump anonymous class.');
165160

src/PhpGenerator/Extractor.php

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ private function parseCode(string $code): void
6161
public function extractMethodBodies(string $className): array
6262
{
6363
$nodeFinder = new NodeFinder;
64-
$classNode = $nodeFinder->findFirst($this->statements, function (Node $node) use ($className) {
65-
return ($node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Trait_)
66-
&& $node->namespacedName->toString() === $className;
67-
});
64+
$classNode = $nodeFinder->findFirst(
65+
$this->statements,
66+
fn(Node $node) => ($node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Trait_)
67+
&& $node->namespacedName->toString() === $className,
68+
);
6869

6970
$res = [];
7071
foreach ($nodeFinder->findInstanceOf($classNode, Node\Stmt\ClassMethod::class) as $methodNode) {
@@ -81,9 +82,10 @@ public function extractMethodBodies(string $className): array
8182
public function extractFunctionBody(string $name): ?string
8283
{
8384
/** @var Node\Stmt\Function_ $functionNode */
84-
$functionNode = (new NodeFinder)->findFirst($this->statements, function (Node $node) use ($name) {
85-
return $node instanceof Node\Stmt\Function_ && $node->namespacedName->toString() === $name;
86-
});
85+
$functionNode = (new NodeFinder)->findFirst(
86+
$this->statements,
87+
fn(Node $node) => $node instanceof Node\Stmt\Function_ && $node->namespacedName->toString() === $name,
88+
);
8789

8890
return $this->getReformattedContents($functionNode->stmts, 1);
8991
}
@@ -105,9 +107,11 @@ private function prepareReplacements(array $statements): array
105107
(new NodeFinder)->find($statements, function (Node $node) use (&$replacements, $start) {
106108
if ($node instanceof Node\Name\FullyQualified) {
107109
if ($node->getAttribute('originalName') instanceof Node\Name) {
108-
$of = $node->getAttribute('parent') instanceof Node\Expr\ConstFetch
109-
? PhpNamespace::NAME_CONSTANT
110-
: ($node->getAttribute('parent') instanceof Node\Expr\FuncCall ? PhpNamespace::NAME_FUNCTION : PhpNamespace::NAME_NORMAL);
110+
$of = match (true) {
111+
$node->getAttribute('parent') instanceof Node\Expr\ConstFetch => PhpNamespace::NAME_CONSTANT,
112+
$node->getAttribute('parent') instanceof Node\Expr\FuncCall => PhpNamespace::NAME_FUNCTION,
113+
default => PhpNamespace::NAME_NORMAL,
114+
};
111115
$replacements[] = [
112116
$node->getStartFilePos() - $start,
113117
$node->getEndFilePos() - $start,
@@ -117,7 +121,7 @@ private function prepareReplacements(array $statements): array
117121
} elseif ($node instanceof Node\Scalar\String_ || $node instanceof Node\Scalar\EncapsedStringPart) {
118122
// multi-line strings => singleline
119123
$token = $this->getNodeContents($node);
120-
if (strpos($token, "\n") !== false) {
124+
if (str_contains($token, "\n")) {
121125
$quote = $node instanceof Node\Scalar\String_ ? '"' : '';
122126
$replacements[] = [
123127
$node->getStartFilePos() - $start,
@@ -147,9 +151,7 @@ private function prepareReplacements(array $statements): array
147151

148152
private function performReplacements(string $s, array $replacements): string
149153
{
150-
usort($replacements, function ($a, $b) { // sort by position in file
151-
return $b[0] <=> $a[0];
152-
});
154+
usort($replacements, fn($a, $b) => $b[0] <=> $a[0]);
153155

154156
foreach ($replacements as [$start, $end, $replacement]) {
155157
$s = substr_replace($s, $replacement, $start, $end - $start + 1);
@@ -171,38 +173,25 @@ public function enterNode(Node $node)
171173
};
172174

173175
$visitor->callback = function (Node $node) use (&$class, &$namespace, $phpFile) {
174-
if ($node instanceof Node\Stmt\DeclareDeclare && $node->key->name === 'strict_types') {
175-
$phpFile->setStrictTypes((bool) $node->value->value);
176-
} elseif ($node instanceof Node\Stmt\Namespace_) {
177-
$namespace = $node->name ? $node->name->toString() : '';
178-
} elseif ($node instanceof Node\Stmt\Use_) {
179-
$this->addUseToNamespace($node, $phpFile->addNamespace($namespace));
180-
} elseif ($node instanceof Node\Stmt\Class_) {
181-
if (!$node->name) {
182-
return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN;
183-
}
184-
185-
$class = $this->addClassToFile($phpFile, $node);
186-
} elseif ($node instanceof Node\Stmt\Interface_) {
187-
$class = $this->addInterfaceToFile($phpFile, $node);
188-
} elseif ($node instanceof Node\Stmt\Trait_) {
189-
$class = $this->addTraitToFile($phpFile, $node);
190-
} elseif ($node instanceof Node\Stmt\Enum_) {
191-
$class = $this->addEnumToFile($phpFile, $node);
192-
} elseif ($node instanceof Node\Stmt\Function_) {
193-
$this->addFunctionToFile($phpFile, $node);
194-
} elseif ($node instanceof Node\Stmt\TraitUse) {
195-
$this->addTraitToClass($class, $node);
196-
} elseif ($node instanceof Node\Stmt\Property) {
197-
$this->addPropertyToClass($class, $node);
198-
} elseif ($node instanceof Node\Stmt\ClassMethod) {
199-
$this->addMethodToClass($class, $node);
200-
} elseif ($node instanceof Node\Stmt\ClassConst) {
201-
$this->addConstantToClass($class, $node);
202-
} elseif ($node instanceof Node\Stmt\EnumCase) {
203-
$this->addEnumCaseToClass($class, $node);
176+
if ($node instanceof Node\Stmt\Class_ && !$node->name) {
177+
return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN;
204178
}
205-
179+
match (true) {
180+
$node instanceof Node\Stmt\DeclareDeclare && $node->key->name === 'strict_types' => $phpFile->setStrictTypes((bool) $node->value->value),
181+
$node instanceof Node\Stmt\Namespace_ => $namespace = $node->name?->toString(),
182+
$node instanceof Node\Stmt\Use_ => $this->addUseToNamespace($node, $phpFile->addNamespace($namespace)),
183+
$node instanceof Node\Stmt\Class_ => $class = $this->addClassToFile($phpFile, $node),
184+
$node instanceof Node\Stmt\Interface_ => $class = $this->addInterfaceToFile($phpFile, $node),
185+
$node instanceof Node\Stmt\Trait_ => $class = $this->addTraitToFile($phpFile, $node),
186+
$node instanceof Node\Stmt\Enum_ => $class = $this->addEnumToFile($phpFile, $node),
187+
$node instanceof Node\Stmt\Function_ => $this->addFunctionToFile($phpFile, $node),
188+
$node instanceof Node\Stmt\TraitUse => $this->addTraitToClass($class, $node),
189+
$node instanceof Node\Stmt\Property => $this->addPropertyToClass($class, $node),
190+
$node instanceof Node\Stmt\ClassMethod => $this->addMethodToClass($class, $node),
191+
$node instanceof Node\Stmt\ClassConst => $this->addConstantToClass($class, $node),
192+
$node instanceof Node\Stmt\EnumCase => $this->addEnumCaseToClass($class, $node),
193+
default => null,
194+
};
206195
if ($node instanceof Node\FunctionLike) {
207196
return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN;
208197
}
@@ -227,7 +216,7 @@ private function addUseToNamespace(Node\Stmt\Use_ $node, PhpNamespace $namespace
227216
$node::TYPE_CONSTANT => PhpNamespace::NAME_CONSTANT,
228217
][$node->type];
229218
foreach ($node->uses as $use) {
230-
$namespace->addUse($use->name->toString(), $use->alias ? $use->alias->toString() : null, $of);
219+
$namespace->addUse($use->name->toString(), $use->alias?->toString(), $of);
231220
}
232221
}
233222

@@ -360,7 +349,7 @@ private function addConstantToClass(ClassType $class, Node\Stmt\ClassConst $node
360349

361350
private function addEnumCaseToClass(ClassType $class, Node\Stmt\EnumCase $node)
362351
{
363-
$case = $class->addCase($node->name->toString(), $node->expr ? $node->expr->value : null);
352+
$case = $class->addCase($node->name->toString(), $node->expr?->value);
364353
$this->addCommentAndAttributes($case, $node);
365354
}
366355

src/PhpGenerator/Factory.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class Factory
2727
public function fromClassReflection(
2828
\ReflectionClass $from,
2929
bool $withBodies = false,
30-
bool $materializeTraits = true
30+
bool $materializeTraits = true,
3131
): ClassType {
3232
if ($withBodies && $from->isAnonymous()) {
3333
throw new Nette\NotSupportedException('The $withBodies parameter cannot be used for anonymous functions.');
@@ -50,9 +50,7 @@ public function fromClassReflection(
5050

5151
$ifaces = $from->getInterfaceNames();
5252
foreach ($ifaces as $iface) {
53-
$ifaces = array_filter($ifaces, function (string $item) use ($iface): bool {
54-
return !is_subclass_of($iface, $item);
55-
});
53+
$ifaces = array_filter($ifaces, fn(string $item): bool => !is_subclass_of($iface, $item));
5654
}
5755

5856
if ($from->isInterface()) {
@@ -99,7 +97,7 @@ public function fromClassReflection(
9997
if ($withBodies) {
10098
$realMethodClass = $realMethod->getDeclaringClass();
10199
$bodies = &$this->bodyCache[$realMethodClass->name];
102-
$bodies = $bodies ?? $this->getExtractor($realMethodClass)->extractMethodBodies($realMethodClass->name);
100+
$bodies ??= $this->getExtractor($realMethodClass)->extractMethodBodies($realMethodClass->name);
103101
if (isset($bodies[$realMethod->name])) {
104102
$m->setBody($bodies[$realMethod->name]);
105103
}
@@ -298,18 +296,14 @@ public function fromPropertyReflection(\ReflectionProperty $from): Property
298296

299297
public function fromObject(object $obj): Literal
300298
{
301-
return new Literal('new ' . get_class($obj) . '(/* unknown */)');
299+
return new Literal('new ' . $obj::class . '(/* unknown */)');
302300
}
303301

304302

305303
public function fromClassCode(string $code): ClassType
306304
{
307305
$classes = $this->fromCode($code)->getClasses();
308-
if (!$classes) {
309-
throw new Nette\InvalidStateException('The code does not contain any class.');
310-
}
311-
312-
return reset($classes);
306+
return reset($classes) ?: throw new Nette\InvalidStateException('The code does not contain any class.');
313307
}
314308

315309

src/PhpGenerator/GlobalFunction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static function from(string $function, bool $withBody = false): self
3333

3434
public static function withBodyFrom(string $function): self
3535
{
36-
return (new Factory)->fromFunctionReflection(new \ReflectionFunction($function), true);
36+
return (new Factory)->fromFunctionReflection(new \ReflectionFunction($function), withBody: true);
3737
}
3838

3939

src/PhpGenerator/PhpFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function addNamespace($namespace): PhpNamespace
7777
$res = $this->namespaces[$namespace->getName()] = $namespace;
7878

7979
} elseif (is_string($namespace)) {
80-
$res = $this->namespaces[$namespace] = $this->namespaces[$namespace] ?? new PhpNamespace($namespace);
80+
$res = $this->namespaces[$namespace] ??= new PhpNamespace($namespace);
8181

8282
} else {
8383
throw new Nette\InvalidArgumentException('Argument must be string|PhpNamespace.');

src/PhpGenerator/PhpNamespace.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function addUse(string $name, ?string $alias = null, string $of = self::N
122122
$lower = strtolower($alias);
123123
if (isset($aliases[$lower]) && strcasecmp($aliases[$lower], $name) !== 0) {
124124
throw new InvalidStateException(
125-
"Alias '$alias' used already for '{$aliases[$lower]}', cannot use for '$name'."
125+
"Alias '$alias' used already for '{$aliases[$lower]}', cannot use for '$name'.",
126126
);
127127
} elseif (isset($used[$lower])) {
128128
throw new Nette\InvalidStateException("Name '$alias' used already for '$this->name\\{$used[$lower]->getName()}'.");
@@ -154,8 +154,8 @@ public function getUses(string $of = self::NAME_NORMAL): array
154154
asort($this->aliases[$of]);
155155
return array_filter(
156156
$this->aliases[$of],
157-
function ($name, $alias) { return strcasecmp(($this->name ? $this->name . '\\' : '') . $alias, $name); },
158-
ARRAY_FILTER_USE_BOTH
157+
fn($name, $alias) => strcasecmp(($this->name ? $this->name . '\\' : '') . $alias, $name),
158+
ARRAY_FILTER_USE_BOTH,
159159
);
160160
}
161161

@@ -190,7 +190,7 @@ public function resolveName(string $name, string $of = self::NAME_NORMAL): strin
190190

191191
public function simplifyType(string $type, string $of = self::NAME_NORMAL): string
192192
{
193-
return preg_replace_callback('~[\w\x7f-\xff\\\\]+~', function ($m) use ($of) { return $this->simplifyName($m[0], $of); }, $type);
193+
return preg_replace_callback('~[\w\x7f-\xff\\\\]+~', fn($m) => $this->simplifyName($m[0], $of), $type);
194194
}
195195

196196

0 commit comments

Comments
 (0)