Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 3a9ec93

Browse files
committed
Merge branch 'feature/#26-complete-has-and-remove-support-on-class-generator' into develop
Close #26
2 parents 85c5e66 + e185e2f commit 3a9ec93

File tree

4 files changed

+304
-28
lines changed

4 files changed

+304
-28
lines changed

doc/book/generator/reference.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class Zend\Code\Generator\ClassGenerator extends Zend\Code\Generator\AbstractGen
104104
public function addConstant($property)
105105
public function getConstants()
106106
public function getConstant($propertyName)
107+
public function removeConstant($constantName)
107108
public function setDocblock(Zend\Code\Generator\DocBlockGenerator $docblock)
108109
public function getDocblock()
109110
public function setName($name)
@@ -112,12 +113,15 @@ class Zend\Code\Generator\ClassGenerator extends Zend\Code\Generator\AbstractGen
112113
public function isAbstract()
113114
public function setExtendedClass($extendedClass)
114115
public function getExtendedClass()
116+
public function hasExtentedClass()
117+
public function removeExtentedClass()
115118
public function setImplementedInterfaces(Array $implementedInterfaces)
116119
public function getImplementedInterfaces()
117120
public function addProperties(Array $properties)
118121
public function addProperty($property)
119122
public function getProperties()
120123
public function getProperty($propertyName)
124+
public function removeProperty($propertyName)
121125
public function addMethods(Array $methods)
122126
public function addMethod(
123127
$name,
@@ -129,6 +133,12 @@ class Zend\Code\Generator\ClassGenerator extends Zend\Code\Generator\AbstractGen
129133
public function getMethods()
130134
public function getMethod($methodName)
131135
public function hasMethod($methodName)
136+
public function hasUse($use)
137+
public function removeUse($use)
138+
public function hasUseAlias($use)
139+
public function removeUseAlias($use)
140+
public function hasImplementedInterface($implementedInterface)
141+
public function removeImplementedInterface($implementedInterface)
132142
public function isSourceDirty()
133143
public function generate()
134144
}

src/Generator/ClassGenerator.php

