Skip to content

Commit d003c62

Browse files
committed
ClassType divided into InterfaceType, TraitType & EnumType
1 parent c3e5218 commit d003c62

25 files changed

+933
-528
lines changed

phpstan-baseline.neon

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
parameters:
22
ignoreErrors:
3+
-
4+
message: '#^Match expression does not handle remaining value\: true$#'
5+
count: 1
6+
path: src/PhpGenerator/ClassLike.php
7+
38
-
49
message: '#^Method Nette\\PhpGenerator\\ClassType\:\:addTrait\(\) has parameter \$deprecatedParam with no value type specified in iterable type array\.$#'
510
count: 1
611
path: src/PhpGenerator/ClassType.php
712

13+
-
14+
message: '#^Method Nette\\PhpGenerator\\EnumType\:\:addTrait\(\) has parameter \$deprecatedParam with no value type specified in iterable type array\.$#'
15+
count: 1
16+
path: src/PhpGenerator/EnumType.php
17+
818
-
919
message: '#^Access to an undefined property PhpParser\\Node\:\:\$attrGroups\.$#'
1020
count: 1
@@ -15,6 +25,26 @@ parameters:
1525
count: 1
1626
path: src/PhpGenerator/Extractor.php
1727

28+
-
29+
message: '#^Call to an undefined method Nette\\PhpGenerator\\ClassLike\:\:addConstant\(\)\.$#'
30+
count: 1
31+
path: src/PhpGenerator/Extractor.php
32+
33+
-
34+
message: '#^Call to an undefined method Nette\\PhpGenerator\\ClassLike\:\:addMethod\(\)\.$#'
35+
count: 1
36+
path: src/PhpGenerator/Extractor.php
37+
38+
-
39+
message: '#^Call to an undefined method Nette\\PhpGenerator\\ClassLike\:\:addProperty\(\)\.$#'
40+
count: 1
41+
path: src/PhpGenerator/Extractor.php
42+
43+
-
44+
message: '#^Call to an undefined method Nette\\PhpGenerator\\ClassLike\:\:addTrait\(\)\.$#'
45+
count: 1
46+
path: src/PhpGenerator/Extractor.php
47+
1848
-
1949
message: '#^Method Nette\\PhpGenerator\\Extractor\:\:addCommentAndAttributes\(\) has parameter \$element with no type specified\.$#'
2050
count: 1
@@ -60,6 +90,11 @@ parameters:
6090
count: 1
6191
path: src/PhpGenerator/Factory.php
6292

93+
-
94+
message: '#^Parameter \#1 \$name of method Nette\\PhpGenerator\\ClassType\:\:setExtends\(\) expects string\|null, array\<int, string\> given\.$#'
95+
count: 1
96+
path: src/PhpGenerator/Factory.php
97+
6398
-
6499
message: '#^Unreachable statement \- code above always terminates\.$#'
65100
count: 1
@@ -79,3 +114,8 @@ parameters:
79114
message: '#^Method Nette\\PhpGenerator\\Method\:\:from\(\) has parameter \$method with no value type specified in iterable type array\.$#'
80115
count: 1
81116
path: src/PhpGenerator/Method.php
117+
118+
-
119+
message: '#^Method Nette\\PhpGenerator\\TraitType\:\:addTrait\(\) has parameter \$deprecatedParam with no value type specified in iterable type array\.$#'
120+
count: 1
121+
path: src/PhpGenerator/TraitType.php

readme.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,8 @@ Interface or Trait
226226
You can create interfaces and traits:
227227

228228
```php
229-
$interface = Nette\PhpGenerator\ClassType::interface('MyInterface');
230-
$trait = Nette\PhpGenerator\ClassType::trait('MyTrait');
231-
// in a similar way $class = Nette\PhpGenerator\ClassType::class('MyClass');
229+
$interface = Nette\PhpGenerator\InterfaceType('MyInterface');
230+
$trait = Nette\PhpGenerator\TraitType('MyTrait');
232231
```
233232

