Skip to content

Commit 6cd7e11

Browse files
committed
improved PHPDoc descriptions
1 parent 0d70609 commit 6cd7e11

26 files changed

Lines changed: 196 additions & 90 deletions

src/PhpGenerator/ClassLike.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
/**
15-
* Base definition of class, interface, trait or enum type.
15+
* Base for class, interface, trait, and enum definitions.
1616
*/
1717
abstract class ClassLike
1818
{
@@ -35,7 +35,11 @@ abstract class ClassLike
3535
private ?string $name;
3636

3737

38-
/** @param class-string|object $class */
38+
/**
39+
* Creates an instance from a class reflection.
40+
* @param class-string|object $class
41+
* @param bool $withBodies load method bodies (requires nikic/php-parser)
42+
*/
3943
public static function from(string|object $class, bool $withBodies = false): static
4044
{
4145
$instance = (new Factory)
@@ -50,6 +54,9 @@ public static function from(string|object $class, bool $withBodies = false): sta
5054
}
5155

5256

57+
/**
58+
* Creates an instance by parsing PHP source code containing a class definition.
59+
*/
5360
public static function fromCode(string $code): static
5461
{
5562
$instance = (new Factory)
@@ -112,6 +119,9 @@ public function getName(): ?string
112119
}
113120

114121

122+
/**
123+
* Returns the fully qualified name including namespace, or just the short name if no namespace is set.
124+
*/
115125
public function getFullName(): ?string
116126
{
117127
return $this->name && ($namespace = $this->namespace?->getName())

src/PhpGenerator/ClassManipulator.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use const PHP_VERSION_ID;
1212

1313

14+
/**
15+
* Provides advanced manipulation of a ClassType, such as inheriting members from parent classes or implementing interfaces.
16+
*/
1417
final class ClassManipulator
1518
{
1619
public function __construct(
@@ -20,7 +23,8 @@ public function __construct(
2023

2124

2225
/**
23-
* Inherits property from parent class.
26+
* Copies a property from a parent class into this class for overriding.
27+
* @throws Nette\InvalidStateException if the property already exists or the parent is not set
2428
*/
2529
public function inheritProperty(string $name, bool $returnIfExists = false): Property
2630
{
@@ -48,7 +52,8 @@ public function inheritProperty(string $name, bool $returnIfExists = false): Pro
4852

4953

5054
/**
51-
* Inherits method from parent class or interface.
55+
* Copies a method from a parent class or interface into this class for overriding.
56+
* @throws Nette\InvalidStateException if the method already exists or the parent is not set
5257
*/
5358
public function inheritMethod(string $name, bool $returnIfExists = false): Method
5459
{
@@ -75,8 +80,8 @@ public function inheritMethod(string $name, bool $returnIfExists = false): Metho
7580

7681

7782
/**
78-
* Implements all methods from the given interface or abstract class.
79-
* @param class-string $name
83+
* Adds stub implementations for all abstract methods and properties from the given interface or abstract class.
84+
* @param class-string $name
8085
*/
8186
public function implement(string $name): void
8287
{

src/PhpGenerator/ClassType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ public function removeImplement(string $name): static
132132
}
133133

134134

135+
/**
136+
* Adds a member to the class.
137+
* @throws Nette\InvalidStateException if the member already exists and $overwrite is false
138+
*/
135139
public function addMember(Method|Property|Constant|TraitUse $member, bool $overwrite = false): static
136140
{
137141
$name = $member->getName();

src/PhpGenerator/Closure.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ final class Closure
2020
private array $uses = [];
2121

2222

23-
/** @param \Closure(): mixed $closure */
23+
/**
24+
* Creates an instance from a closure reflection.
25+
* @param \Closure(): mixed $closure
26+
*/
2427
public static function from(\Closure $closure): self
2528
{
2629
return (new Factory)->fromFunctionReflection(new \ReflectionFunction($closure));
@@ -52,6 +55,9 @@ public function getUses(): array
5255
}
5356

5457

58+
/**
59+
* Adds a variable binding to the closure's use list.
60+
*/
5561
public function addUse(string $name): Parameter
5662
{
5763
return $this->uses[] = new Parameter($name);

src/PhpGenerator/Dumper.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ final class Dumper
2626

2727

2828
/**
29-
* Returns a PHP representation of a variable.
29+
* Converts a value to its PHP code representation.
30+
* @param int $column current column for array wrapping decisions
3031
*/
3132
public function dump(mixed $var, int $column = 0): string
3233
{
@@ -214,7 +215,9 @@ private function dumpLiteral(Literal $var, int $level): string
214215

215216

216217
/**
217-
* Generates PHP statement. Supports placeholders: ? \? $? ->? ::? ...? ...?: ?*
218+
* Formats a PHP expression using placeholders.
219+
* Supported placeholders: ? (value), \? (literal ?), $? (variable), ->? (property access),
220+
* ::? (static access), ...? (spread array), ...?: (named spread), ?* (alias for ...?)
218221
*/
219222
public function format(string $statement, mixed ...$args): string
220223
{

src/PhpGenerator/EnumType.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ final class EnumType extends ClassLike
2727
private ?string $type = null;
2828

2929

30+
/**
31+
* Sets the backing type of a backed enum ('int' or 'string').
32+
*/
3033
public function setType(?string $type): static
3134
{
3235
$this->type = $type;
3336
return $this;
3437
}
3538

3639

40+
/**
41+
* Returns the backing type ('int' or 'string'), or null for a pure enum.
42+
*/
3743
public function getType(): ?string
3844
{
3945
return $this->type;
@@ -72,7 +78,7 @@ public function removeImplement(string $name): static
7278

7379

7480
/**
75-
* Sets cases to enum
81+
* Replaces all cases.
7682
* @param list<EnumCase> $cases
7783
*/
7884
public function setCases(array $cases): static
@@ -94,7 +100,10 @@ public function getCases(): array
94100
}
95101

96102

97-
/** Adds case to enum */
103+
/**
104+
* Adds a case to the enum.
105+
* @throws Nette\InvalidStateException if the case already exists and $overwrite is false
106+
*/
98107
public function addCase(string $name, string|int|Literal|null $value = null, bool $overwrite = false): EnumCase
99108
{
100109
if (!$overwrite && isset($this->cases[$name])) {
@@ -113,7 +122,8 @@ public function removeCase(string $name): static
113122

114123

115124
/**
116-
* Adds a member. If it already exists, throws an exception or overwrites it if $overwrite is true.
125+
* Adds a member to the enum.
126+
* @throws Nette\InvalidStateException if the member already exists and $overwrite is false
117127
*/
118128
public function addMember(Method|Constant|EnumCase|TraitUse $member, bool $overwrite = false): static
119129
{

src/PhpGenerator/Factory.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
/**
17-
* Creates a representations based on reflection or source code.
17+
* Creates PhpGenerator representations from reflection objects or PHP source code.
1818
*/
1919
final class Factory
2020
{
@@ -25,7 +25,11 @@ final class Factory
2525
private array $extractorCache = [];
2626

2727

28-
/** @param \ReflectionClass<object> $from */
28+
/**
29+
* Creates a ClassLike instance from a reflection object.
30+
* @param \ReflectionClass<object> $from
31+
* @param bool $withBodies load method bodies (requires nikic/php-parser, not available for anonymous/internal classes or interfaces)
32+
*/
2933
public function fromClassReflection(
3034
\ReflectionClass $from,
3135
bool $withBodies = false,
@@ -206,6 +210,10 @@ public function fromMethodReflection(\ReflectionMethod $from): Method
206210
}
207211

208212

213+
/**
214+
* Creates a GlobalFunction or Closure instance from a reflection object.
215+
* @param bool $withBody load function body (requires nikic/php-parser, not available for closures or internal functions)
216+
*/
209217
public function fromFunctionReflection(\ReflectionFunction $from, bool $withBody = false): GlobalFunction|Closure
210218
{
211219
$function = $from->isClosure() ? new Closure : new GlobalFunction($from->name);
@@ -232,7 +240,10 @@ public function fromFunctionReflection(\ReflectionFunction $from, bool $withBody
232240
}
233241

234242

235-
/** @param callable(): mixed $from */
243+
/**
244+
* Creates a Method, GlobalFunction, or Closure instance from a callable.
245+
* @param callable(): mixed $from
246+
*/
236247
public function fromCallable(callable $from): Method|GlobalFunction|Closure
237248
{
238249
$ref = Nette\Utils\Callback::toReflection($from);
@@ -367,13 +378,20 @@ public function fromObject(object $obj): Literal
367378
}
368379

369380

381+
/**
382+
* Parses PHP source code and returns the first class-like type found.
383+
* @throws Nette\InvalidStateException if the code contains no class
384+
*/
370385
public function fromClassCode(string $code): ClassLike
371386
{
372387
$classes = $this->fromCode($code)->getClasses();
373388
return reset($classes) ?: throw new Nette\InvalidStateException('The code does not contain any class.');
374389
}
375390

376391

392+
/**
393+
* Parses PHP source code and returns the resulting PhpFile representation.
394+
*/
377395
public function fromCode(string $code): PhpFile
378396
{
379397
$reader = new Extractor($code);

src/PhpGenerator/GlobalFunction.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ final class GlobalFunction
2020
use Traits\CommentAware;
2121
use Traits\AttributeAware;
2222

23-
/** @param string|(\Closure(): mixed) $function */
23+
/**
24+
* Creates an instance from a function name or closure reflection.
25+
* @param string|(\Closure(): mixed) $function
26+
* @param bool $withBody load function body (requires nikic/php-parser)
27+
*/
2428
public static function from(string|\Closure $function, bool $withBody = false): self
2529
{
2630
return (new Factory)->fromFunctionReflection(Nette\Utils\Callback::toReflection($function), $withBody);

src/PhpGenerator/Helpers.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ public static function createObject(string $class, array $props): object
138138
}
139139

140140

141+
/**
142+
* Validates a type declaration. Strips the leading '?' and sets $nullable=true for nullable shorthand.
143+
* Returns null for empty/null types.
144+
*/
141145
public static function validateType(?string $type, bool &$nullable = false): ?string
142146
{
143147
if ($type === '' || $type === null) {

src/PhpGenerator/InterfaceType.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public function getExtends(): array
4040
}
4141

4242

43+
/**
44+
* Adds a parent interface.
45+
*/
4346
public function addExtend(string $name): static
4447
{
4548
$this->validateNames([$name]);
@@ -49,7 +52,8 @@ public function addExtend(string $name): static
4952

5053

5154
/**
52-
* Adds a member. If it already exists, throws an exception or overwrites it if $overwrite is true.
55+
* Adds a member to the interface.
56+
* @throws Nette\InvalidStateException if the member already exists and $overwrite is false
5357
*/
5458
public function addMember(Method|Constant|Property $member, bool $overwrite = false): static
5559
{

0 commit comments

Comments
 (0)