Lines changed: 128 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class ClassGenerator extends AbstractGenerator
7878
* Build a Code Generation Php Object from a Class Reflection
7979
*
8080
* @param ClassReflection $classReflection
81-
* @return ClassGenerator
81+
* @return self
8282
*/
8383
public static function fromReflection(ClassReflection $classReflection)
8484
{
@@ -167,7 +167,7 @@ public static function fromReflection(ClassReflection $classReflection)
167167
*
168168
* @throws Exception\InvalidArgumentException
169169
* @param array $array
170-
* @return ClassGenerator
170+
* @return self
171171
*/
172172
public static function fromArray(array $array)
173173
{
@@ -262,7 +262,7 @@ public function __construct(
262262

263263
/**
264264
* @param string $name
265-
* @return ClassGenerator
265+
* @return self
266266
*/
267267
public function setName($name)
268268
{
@@ -286,7 +286,7 @@ public function getName()
286286

287287
/**
288288
* @param string $namespaceName
289-
* @return ClassGenerator
289+
* @return self
290290
*/
291291
public function setNamespaceName($namespaceName)
292292
{
@@ -304,7 +304,7 @@ public function getNamespaceName()
304304

305305
/**
306306
* @param FileGenerator $fileGenerator
307-
* @return ClassGenerator
307+
* @return self
308308
*/
309309
public function setContainingFileGenerator(FileGenerator $fileGenerator)
310310
{
@@ -322,7 +322,7 @@ public function getContainingFileGenerator()
322322

323323
/**
324324
* @param DocBlockGenerator $docBlock
325-
* @return ClassGenerator
325+
* @return self
326326
*/
327327
public function setDocBlock(DocBlockGenerator $docBlock)
328328
{
@@ -340,7 +340,7 @@ public function getDocBlock()
340340

341341
/**
342342
* @param array|string $flags
343-
* @return ClassGenerator
343+
* @return self
344344
*/
345345
public function setFlags($flags)
346346
{
@@ -359,7 +359,7 @@ public function setFlags($flags)
359359

360360
/**
361361
* @param string $flag
362-
* @return ClassGenerator
362+
* @return self
363363
*/
364364
public function addFlag($flag)
365365
{
@@ -369,7 +369,7 @@ public function addFlag($flag)
369369

370370
/**
371371
* @param string $flag
372-
* @return ClassGenerator
372+
* @return self
373373
*/
374374
public function removeFlag($flag)
375375
{
@@ -379,7 +379,7 @@ public function removeFlag($flag)
379379

380380
/**
381381
* @param bool $isAbstract
382-
* @return ClassGenerator
382+
* @return self
383383
*/
384384
public function setAbstract($isAbstract)
385385
{
@@ -396,7 +396,7 @@ public function isAbstract()
396396

397397
/**
398398
* @param bool $isFinal
399-
* @return ClassGenerator
399+
* @return self
400400
*/
401401
public function setFinal($isFinal)
402402
{
@@ -413,7 +413,7 @@ public function isFinal()
413413

414414
/**
415415
* @param string $extendedClass
416-
* @return ClassGenerator
416+
* @return self
417417
*/
418418
public function setExtendedClass($extendedClass)
419419
{
@@ -429,12 +429,33 @@ public function getExtendedClass()
429429
return $this->extendedClass;
430430
}
431431

432+
/**
433+
* @return bool
434+
*/
435+
public function hasExtentedClass()
436+
{
437+
return !empty($this->extendedClass);
438+
}
439+
440+
/**
441+
* @return self
442+
*/
443+
public function removeExtentedClass()
444+
{
445+
$this->setExtendedClass(null);
446+
return $this;
447+
}
448+
432449
/**
433450
* @param array $implementedInterfaces
434-
* @return ClassGenerator
451+
* @return self
435452
*/
436453
public function setImplementedInterfaces(array $implementedInterfaces)
437454
{
455+
array_map(function ($implementedInterface) {
456+
return (string) TypeGenerator::fromTypeString($implementedInterface);
457+
}, $implementedInterfaces);
458+
438459
$this->implementedInterfaces = $implementedInterfaces;
439460
return $this;
440461
}
@@ -447,9 +468,29 @@ public function getImplementedInterfaces()
447468
return $this->implementedInterfaces;
448469
}
449470

471+
/**
472+
* @param string $implementedInterface
473+
* @return bool
474+
*/
475+
public function hasImplementedInterface($implementedInterface)
476+
{
477+
$implementedInterface = (string) TypeGenerator::fromTypeString($implementedInterface);
478+
return in_array($implementedInterface, $this->implementedInterfaces);
479+
}
480+
481+
/**
482+
* @param $implementedInterface
483+
* @return self
484+
*/
485+
public function removeImplementedInterface($implementedInterface)
486+
{
487+
$implementedInterface = (string) TypeGenerator::fromTypeString($implementedInterface);
488+
unset($this->implementedInterfaces[array_search($implementedInterface, $this->implementedInterfaces)]);
489+
return $this;
490+
}
491+
450492
/**
451493
* @param string $constantName
452-
*
453494
* @return PropertyGenerator|false
454495
*/
455496
public function getConstant($constantName)
@@ -469,6 +510,17 @@ public function getConstants()
469510
return $this->constants;
470511
}
471512

513+
/**
514+
* @param string $constantName
515+
* @return self
516+
*/
517+
public function removeConstant($constantName)
518+
{
519+
unset($this->constants[$constantName]);
520+
521+
return $this;
522+
}
523+
472524
/**
473525
* @param string $constantName
474526
* @return bool
@@ -483,7 +535,7 @@ public function hasConstant($constantName)
483535
*
484536
* @param PropertyGenerator $constant
485537
* @throws Exception\InvalidArgumentException
486-
* @return ClassGenerator
538+
* @return self
487539
*/
488540
public function addConstantFromGenerator(PropertyGenerator $constant)
489541
{
@@ -516,7 +568,7 @@ public function addConstantFromGenerator(PropertyGenerator $constant)
516568
*
517569
* @throws Exception\InvalidArgumentException
518570
*
519-
* @return ClassGenerator
571+
* @return self
520572
*/
521573
public function addConstant($name, $value)
522574
{
@@ -537,7 +589,7 @@ public function addConstant($name, $value)
537589
/**
538590
* @param PropertyGenerator[]|array[] $constants
539591
*
540-
* @return ClassGenerator
592+
* @return self
541593
*/
542594
public function addConstants(array $constants)
543595
{
@@ -556,7 +608,7 @@ public function addConstants(array $constants)
556608

557609
/**
558610
* @param array $properties
559-
* @return ClassGenerator
611+
* @return self
560612
*/
561613
public function addProperties(array $properties)
562614
{
@@ -582,7 +634,7 @@ public function addProperties(array $properties)
582634
* @param string|array $defaultValue
583635
* @param int $flags
584636
* @throws Exception\InvalidArgumentException
585-
* @return ClassGenerator
637+
* @return self
586638
*/
587639
public function addProperty($name, $defaultValue = null, $flags = PropertyGenerator::FLAG_PUBLIC)
588640
{
@@ -608,7 +660,7 @@ public function addProperty($name, $defaultValue = null, $flags = PropertyGenera
608660
*
609661
* @param PropertyGenerator $property
610662
* @throws Exception\InvalidArgumentException
611-
* @return ClassGenerator
663+
* @return self
612664
*/
613665
public function addPropertyFromGenerator(PropertyGenerator $property)
614666
{
@@ -659,14 +711,52 @@ public function getProperty($propertyName)
659711
*
660712
* @param string $use
661713
* @param string|null $useAlias
662-
* @return ClassGenerator
714+
* @return self
663715
*/
664716
public function addUse($use, $useAlias = null)
665717
{
666718
$this->traitUsageGenerator->addUse($use, $useAlias);
667719
return $this;
668720
}
669721

722+
/**
723+
* @param string $use
724+
* @return self
725+
*/
726+
public function hasUse($use)
727+
{
728+
return $this->traitUsageGenerator->hasUse($use);
729+
}
730+
731+
/**
732+
* @param string $use
733+
* @return self
734+
*/
735+
public function removeUse($use)
736+
{
737+
$this->traitUsageGenerator->removeUse($use);
738+
return $this;
739+
}
740+
741+
/**
742+
* @param string $use
743+
* @return bool
744+
*/
745+
public function hasUseAlias($use)
746+
{
747+
return $this->traitUsageGenerator->hasUseAlias($use);
748+
}
749+
750+
/**
751+
* @param $use
752+
* @return self
753+
*/
754+
public function removeUseAlias($use)
755+
{
756+
$this->traitUsageGenerator->removeUseAlias($use);
757+
return $this;
758+
}
759+
670760
/**
671761
* Returns the "use" classes
672762
*
@@ -677,6 +767,18 @@ public function getUses()
677767
return $this->traitUsageGenerator->getUses();
678768
}
679769

770+
771+
/**
772+
* @param string $propertyName
773+
* @return self
774+
*/
775+
public function removeProperty($propertyName)
776+
{
777+
unset($this->properties[$propertyName]);
778+
779+
return $this;
780+
}
781+
680782
/**
681783
* @param string $propertyName
682784
* @return bool
@@ -688,7 +790,7 @@ public function hasProperty($propertyName)
688790

689791
/**
690792
* @param array $methods
691-
* @return ClassGenerator
793+
* @return self
692794
*/
693795
public function addMethods(array $methods)
694796
{
@@ -716,7 +818,7 @@ public function addMethods(array $methods)
716818
* @param string $body
717819
* @param string $docBlock
718820
* @throws Exception\InvalidArgumentException
719-
* @return ClassGenerator
821+
* @return self
720822
*/
721823
public function addMethod(
722824
$name,
@@ -741,7 +843,7 @@ public function addMethod(
741843
*
742844
* @param MethodGenerator $method
743845
* @throws Exception\InvalidArgumentException
744-
* @return ClassGenerator
846+
* @return self
745847
*/
746848
public function addMethodFromGenerator(MethodGenerator $method)
747849
{
@@ -777,13 +879,11 @@ public function getMethod($methodName)
777879

778880
/**
779881
* @param string $methodName
780-
* @return ClassGenerator
882+
* @return self
781883
*/
782884
public function removeMethod($methodName)
783885
{
784-
if ($this->hasMethod($methodName)) {
785-
unset($this->methods[strtolower($methodName)]);
786-
}
886+
unset($this->methods[strtolower($methodName)]);
787887

788888
return $this;
789889
}

0 commit comments

Comments
 (0)