edmondscommerce /
doctrine-static-meta
| 1 | <?php declare(strict_types=1); |
||||
| 2 | |||||
| 3 | namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration; |
||||
| 4 | |||||
| 5 | use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator; |
||||
| 6 | use ts\Reflection\ReflectionClass; |
||||
| 7 | |||||
| 8 | class ReflectionHelper |
||||
| 9 | { |
||||
| 10 | |||||
| 11 | /** |
||||
| 12 | * @var NamespaceHelper |
||||
| 13 | */ |
||||
| 14 | protected $namespaceHelper; |
||||
| 15 | |||||
| 16 | public function __construct(NamespaceHelper $namespaceHelper) |
||||
| 17 | { |
||||
| 18 | $this->namespaceHelper = $namespaceHelper; |
||||
| 19 | } |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * @param ReflectionClass $fieldTraitReflection |
||||
| 23 | * |
||||
| 24 | * @return string |
||||
| 25 | */ |
||||
| 26 | public function getFakerProviderFqnFromFieldTraitReflection(ReflectionClass $fieldTraitReflection |
||||
| 27 | ): string |
||||
| 28 | { |
||||
| 29 | return \str_replace( |
||||
| 30 | [ |
||||
| 31 | '\\Traits\\', |
||||
| 32 | 'FieldTrait', |
||||
| 33 | ], |
||||
| 34 | [ |
||||
| 35 | '\\FakerData\\', |
||||
| 36 | 'FakerData', |
||||
| 37 | ], |
||||
| 38 | $fieldTraitReflection->getName() |
||||
| 39 | ); |
||||
| 40 | } |
||||
| 41 | |||||
| 42 | /** |
||||
| 43 | * Work out the entity namespace root from a single entity reflection object. |
||||
| 44 | * |
||||
| 45 | * @param ReflectionClass $entityReflection |
||||
| 46 | * |
||||
| 47 | * @return string |
||||
| 48 | */ |
||||
| 49 | public function getEntityNamespaceRootFromEntityReflection( |
||||
| 50 | ReflectionClass $entityReflection |
||||
| 51 | ): string { |
||||
| 52 | return $this->namespaceHelper->tidy( |
||||
| 53 | $this->namespaceHelper->getNamespaceRootToDirectoryFromFqn( |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 54 | $entityReflection->getName(), |
||||
| 55 | AbstractGenerator::ENTITIES_FOLDER_NAME |
||||
| 56 | ) |
||||
| 57 | ); |
||||
| 58 | } |
||||
| 59 | |||||
| 60 | /** |
||||
| 61 | * Find which trait is implementing a method in a class |
||||
| 62 | * |
||||
| 63 | * @param ReflectionClass $class |
||||
| 64 | * @param string $methodName |
||||
| 65 | * |
||||
| 66 | * @return ReflectionClass |
||||
| 67 | * @throws \ReflectionException |
||||
| 68 | */ |
||||
| 69 | public function getTraitImplementingMethod(ReflectionClass $class, string $methodName): ReflectionClass |
||||
| 70 | { |
||||
| 71 | $traitsWithMethod = []; |
||||
| 72 | foreach ($class->getTraits() as $trait) { |
||||
| 73 | try { |
||||
| 74 | $trait->getMethod($methodName); |
||||
| 75 | $traitsWithMethod[] = $trait; |
||||
| 76 | } catch (\ReflectionException $e) { |
||||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||||
| 77 | } |
||||
| 78 | } |
||||
| 79 | if (count($traitsWithMethod) > 1) { |
||||
| 80 | throw new \RuntimeException( |
||||
| 81 | 'Found more than one trait implementing the method ' . $methodName . ' in ' . |
||||
| 82 | $class->getShortName() |
||||
| 83 | ); |
||||
| 84 | } |
||||
| 85 | if ([] === $traitsWithMethod) { |
||||
| 86 | throw new \RuntimeException( |
||||
| 87 | 'Failed finding trait implementing the method ' . $methodName . ' in ' . |
||||
| 88 | $class->getShortName() |
||||
| 89 | ); |
||||
| 90 | } |
||||
| 91 | |||||
| 92 | return current($traitsWithMethod); |
||||
| 93 | } |
||||
| 94 | |||||
| 95 | /** |
||||
| 96 | * Find which trait is implementing a method in a class |
||||
| 97 | * |
||||
| 98 | * @param ReflectionClass $class |
||||
| 99 | * @param string $propertyName |
||||
| 100 | * |
||||
| 101 | * @return ReflectionClass |
||||
| 102 | * @throws \ReflectionException |
||||
| 103 | */ |
||||
| 104 | public function getTraitProvidingProperty(ReflectionClass $class, string $propertyName): ReflectionClass |
||||
| 105 | { |
||||
| 106 | $traitsWithProperty = []; |
||||
| 107 | foreach ($class->getTraits() as $trait) { |
||||
| 108 | try { |
||||
| 109 | $trait->getProperty($propertyName); |
||||
| 110 | $traitsWithProperty[] = $trait; |
||||
| 111 | } catch (\ReflectionException $e) { |
||||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||||
| 112 | } |
||||
| 113 | } |
||||
| 114 | if ([] === $traitsWithProperty) { |
||||
| 115 | if ($class->isTrait() && $class->hasProperty($propertyName)) { |
||||
| 116 | return $class; |
||||
| 117 | } |
||||
| 118 | throw new \RuntimeException( |
||||
| 119 | 'Failed finding trait providing the property ' . $propertyName . ' in ' . |
||||
| 120 | $class->getShortName() |
||||
| 121 | ); |
||||
| 122 | } |
||||
| 123 | if (count($traitsWithProperty) > 1) { |
||||
| 124 | throw new \RuntimeException( |
||||
| 125 | 'Found more than one trait providing the property ' . $propertyName . ' in ' . |
||||
| 126 | $class->getShortName() |
||||
| 127 | ); |
||||
| 128 | } |
||||
| 129 | |||||
| 130 | return current($traitsWithProperty); |
||||
| 131 | } |
||||
| 132 | |||||
| 133 | /** |
||||
| 134 | * Get the full method body using reflection |
||||
| 135 | * |
||||
| 136 | * @param string $methodName |
||||
| 137 | * @param ReflectionClass $reflectionClass |
||||
| 138 | * |
||||
| 139 | * @return string |
||||
| 140 | */ |
||||
| 141 | public function getMethodBody(string $methodName, ReflectionClass $reflectionClass): string |
||||
| 142 | { |
||||
| 143 | $method = $reflectionClass->getMethod($methodName); |
||||
| 144 | $startLine = $method->getStartLine() - 1; |
||||
| 145 | $length = $method->getEndLine() - $startLine; |
||||
| 146 | $lines = file($reflectionClass->getFileName()); |
||||
| 147 | $methodLines = \array_slice($lines, $startLine, $length); |
||||
|
0 ignored issues
–
show
It seems like
$lines can also be of type false; however, parameter $array of array_slice() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 148 | |||||
| 149 | return implode('', $methodLines); |
||||
| 150 | } |
||||
| 151 | |||||
| 152 | /** |
||||
| 153 | * @param ReflectionClass $reflectionClass |
||||
| 154 | * |
||||
| 155 | * @return array|string[] |
||||
| 156 | */ |
||||
| 157 | public function getUseStatements(ReflectionClass $reflectionClass): array |
||||
| 158 | { |
||||
| 159 | $content = \ts\file_get_contents($reflectionClass->getFileName()); |
||||
| 160 | preg_match_all('%^use.+?;%m', $content, $matches); |
||||
| 161 | |||||
| 162 | return $matches[0]; |
||||
| 163 | } |
||||
| 164 | } |
||||
| 165 |