GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#121)
by joseph
56:14
created

DoctrineStaticMeta::getGetters()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 32
rs 8.6506
c 0
b 0
f 0
ccs 0
cts 20
cp 0
cc 7
nc 7
nop 0
crap 56
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta;
4
5
use Doctrine\Common\Inflector\Inflector;
6
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
7
use Doctrine\ORM\Mapping\ClassMetadata;
8
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator;
9
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\UsesPHPMetaDataInterface;
10
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
11
12
class DoctrineStaticMeta
13
{
14
    /**
15
     * @var \ts\Reflection\ReflectionClass
16
     */
17
    private $reflectionClass;
18
19
    /**
20
     * @var ClassMetadata
21
     */
22
    private $metaData;
23
24
25
    /**
26
     * @var string
27
     */
28
    private $singular;
29
30
    /**
31
     * @var string
32
     */
33
    private $plural;
34
35
    /**
36
     * @var array
37
     */
38
    private $setters;
39
40
    /**
41
     * @var array
42
     */
43
    private $getters;
44
45
    /**
46
     * @var array|null
47
     */
48
    private $staticMethods;
49
50
    /**
51
     * DoctrineStaticMeta constructor.
52
     *
53
     * @param string $entityFqn
54
     *
55
     * @throws \ReflectionException
56
     */
57
    public function __construct(string $entityFqn)
58
    {
59
        $this->reflectionClass = new \ts\Reflection\ReflectionClass($entityFqn);
60
    }
61
62
    public function buildMetaData(): void
63
    {
64
        $builder = new ClassMetadataBuilder($this->metaData);
65
        $this->loadPropertyDoctrineMetaData($builder);
66
        $this->loadClassDoctrineMetaData($builder);
67
        self::setChangeTrackingPolicy($builder);
68
    }
69
70
    /**
71
     * This method will reflect on the entity class and pull out all the methods that begin with
72
     * UsesPHPMetaDataInterface::METHOD_PREFIX_GET_PROPERTY_DOCTRINE_META
73
     *
74
     * Once it has an array of methods, it calls them all, passing in the $builder
75
     *
76
     * @param ClassMetadataBuilder $builder
77
     *
78
     * @throws DoctrineStaticMetaException
79
     * @SuppressWarnings(PHPMD.StaticAccess)
80
     */
81
    private function loadPropertyDoctrineMetaData(ClassMetadataBuilder $builder): void
82
    {
83
        $methodName = '__no_method__';
84
        try {
85
            $staticMethods = $this->getStaticMethods();
86
            //now loop through and call them
87
            foreach ($staticMethods as $method) {
88
                $methodName = $method->getName();
89
                if (0 === stripos(
90
                    $methodName,
91
                    UsesPHPMetaDataInterface::METHOD_PREFIX_GET_PROPERTY_DOCTRINE_META
92
                )
93
                ) {
94
                    $method->setAccessible(true);
95
                    $method->invokeArgs(null, [$builder]);
96
                }
97
            }
98
        } catch (\Exception $e) {
99
            throw new DoctrineStaticMetaException(
100
                'Exception in ' . __METHOD__ . ' for '
101
                . $this->reflectionClass->getName() . "::$methodName\n\n"
102
                . $e->getMessage(),
103
                $e->getCode(),
104
                $e
105
            );
106
        }
107
    }
108
109
    /**
110
     * Get an array of all static methods implemented by the current class
111
     *
112
     * Merges trait methods
113
     * Filters out this trait
114
     *
115
     * @return array|\ReflectionMethod[]
116
     * @throws \ReflectionException
117
     */
118
    public function getStaticMethods(): array
119
    {
120
        if (null !== $this->staticMethods) {
121
            return $this->staticMethods;
122
        }
123
        $this->staticMethods = $this->reflectionClass->getMethods(
124
            \ReflectionMethod::IS_STATIC
125
        );
126
        return $this->staticMethods;
127
    }
128
129
    /**
130
     * Get class level meta data, eg table name, custom repository
131
     *
132
     * @param ClassMetadataBuilder $builder
133
     *
134
     * @SuppressWarnings(PHPMD.StaticAccess)
135
     */
136
    private function loadClassDoctrineMetaData(ClassMetadataBuilder $builder): void
137
    {
138
        $tableName = MappingHelper::getTableNameForEntityFqn($this->reflectionClass->getName());
139
        $builder->setTable($tableName);
140
        $this->callPrivateStaticMethodOnEntity('setCustomRepositoryClass', [$builder]);
141
    }
142
143
    private function callPrivateStaticMethodOnEntity(string $methodName, array $args): void
144
    {
145
        $method = $this->reflectionClass->getMethod($methodName);
146
        $method->setAccessible(true);
147
        $method->invokeArgs(null, $args);
148
    }
149
150
    /**
151
     * Setting the change policy to be Notify - best performance
152
     *
153
     * @see http://doctrine-orm.readthedocs.io/en/latest/reference/change-tracking-policies.html
154
     *
155
     * @param ClassMetadataBuilder $builder
156
     */
157
    public static function setChangeTrackingPolicy(ClassMetadataBuilder $builder): void
158
    {
159
        $builder->setChangeTrackingPolicyNotify();
160
    }
161
162
    /**
163
     * Get the property name the Entity is mapped by when plural
164
     *
165
     * Override it in your entity class if you are using an Entity class name that doesn't pluralize nicely
166
     *
167
     * @return string
168
     * @throws DoctrineStaticMetaException
169
     * @SuppressWarnings(PHPMD.StaticAccess)
170
     */
171
    public function getPlural(): string
172
    {
173
        try {
174
            if (null === $this->plural) {
175
                $singular     = $this->getSingular();
176
                $this->plural = Inflector::pluralize($singular);
177
            }
178
179
            return $this->plural;
180
        } catch (\Exception $e) {
181
            throw new DoctrineStaticMetaException(
182
                'Exception in ' . __METHOD__ . ': ' . $e->getMessage(),
183
                $e->getCode(),
184
                $e
185
            );
186
        }
187
    }
188
189
    /**
190
     * Get the property the name the Entity is mapped by when singular
191
     *
192
     * @return string
193
     * @throws DoctrineStaticMetaException
194
     * @SuppressWarnings(PHPMD.StaticAccess)
195
     */
196
    public function getSingular(): string
197
    {
198
        try {
199
            if (null === $this->singular) {
200
                $reflectionClass = $this->getReflectionClass();
201
202
                $shortName         = $reflectionClass->getShortName();
203
                $singularShortName = Inflector::singularize($shortName);
204
205
                $namespaceName   = $reflectionClass->getNamespaceName();
206
                $namespaceParts  = \explode(AbstractGenerator::ENTITIES_FOLDER_NAME, $namespaceName);
207
                $entityNamespace = \array_pop($namespaceParts);
208
209
                $namespacedShortName = \preg_replace(
210
                    '/\\\\/',
211
                    '',
212
                    $entityNamespace . $singularShortName
213
                );
214
215
                $this->singular = \lcfirst($namespacedShortName);
216
            }
217
218
            return $this->singular;
219
        } catch (\Exception $e) {
220
            throw new DoctrineStaticMetaException(
221
                'Exception in ' . __METHOD__ . ': ' . $e->getMessage(),
222
                $e->getCode(),
223
                $e
224
            );
225
        }
226
    }
227
228
    /**
229
     * @return \ts\Reflection\ReflectionClass
230
     */
231
    public function getReflectionClass(): \ts\Reflection\ReflectionClass
232
    {
233
        return $this->reflectionClass;
234
    }
235
236
    /**
237
     * Get an array of setters by name
238
     *
239
     * @return array|string[]
240
     */
241
    public function getSetters(): array
242
    {
243
        if (null !== $this->setters) {
244
            return $this->setters;
245
        }
246
        $skip            = [
247
            'addPropertyChangedListener' => true,
248
        ];
249
        $this->setters   = [];
250
        $reflectionClass = $this->getReflectionClass();
251
        foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
252
            $methodName = $method->getName();
253
            if (isset($skip[$methodName])) {
254
                continue;
255
            }
256
            if (\ts\stringStartsWith($methodName, 'set')) {
257
                $this->setters[] = $methodName;
258
                continue;
259
            }
260
            if (\ts\stringStartsWith($methodName, 'add')) {
261
                $this->setters[] = $methodName;
262
                continue;
263
            }
264
        }
265
266
        return $this->setters;
267
    }
