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
Push — master ( 7d5b61...8619f1 )
by joseph
11s
created

UsesPHPMetaDataTrait::loadMetadata()   A

Complexity

Conditions 2
Paths 6

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 6
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Traits;
4
5
use Doctrine\Common\Util\Inflector;
6
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator;
8
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\UsesPHPMetaDataInterface;
9
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
10
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper;
11
use Doctrine\ORM\Mapping\ClassMetadata as DoctrineClassMetaData;
12
13
trait UsesPHPMetaDataTrait
14
{
15
16
    /**
17
     * @var \ReflectionClass
18
     */
19
    private static $reflectionClass;
20
21
    /**
22
     * @var string
23
     */
24
    private static $singular;
25
26
    /**
27
     * @var string
28
     */
29
    private static $plural;
30
31
32
    public function __construct()
33
    {
34
        $this->runInitMethods();
35
    }
36
37
    /**
38
     * Find and run all init methods
39
     * - defined in relationship traits and generally to init ArrayCollection properties
40
     */
41
    protected function runInitMethods(): void
42
    {
43
        $methods = static::$reflectionClass->getMethods(\ReflectionMethod::IS_PRIVATE);
0 ignored issues
show
Bug introduced by
Since $reflectionClass is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $reflectionClass to at least protected.
Loading history...
44
        foreach ($methods as $method) {
45
            if ($method instanceof \ReflectionMethod) {
46
                $method = $method->getName();
47
            }
48
            if (0 === strpos($method, UsesPHPMetaDataInterface::METHOD_PREFIX_INIT)) {
49
                $this->$method();
50
            }
51
        }
52
    }
53
54
    /**
55
     * Loads the class and property meta data in the class
56
     *
57
     * This is the method called by Doctrine to load the meta data
58
     *
59
     * @param DoctrineClassMetaData $metadata
60
     *
61
     * @throws DoctrineStaticMetaException
62
     * @SuppressWarnings(PHPMD.StaticAccess)
63
     */
64
    public static function loadMetadata(DoctrineClassMetaData $metadata): void
65
    {
66
        try {
67
            $builder                 = new ClassMetadataBuilder($metadata);
68
            static::$reflectionClass = $metadata->getReflectionClass();
0 ignored issues
show
Bug introduced by
Since $reflectionClass is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $reflectionClass to at least protected.
Loading history...
69
            static::loadPropertyDoctrineMetaData($builder);
70
            static::loadClassDoctrineMetaData($builder);
71
            static::setChangeTrackingPolicy($builder);
72
        } catch (\Exception $e) {
73
            throw new DoctrineStaticMetaException(
74
                'Exception in '.__METHOD__.': '.$e->getMessage(),
75
                $e->getCode(),
76
                $e
77
            );
78
        }
79
    }
80
81
    /**
82
     * Setthing the change policy to be deferred explicit for the moment. Should consider if this needs to be
83
     * configurable in the future
84
     *
85
     * @see http://doctrine-orm.readthedocs.io/en/latest/reference/change-tracking-policies.html
86
     *
87
     * @param ClassMetadataBuilder $builder
88
     */
89
    public static function setChangeTrackingPolicy(ClassMetadataBuilder $builder)
90
    {
91
        $builder->setChangeTrackingPolicyDeferredExplicit();
92
    }
93
94
    /**
95
     * This method will reflect on the entity class and pull out all the methods that begin with
96
     * UsesPHPMetaDataInterface::METHOD_PREFIX_GET_PROPERTY_DOCTRINE_META
97
     *
98
     * Once it has an array of methods, it calls them all, passing in the $builder
99
     *
100
     * @param ClassMetadataBuilder $builder
101
     *
102
     * @throws DoctrineStaticMetaException
103
     * @SuppressWarnings(PHPMD.StaticAccess)
104
     */
105
    protected static function loadPropertyDoctrineMetaData(ClassMetadataBuilder $builder): void
106
    {
107
        $methodName = '__no_method__';
108
        try {
109
            $staticMethods = static::getStaticMethods();
110
            //now loop through and call them
111
            foreach ($staticMethods as $method) {
112
                $methodName = $method->getName();
113
                if (0 === stripos($methodName, UsesPHPMetaDataInterface::METHOD_PREFIX_GET_PROPERTY_DOCTRINE_META)) {
114
                    static::$methodName($builder);
115
                }
116
            }
117
        } catch (\Exception $e) {
118
            throw new DoctrineStaticMetaException(
119
                'Exception in '.__METHOD__.'for '
120
                .self::$reflectionClass->getName()."::$methodName\n\n"
121
                .$e->getMessage()
122
            );
123
        }
124
    }
125
126
    /**
127
     * Get class level meta data, eg table name
128
     *
129
     * @param ClassMetadataBuilder $builder
130
     *
131
     * @SuppressWarnings(PHPMD.StaticAccess)
132
     */
133
    protected static function loadClassDoctrineMetaData(ClassMetadataBuilder $builder): void
134
    {
135
        $tableName = MappingHelper::getTableNameForEntityFqn(static::class, self::$reflectionClass);
0 ignored issues
show
Unused Code introduced by
The call to EdmondsCommerce\Doctrine...TableNameForEntityFqn() has too many arguments starting with self::reflectionClass. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
        /** @scrutinizer ignore-call */ 
136
        $tableName = MappingHelper::getTableNameForEntityFqn(static::class, self::$reflectionClass);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
136
        $builder->setTable($tableName);
137
    }
138
139
    /**
140
     * Get an array of all static methods implemented by the current class
141
     *
142
     * Merges trait methods
143
     * Filters out this trait
144
     *
145
     * @return array|\ReflectionMethod[]
146
     * @throws \ReflectionException
147
     */
148
    protected static function getStaticMethods(): array
149
    {
150
        $currentClass = static::class;
151
        // get class level static methods
152
        if (!static::$reflectionClass instanceof \ReflectionClass
0 ignored issues
show
Bug introduced by
Since $reflectionClass is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $reflectionClass to at least protected.
Loading history...
introduced by
static::reflectionClass is always a sub-type of ReflectionClass.
Loading history...
153
            || static::$reflectionClass->getName() !== $currentClass
154
        ) {
155
            static::$reflectionClass = new \ReflectionClass($currentClass);
156
        }
157
        $staticMethods = static::$reflectionClass->getMethods(
158
            \ReflectionMethod::IS_STATIC
159
        );
160
        // get static methods from traits
161
        $traits = self::$reflectionClass->getTraits();
162
        foreach ($traits as $trait) {
163
            if ($trait->getShortName() === 'UsesPHPMetaData') {
164
                continue;
165
            }
166
            $traitStaticMethods = $trait->getMethods(
167
                \ReflectionMethod::IS_STATIC
168
            );
169
            array_merge(
170
                $staticMethods,
171
                $traitStaticMethods
172
            );
173
        }
174
175
        return $staticMethods;
176
    }
177
178
179
180
    /**
181
     * Get the property name the Entity is mapped by when plural
182
     *
183
     * Override it in your entity class if you are using an Entity class name that doesn't pluralize nicely
184
     *
185
     * @return string
186
     * @throws DoctrineStaticMetaException
187
     * @SuppressWarnings(PHPMD.StaticAccess)
188
     */
189
    public static function getPlural(): string
190
    {
191
        try {
192
            if (null === static::$plural) {
0 ignored issues
show
introduced by
The condition null === static::plural is always false.
Loading history...
Bug introduced by
Since $plural is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $plural to at least protected.
Loading history...
193
                $singular       = static::getSingular();
194
                static::$plural = Inflector::pluralize($singular);
195
            }
196
197
            return static::$plural;
198
        } catch (\Exception $e) {
199
            throw new DoctrineStaticMetaException('Exception in '.__METHOD__.': '.$e->getMessage(), $e->getCode(), $e);
200
        }
201
    }
202
203
    /**
204
     * Get the property the name the Entity is mapped by when singular
205
     *
206
     * @return string
207
     * @throws DoctrineStaticMetaException
208
     * @SuppressWarnings(PHPMD.StaticAccess)
209
     */
210
    public static function getSingular(): string
211
    {
212
        try {
213
            if (null === static::$singular) {
0 ignored issues
show
introduced by
The condition null === static::singular is always false.
Loading history...
Bug introduced by
Since $singular is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $singular to at least protected.
Loading history...
214
                if (null === self::$reflectionClass) {
215
                    self::$reflectionClass = new \ReflectionClass(static::class);
216
                }
217
218
                $shortName         = self::$reflectionClass->getShortName();
219
                $singularShortName = Inflector::singularize($shortName);
220
221
                $namespaceName = self::$reflectionClass->getNamespaceName();
222
                $namespaceParts = \explode(AbstractGenerator::ENTITIES_FOLDER_NAME, $namespaceName);
223
                $entityNamespace = \array_pop($namespaceParts);
224
225
                $namespacedShortName = \preg_replace(
226
                    '/\\\\/',
227
                    '',
228
                    $entityNamespace . $singularShortName
229
                );
230
231
                static::$singular = \lcfirst($namespacedShortName);
232
            }
233
234
            return static::$singular;
235
        } catch (\Exception $e) {
236
            throw new DoctrineStaticMetaException('Exception in '.__METHOD__.': '.$e->getMessage(), $e->getCode(), $e);
237
        }
238
    }
239
240
    /**
241
     * Which field is being used for ID - will normally be `id` as implemented by
242
     * \EdmondsCommerce\DoctrineStaticMeta\Fields\Traits\IdField
243
     *
244
     * @return string
245
     * @SuppressWarnings(PHPMD.StaticAccess)
246
     */
247
    public static function getIdField(): string
248
    {
249
        return 'id';
250
    }
251
}
252