Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.61% |
19 / 23 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Utils | |
82.61% |
19 / 23 |
|
66.67% |
2 / 3 |
12.76 | |
0.00% |
0 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| addOrGetNamespace | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addUseStatements | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| searchOrAddProperty | |
75.00% |
12 / 16 |
|
0.00% |
0 / 1 |
7.77 | |||
| 1 | <?php |
| 2 | namespace Apie\Maker; |
| 3 | |
| 4 | use Apie\Common\ValueObjects\EntityNamespace; |
| 5 | use Nette\PhpGenerator\ClassType; |
| 6 | use Nette\PhpGenerator\PhpFile; |
| 7 | use Nette\PhpGenerator\PhpNamespace; |
| 8 | use Nette\PhpGenerator\PromotedParameter; |
| 9 | use Nette\PhpGenerator\Property; |
| 10 | |
| 11 | final class Utils |
| 12 | { |
| 13 | public const MAKER_CONFIG='maker_config'; |
| 14 | /** |
| 15 | * @codeCoverageIgnore |
| 16 | */ |
| 17 | private function __construct() |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | public static function addOrGetNamespace(PhpFile $phpFile, EntityNamespace $namespace): PhpNamespace |
| 22 | { |
| 23 | $ns = rtrim((string) $namespace, '\\'); |
| 24 | return $phpFile->getNamespaces()[$ns] ?? $phpFile->addNamespace($ns); |
| 25 | } |
| 26 | |
| 27 | public static function addUseStatements(PhpNamespace $phpNamespace, string... $useStatements): void |
| 28 | { |
| 29 | $uses = $phpNamespace->getUses(); |
| 30 | foreach ($useStatements as $useStatement) { |
| 31 | if (!in_array($useStatement, $uses)) { |
| 32 | $phpNamespace->addUse($useStatement); |
| 33 | $uses[] = $useStatement; |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public static function searchOrAddProperty(ClassType $classType, string $propertyName, bool $noPromotion, bool $nullDefault = false): Property|PromotedParameter |
| 39 | { |
| 40 | if ($classType->hasProperty($propertyName)) { |
| 41 | return $classType->getProperty($propertyName); |
| 42 | } |
| 43 | if (!$classType->hasMethod('__construct')) { |
| 44 | $classType->addMethod('__construct'); |
| 45 | } |
| 46 | $method = $classType->getMethod('__construct'); |
| 47 | if ($method->hasParameter($propertyName)) { |
| 48 | $parameter = $method->getParameter($propertyName); |
| 49 | if ($parameter instanceof PromotedParameter) { |
| 50 | return $parameter; |
| 51 | } |
| 52 | return $classType->addProperty($propertyName)->setPrivate(); |
| 53 | } |
| 54 | if ($noPromotion) { |
| 55 | return $classType->addProperty($propertyName)->setPrivate(); |
| 56 | } |
| 57 | $prop = $method->addPromotedParameter($propertyName)->setPrivate(); |
| 58 | if ($nullDefault) { |
| 59 | $prop->setDefaultValue(null); |
| 60 | } |
| 61 | return $prop; |
| 62 | } |
| 63 | } |