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.
Test Failed
Pull Request — master (#30)
by Ross
08:59
created

loadPropertyDoctrineMetaData()   A

Complexity

Conditions 4
Paths 9

Size

Total Lines 20
Code Lines 13

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
c 0
b 0
f 0
rs 9.2
cc 4
eloc 13
nc 9
nop 1
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; consider using self, or increasing the visibility of $reflectionClass to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
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; consider using self, or increasing the visibility of $reflectionClass to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
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 View Code Duplication
    protected static function loadPropertyDoctrineMetaData(ClassMetadataBuilder $builder): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 MappingHelper::getTableNameForEntityFqn() has too many arguments starting with 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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

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; consider using self, or increasing the visibility of $reflectionClass to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
153
            || static::$reflectionClass->getName() !== $currentClass
0 ignored issues
show
Bug introduced by
Since $reflectionClass is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $reflectionClass to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
154
        ) {
155
            static::$reflectionClass = new \ReflectionClass($currentClass);
0 ignored issues
show
Bug introduced by
Since $reflectionClass is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $reflectionClass to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
156
        }
157
        $staticMethods = static::$reflectionClass->getMethods(
0 ignored issues
show
Bug introduced by
Since $reflectionClass is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $reflectionClass to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
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
Bug introduced by
Since $plural is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $plural to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
193
                $singular       = static::getSingular();
194
                static::$plural = Inflector::pluralize($singular);
0 ignored issues
show
Bug introduced by
Since $plural is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $plural to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
195
            }
196
197
            return static::$plural;
0 ignored issues
show
Bug introduced by
Since $plural is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $plural to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
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
Bug introduced by
Since $singular is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $singular to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
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);
0 ignored issues
show
Bug introduced by
Since $singular is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $singular to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
232
            }
233
234
            return static::$singular;
0 ignored issues
show
Bug introduced by
Since $singular is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $singular to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
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