Skip to content

Commit a98efae

Browse files
authored
Merge pull request #113 from michaelpetri/feature/add-final-const-support
Added support for final const properties
2 parents e9e2dcc + 75b3bc4 commit a98efae

File tree

7 files changed

+211
-133
lines changed

7 files changed

+211
-133
lines changed

src/Generator/ClassGenerator.php

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use function is_scalar;
1919
use function is_string;
2020
use function ltrim;
21+
use function method_exists;
2122
use function rtrim;
2223
use function sprintf;
2324
use function str_replace;
@@ -117,10 +118,13 @@ public static function fromReflection(ClassReflection $classReflection)
117118

118119
$constants = [];
119120

120-
foreach ($classReflection->getConstants() as $name => $value) {
121+
foreach ($classReflection->getReflectionConstants() as $constReflection) {
121122
$constants[] = [
122-
'name' => $name,
123-
'value' => $value,
123+
'name' => $constReflection->getName(),
124+
'value' => $constReflection->getValue(),
125+
'isFinal' => method_exists($constReflection, 'isFinal')
126+
? $constReflection->isFinal()
127+
: false,
124128
];
125129
}
126130

@@ -159,7 +163,7 @@ public static function fromReflection(ClassReflection $classReflection)
159163
* @configkey methods
160164
* @throws Exception\InvalidArgumentException
161165
* @param array $array
162-
* @return self
166+
* @return static
163167
*/
164168
public static function fromArray(array $array)
165169
{
@@ -255,7 +259,7 @@ public function __construct(
255259

256260
/**
257261
* @param string $name
258-
* @return self
262+
* @return static
259263
*/
260264
public function setName($name)
261265
{
@@ -279,7 +283,7 @@ public function getName()
279283

280284
/**
281285
* @param ?string $namespaceName
282-
* @return self
286+
* @return static
283287
*/
284288
public function setNamespaceName($namespaceName)
285289
{
@@ -296,7 +300,7 @@ public function getNamespaceName()
296300
}
297301

298302
/**
299-
* @return self
303+
* @return static
300304
*/
301305
public function setContainingFileGenerator(FileGenerator $fileGenerator)
302306
{
@@ -313,7 +317,7 @@ public function getContainingFileGenerator()
313317
}
314318

315319
/**
316-
* @return self
320+
* @return static
317321
*/
318322
public function setDocBlock(DocBlockGenerator $docBlock)
319323
{
@@ -331,7 +335,7 @@ public function getDocBlock()
331335

332336
/**
333337
* @param int[]|int $flags
334-
* @return self
338+
* @return static
335339
*/
336340
public function setFlags($flags)
337341
{
@@ -350,7 +354,7 @@ public function setFlags($flags)
350354

351355
/**
352356
* @param int $flag
353-
* @return self
357+
* @return static
354358
*/
355359
public function addFlag($flag)
356360
{
@@ -360,7 +364,7 @@ public function addFlag($flag)
360364

361365
/**
362366
* @param int $flag
363-
* @return self
367+
* @return static
364368
*/
365369
public function removeFlag($flag)
366370
{
@@ -370,7 +374,7 @@ public function removeFlag($flag)
370374

371375
/**
372376
* @param bool $isAbstract
373-
* @return self
377+
* @return static
374378
*/
375379
public function setAbstract($isAbstract)
376380
{
@@ -387,7 +391,7 @@ public function isAbstract()
387391

388392
/**
389393
* @param bool $isFinal
390-
* @return self
394+
* @return static
391395
*/
392396
public function setFinal($isFinal)
393397
{
@@ -405,7 +409,7 @@ public function isFinal()
405409
/**
406410
* @param ?string $extendedClass
407411
* @psalm-param ?class-string $extendedClass
408-
* @return self
412+
* @return static
409413
*/
410414
public function setExtendedClass($extendedClass)
411415
{
@@ -431,7 +435,7 @@ public function hasExtentedClass()
431435
}
432436

433437
/**
434-
* @return self
438+
* @return static
435439
*/
436440
public function removeExtentedClass()
437441
{
@@ -442,7 +446,7 @@ public function removeExtentedClass()
442446
/**
443447
* @param string[] $implementedInterfaces
444448
* @psalm-param array<class-string> $implementedInterfaces
445-
* @return self
449+
* @return static
446450
*/
447451
public function setImplementedInterfaces(array $implementedInterfaces)
448452
{
@@ -477,7 +481,7 @@ public function hasImplementedInterface($implementedInterface)
477481
/**
478482
* @param string $implementedInterface
479483
* @psalm-param class-string $implementedInterface
480-
* @return self
484+
* @return static
481485
*/
482486
public function removeImplementedInterface($implementedInterface)
483487
{
@@ -514,7 +518,7 @@ public function getConstants()
514518

515519
/**
516520
* @param string $constantName
517-
* @return self
521+
* @return static
518522
*/
519523
public function removeConstant($constantName)
520524
{
@@ -536,7 +540,7 @@ public function hasConstant($constantName)
536540
* Add constant from PropertyGenerator
537541
*
538542
* @throws Exception\InvalidArgumentException
539-
* @return self
543+
* @return static
540544
*/
541545
public function addConstantFromGenerator(PropertyGenerator $constant)
542546
{
@@ -567,9 +571,9 @@ public function addConstantFromGenerator(PropertyGenerator $constant)
567571
* @param string $name Non-empty string
568572
* @param string|int|null|float|array $value Scalar
569573
* @throws Exception\InvalidArgumentException
570-
* @return self
574+
* @return static
571575
*/
572-
public function addConstant($name, $value)
576+
public function addConstant($name, $value, bool $isFinal = false)
573577
{
574578
if (empty($name) || ! is_string($name)) {
575579
throw new Exception\InvalidArgumentException(sprintf(
@@ -581,13 +585,19 @@ public function addConstant($name, $value)
581585
$this->validateConstantValue($value);
582586

583587
return $this->addConstantFromGenerator(
584-
new PropertyGenerator($name, new PropertyValueGenerator($value), PropertyGenerator::FLAG_CONSTANT)
588+
new PropertyGenerator(
589+
$name,
590+
new PropertyValueGenerator($value),
591+
$isFinal
592+
? PropertyGenerator::FLAG_CONSTANT | PropertyGenerator::FLAG_FINAL
593+
: PropertyGenerator::FLAG_CONSTANT
594+
)
585595
);
586596
}
587597

588598
/**
589599
* @param PropertyGenerator[]|array[] $constants
590-
* @return self
600+
* @return static
591601
*/
592602
public function addConstants(array $constants)
593603
{
@@ -606,7 +616,7 @@ public function addConstants(array $constants)
606616

607617
/**
608618
* @param PropertyGenerator[]|string[]|array[] $properties
609-
* @return self
619+
* @return static
610620
*/
611621
public function addProperties(array $properties)
612622
{
@@ -630,7 +640,7 @@ public function addProperties(array $properties)
630640
* @param string|array $defaultValue
631641
* @param int $flags
632642
* @throws Exception\InvalidArgumentException
633-
* @return self
643+
* @return static
634644
*/
635645
public function addProperty($name, $defaultValue = null, $flags = PropertyGenerator::FLAG_PUBLIC)
636646
{
@@ -655,7 +665,7 @@ public function addProperty($name, $defaultValue = null, $flags = PropertyGenera
655665
* Add property from PropertyGenerator
656666
*
657667
* @throws Exception\InvalidArgumentException
658-
* @return self
668+
* @return static
659669
*/
660670
public function addPropertyFromGenerator(PropertyGenerator $property)
661671
{
@@ -706,7 +716,7 @@ public function getProperty($propertyName)
706716
*
707717
* @param string $use
708718
* @param string|null $useAlias
709-
* @return self
719+
* @return static
710720
*/
711721
public function addUse($use, $useAlias = null)
712722
{
@@ -725,7 +735,7 @@ public function hasUse($use)
725735

726736
/**
727737
* @param string $use
728-
* @return self
738+
* @return static
729739
*/
730740
public function removeUse($use)
731741
{
@@ -744,7 +754,7 @@ public function hasUseAlias($use)
744754

745755
/**
746756
* @param string $use
747-
* @return self
757+
* @return static
748758
*/
749759
public function removeUseAlias($use)
750760
{
@@ -764,7 +774,7 @@ public function getUses()
764774

765775
/**
766776
* @param string $propertyName
767-
* @return self
777+
* @return static
768778
*/
769779
public function removeProperty($propertyName)
770780
{
@@ -784,7 +794,7 @@ public function hasProperty($propertyName)
784794

785795
/**
786796
* @param MethodGenerator[]|string[]|array[] $methods
787-
* @return self
797+
* @return static
788798
*/
789799
public function addMethods(array $methods)
790800
{
@@ -810,7 +820,7 @@ public function addMethods(array $methods)
810820
* @param string $body
811821
* @param string $docBlock
812822
* @throws Exception\InvalidArgumentException
813-
* @return self
823+
* @return static
814824
*/
815825
public function addMethod(
816826
$name,
@@ -834,7 +844,7 @@ public function addMethod(
834844
* Add Method from MethodGenerator
835845
*
836846
* @throws Exception\InvalidArgumentException
837-
* @return self
847+
* @return static
838848
*/
839849
public function addMethodFromGenerator(MethodGenerator $method)
840850
{
@@ -870,7 +880,7 @@ public function getMethod($methodName)
870880

871881
/**
872882
* @param string $methodName
873-
* @return self
883+
* @return static
874884
*/
875885
public function removeMethod($methodName)
876886
{

src/Generator/InterfaceGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static function fromReflection(ClassReflection $classReflection)
7575
* @configkey methods
7676
* @throws Exception\InvalidArgumentException
7777
* @param array $array
78-
* @return InterfaceGenerator
78+
* @return static
7979
*/
8080
public static function fromArray(array $array)
8181
{

src/Generator/PropertyGenerator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,12 @@ public function generate()
211211
$this->name
212212
));
213213
}
214-
return $output . $this->indentation . $this->getVisibility() . ' const ' . $name . ' = '
214+
return $output
215+
. $this->indentation
216+
. ($this->isFinal() ? 'final ' : '')
217+
. $this->getVisibility()
218+
. ' const '
219+
. $name . ' = '
215220
. ($defaultValue !== null ? $defaultValue->generate() : 'null;');
216221
}
217222

src/Generator/TraitGenerator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function fromReflection(ClassReflection $classReflection)
6868
* @configkey methods
6969
* @throws Exception\InvalidArgumentException
7070
* @param array $array
71-
* @return TraitGenerator
71+
* @return static
7272
*/
7373
public static function fromArray(array $array)
7474
{
@@ -115,7 +115,7 @@ public function setFlags($flags)
115115

116116
/**
117117
* @param int $flag
118-
* @return self
118+
* @return static
119119
*/
120120
public function addFlag($flag)
121121
{
@@ -124,7 +124,7 @@ public function addFlag($flag)
124124

125125
/**
126126
* @param int $flag
127-
* @return self
127+
* @return static
128128
*/
129129
public function removeFlag($flag)
130130
{
@@ -141,7 +141,7 @@ public function setFinal($isFinal)
141141

142142
/**
143143
* @param ?string $extendedClass
144-
* @return self
144+
* @return static
145145
*/
146146
public function setExtendedClass($extendedClass)
147147
{

0 commit comments

Comments
 (0)