Skip to content

Commit 0e53ed2

Browse files
committed
improved phpDoc and typehints
1 parent f7d123e commit 0e53ed2

16 files changed

+66
-29
lines changed

src/PhpGenerator/Attribute.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ final class Attribute
2020
use Nette\SmartObject;
2121

2222
private string $name;
23+
24+
/** @var mixed[] */
2325
private array $args;
2426

2527

28+
/** @param mixed[] $args */
2629
public function __construct(string $name, array $args)
2730
{
2831
if (!Helpers::isNamespaceIdentifier($name)) {
@@ -40,6 +43,7 @@ public function getName(): string
4043
}
4144

4245

46+
/** @return mixed[] */
4347
public function getArguments(): array
4448
{
4549
return $this->args;

src/PhpGenerator/ClassType.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ final class ClassType
5555
/** @var TraitUse[] */
5656
private array $traits = [];
5757

58-
/** @var Constant[] name => Constant */
58+
/** @var array<string, Constant> */
5959
private array $consts = [];
6060

61-
/** @var Property[] name => Property */
61+
/** @var array<string, Property> */
6262
private array $properties = [];
6363

64-
/** @var Method[] name => Method */
64+
/** @var array<string, Method> */
6565
private array $methods = [];
6666

67-
/** @var EnumCase[] name => EnumCase */
67+
/** @var array<string, EnumCase> */
6868
private array $cases = [];
6969

7070

@@ -401,7 +401,7 @@ public function getConstants(): array
401401
}
402402

403403

404-
public function addConstant(string $name, $value): Constant
404+
public function addConstant(string $name, mixed $value): Constant
405405
{
406406
if (isset($this->consts[$name])) {
407407
throw new Nette\InvalidStateException("Cannot add constant '$name', because it already exists.");
@@ -495,7 +495,7 @@ public function getProperty(string $name): Property
495495
/**
496496
* @param string $name without $
497497
*/
498-
public function addProperty(string $name, $value = null): Property
498+
public function addProperty(string $name, mixed $value = null): Property
499499
{
500500
if (isset($this->properties[$name])) {
501501
throw new Nette\InvalidStateException("Cannot add property '$name', because it already exists.");
@@ -603,6 +603,7 @@ public function validate(): void
603603
}
604604

605605

606+
/** @param string[] $names */
606607
private function validateNames(array $names): void
607608
{
608609
foreach ($names as $name) {

src/PhpGenerator/Closure.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function setUses(array $uses): static
5050
}
5151

5252

53+
/** @return Parameter[] */
5354
public function getUses(): array
5455
{
5556
return $this->uses;

src/PhpGenerator/Constant.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class Constant
2727
private bool $final = false;
2828

2929

30-
public function setValue($val): static
30+
public function setValue(mixed $val): static
3131
{
3232
$this->value = $val;
3333
return $this;

src/PhpGenerator/Dumper.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ final class Dumper
2727
/**
2828
* Returns a PHP representation of a variable.
2929
*/
30-
public function dump($var, int $column = 0): string
30+
public function dump(mixed $var, int $column = 0): string
3131
{
3232
return $this->dumpVar($var, [], 0, $column);
3333
}
3434

3535

36-
private function dumpVar(&$var, array $parents = [], int $level = 0, int $column = 0): string
36+
/** @param array<mixed[]|object> $parents */
37+
private function dumpVar(mixed &$var, array $parents = [], int $level = 0, int $column = 0): string
3738
{
3839
if ($var === null) {
3940
return 'null';
@@ -95,6 +96,10 @@ private static function utf8Ord(string $c): int
9596
}
9697

9798

99+
/**
100+
* @param mixed[] $var
101+
* @param array<mixed[]|object> $parents
102+
*/
98103
private function dumpArray(array &$var, array $parents, int $level, int $column): string
99104
{
100105
if (empty($var)) {
@@ -130,7 +135,8 @@ private function dumpArray(array &$var, array $parents, int $level, int $column)
130135
}
131136

132137

133-
private function dumpObject($var, array $parents, int $level): string
138+
/** @param array<mixed[]|object> $parents */
139+
private function dumpObject(object $var, array $parents, int $level): string
134140
{
135141
if ($var instanceof \Serializable) {
136142
return 'unserialize(' . $this->dumpString(serialize($var)) . ')';
@@ -200,7 +206,7 @@ private function dumpLiteral(Literal $var, int $level): string
200206
/**
201207
* Generates PHP statement. Supports placeholders: ? \? $? ->? ::? ...? ...?: ?*
202208
*/
203-
public function format(string $statement, ...$args): string
209+
public function format(string $statement, mixed ...$args): string
204210
{
205211
$tokens = preg_split('#(\.\.\.\?:?|\$\?|->\?|::\?|\\\\\?|\?\*|\?(?!\w))#', $statement, -1, PREG_SPLIT_DELIM_CAPTURE);
206212
$res = '';
@@ -239,6 +245,7 @@ public function format(string $statement, ...$args): string
239245
}
240246

241247

248+
/** @param mixed[] $var */
242249
private function dumpArguments(array &$var, int $column, bool $named): string
243250
{
244251
$outInline = $outWrapped = '';
@@ -257,6 +264,7 @@ private function dumpArguments(array &$var, int $column, bool $named): string
257264

258265

259266
/**
267+
* @param mixed[] $props
260268
* @internal
261269
*/
262270
public static function createObject(string $class, array $props): object

src/PhpGenerator/EnumCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function setValue(string|int|null $val): static
3232
}
3333

3434

35-
public function getValue()
35+
public function getValue(): string|int|null
3636
{
3737
return $this->value;
3838
}

src/PhpGenerator/Extractor.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ final class Extractor
2525
use Nette\SmartObject;
2626

2727
private string $code;
28+
29+
/** @var Node[] */
2830
private array $statements;
2931
private PhpParser\PrettyPrinterAbstract $printer;
3032

@@ -58,6 +60,7 @@ private function parseCode(string $code): void
5860
}
5961

6062

63+
/** @return array<string, string> */
6164
public function extractMethodBodies(string $className): array
6265
{
6366
$nodeFinder = new NodeFinder;
@@ -91,20 +94,24 @@ public function extractFunctionBody(string $name): ?string
9194
}
9295

9396

94-
/** @param Node[] $statements */
95-
private function getReformattedContents(array $statements, int $level): string
97+
/** @param Node[] $nodes */
98+
private function getReformattedContents(array $nodes, int $level): string
9699
{
97-
$body = $this->getNodeContents(...$statements);
98-
$body = $this->performReplacements($body, $this->prepareReplacements($statements));
100+
$body = $this->getNodeContents(...$nodes);
101+
$body = $this->performReplacements($body, $this->prepareReplacements($nodes));
99102
return Helpers::unindent($body, $level);
100103
}
101104

102105

103-
private function prepareReplacements(array $statements): array
106+
/**
107+
* @param Node[] $nodes
108+
* @return array<array{int, int, string}>
109+
*/
110+
private function prepareReplacements(array $nodes): array
104111
{
105-
$start = $statements[0]->getStartFilePos();
112+
$start = $nodes[0]->getStartFilePos();
106113
$replacements = [];
107-
(new NodeFinder)->find($statements, function (Node $node) use (&$replacements, $start) {
114+
(new NodeFinder)->find($nodes, function (Node $node) use (&$replacements, $start) {
108115
if ($node instanceof Node\Name\FullyQualified) {
109116
if ($node->getAttribute('originalName') instanceof Node\Name) {
110117
$of = match (true) {
@@ -149,6 +156,7 @@ private function prepareReplacements(array $statements): array
149156
}
150157

151158

159+
/** @param array<array{int, int, string}> $replacements */
152160
private function performReplacements(string $s, array $replacements): string
153161
{
154162
usort($replacements, fn($a, $b) => $b[0] <=> $a[0]);

src/PhpGenerator/Factory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ final class Factory
2020
{
2121
use Nette\SmartObject;
2222

23+
/** @var string[][] */
2324
private array $bodyCache = [];
25+
26+
/** @var Extractor[] */
2427
private array $extractorCache = [];
2528

2629

30+
/** @param \ReflectionClass<object> $from */
2731
public function fromClassReflection(
2832
\ReflectionClass $from,
2933
bool $withBodies = false,

src/PhpGenerator/Helpers.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ final class Helpers
4949

5050

5151
/** @deprecated use (new Nette\PhpGenerator\Dumper)->dump() */
52-
public static function dump($var): string
52+
public static function dump(mixed $var): string
5353
{
5454
trigger_error(__METHOD__ . '() is deprecated, use (new Nette\PhpGenerator\Dumper)->dump().', E_USER_DEPRECATED);
5555
return (new Dumper)->dump($var);
5656
}
5757

5858

5959
/** @deprecated use (new Nette\PhpGenerator\Dumper)->format() */
60-
public static function format(string $statement, ...$args): string
60+
public static function format(string $statement, mixed ...$args): string
6161
{
6262
trigger_error(__METHOD__ . '() is deprecated, use (new Nette\PhpGenerator\Dumper)->format().', E_USER_DEPRECATED);
6363
return (new Dumper)->format($statement, ...$args);
@@ -119,13 +119,13 @@ public static function unindent(string $s, int $level = 1): string
119119
}
120120

121121

122-
public static function isIdentifier($value): bool
122+
public static function isIdentifier(mixed $value): bool
123123
{
124124
return is_string($value) && preg_match('#^' . self::PHP_IDENT . '$#D', $value);
125125
}
126126

127127

128-
public static function isNamespaceIdentifier($value, bool $allowLeadingSlash = false): bool
128+
public static function isNamespaceIdentifier(mixed $value, bool $allowLeadingSlash = false): bool
129129
{
130130
$re = '#^' . ($allowLeadingSlash ? '\\\\?' : '') . self::PHP_IDENT . '(\\\\' . self::PHP_IDENT . ')*$#D';
131131
return is_string($value) && preg_match($re, $value);
@@ -152,7 +152,10 @@ public static function tabsToSpaces(string $s, int $count = 4): string
152152
}
153153

154154

155-
/** @internal */
155+
/**
156+
* @param mixed[] $props
157+
* @internal
158+
*/
156159
public static function createObject(string $class, array $props): object
157160
{
158161
return Dumper::createObject($class, $props);

src/PhpGenerator/Literal.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Literal
1717
{
1818
public function __construct(
1919
private string $value,
20+
/** @var ?mixed[] */
2021
private ?array $args = null,
2122
) {
2223
}

0 commit comments

Comments
 (0)