268
269
    /**
270
     * Get the short name (without fully qualified namespace) of the current Entity
271
     *
272
     * @return string
273
     */
274
    public function getShortName(): string
275
    {
276
        $reflectionClass = $this->getReflectionClass();
277
278
        return $reflectionClass->getShortName();
279
    }
280
281
    /**
282
     * Get an array of getters by name
283
     * [];
284
     *
285
     * @return array|string[]
286
     */
287
    public function getGetters(): array
288
    {
289
        if (null !== $this->getters) {
290
            return $this->getters;
291
        }
292
        $skip = [
293
            'getDoctrineStaticMeta' => true,
294
            'isValid'               => true,
295
        ];
296
297
        $this->getters   = [];
298
        $reflectionClass = $this->getReflectionClass();
299
        foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
300
            $methodName = $method->getName();
301
            if (isset($skip[$methodName])) {
302
                continue;
303
            }
304
            if (\ts\stringStartsWith($methodName, 'get')) {
305
                $this->getters[] = $methodName;
306
                continue;
307
            }
308
            if (\ts\stringStartsWith($methodName, 'is')) {
309
                $this->getters[] = $methodName;
310
                continue;
311
            }
312
            if (\ts\stringStartsWith($methodName, 'has')) {
313
                $this->getters[] = $methodName;
314
                continue;
315
            }
316
        }
317
318
        return $this->getters;
319
    }
320
321
    /**
322
     * @return ClassMetadata
323
     */
324
    public function getMetaData(): ClassMetadata
325
    {
326
        return $this->metaData;
327
    }
328
329
    /**
330
     * @param ClassMetadata $metaData
331
     *
332
     * @return DoctrineStaticMeta
333
     */
334
    public function setMetaData(ClassMetadata $metaData): self
335
    {
336
        $this->metaData = $metaData;
337
338
        return $this;
339
    }
340
}
341