234233
Enums
@@ -237,7 +236,7 @@ Enums
237236
You can easily create the enums that PHP 8.1 brings:
238237

239238
```php
240-
$enum = Nette\PhpGenerator\ClassType::enum('Suit');
239+
$enum = Nette\PhpGenerator\EnumType('Suit');
241240
$enum->addCase('Clubs');
242241
$enum->addCase('Diamonds');
243242
$enum->addCase('Hearts');

src/PhpGenerator/ClassLike.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\PhpGenerator;
11+
12+
use Nette;
13+
14+
15+
/**
16+
* Class/Interface/Trait/Enum description.
17+
*/
18+
abstract class ClassLike
19+
{
20+
use Nette\SmartObject;
21+
use Traits\CommentAware;
22+
use Traits\AttributeAware;
23+
24+
public const
25+
VISIBILITY_PUBLIC = 'public',
26+
VISIBILITY_PROTECTED = 'protected',
27+
VISIBILITY_PRIVATE = 'private';
28+
29+
private ?PhpNamespace $namespace;
30+
private ?string $name;
31+
32+
33+
public static function from(string|object $class, bool $withBodies = false, ?bool $materializeTraits = null): self
34+
{
35+
if ($materializeTraits !== null) {
36+
trigger_error(__METHOD__ . '() parameter $materializeTraits has been removed (is always false).', E_USER_DEPRECATED);
37+
}
38+
return (new Factory)
39+
->fromClassReflection(new \ReflectionClass($class), $withBodies);
40+
}
41+
42+
43+
/** @deprecated use from(..., withBodies: true) */
44+
public static function withBodiesFrom(string|object $class): self
45+
{
46+
trigger_error(__METHOD__ . '() is deprecated, use from(..., withBodies: true)', E_USER_DEPRECATED);
47+
return (new Factory)
48+
->fromClassReflection(new \ReflectionClass($class), withBodies: true);
49+
}
50+
51+
52+
public static function fromCode(string $code): self
53+
{
54+
return (new Factory)
55+
->fromClassCode($code);
56+
}
57+
58+
59+
public function __construct(string $name, ?PhpNamespace $namespace = null)
60+
{
61+
$this->setName($name);
62+
$this->namespace = $namespace;
63+
}
64+
65+
66+
public function __toString(): string
67+
{
68+
return (new Printer)->printClass($this, $this->namespace);
69+
}
70+
71+
72+
/** @deprecated an object can be in multiple namespaces */
73+
public function getNamespace(): ?PhpNamespace
74+
{
75+
return $this->namespace;
76+
}
77+
78+
79+
public function setName(?string $name): static
80+
{
81+
if ($name !== null && (!Helpers::isIdentifier($name) || isset(Helpers::KEYWORDS[strtolower($name)]))) {
82+
throw new Nette\InvalidArgumentException("Value '$name' is not valid class name.");
83+
}
84+
85+
$this->name = $name;
86+
return $this;
87+
}
88+
89+
90+
public function getName(): ?string
91+
{
92+
return $this->name;
93+
}
94+
95+
96+
public function isClass(): bool
97+
{
98+
return $this instanceof ClassType;
99+
}
100+
101+
102+
public function isInterface(): bool
103+
{
104+
return $this instanceof InterfaceType;
105+
}
106+
107+
108+
public function isTrait(): bool
109+
{
110+
return $this instanceof TraitType;
111+
}
112+
113+
114+
public function isEnum(): bool
115+
{
116+
return $this instanceof EnumType;
117+
}
118+
119+
120+
/** @param string[] $names */
121+
protected function validateNames(array $names): void
122+
{
123+
foreach ($names as $name) {
124+
if (!Helpers::isNamespaceIdentifier($name, allowLeadingSlash: true)) {
125+
throw new Nette\InvalidArgumentException("Value '$name' is not valid class name.");
126+
}
127+
}
128+
}
129+
130+
131+
public function validate(): void
132+
{
133+
}
134+
}

0 commit comments

Comments
 (0)