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 (#96)
by Ross
15:04 queued 12:23
created

AbstractEntityRepository::findBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories;
4
5
use Doctrine\Common\Cache\ArrayCache;
6
use Doctrine\Common\Collections\Criteria;
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Doctrine\ORM\EntityRepository;
10
use Doctrine\ORM\LazyCriteriaCollection;
11
use Doctrine\ORM\Mapping\ClassMetadata;
12
use Doctrine\ORM\NativeQuery;
13
use Doctrine\ORM\Query;
14
use Doctrine\ORM\QueryBuilder;
15
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
16
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
17
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityValidatorFactory;
18
use Symfony\Component\Validator\Mapping\Cache\DoctrineCache;
19
20
/**
21
 * Class AbstractEntityRepository
22
 *
23
 * This provides a base class that handles instantiating the correctly configured EntityRepository and provides an
24
 * extensible baseline for further customisation
25
 *
26
 * We have extracted an interface from the standard Doctrine EntityRepository and implemented that
27
 * However, so we can add type safety, we can't "actually" implement it
28
 *
29
 * We have also deliberately left out the magic calls. Please make real methods in your concrete repository class
30
 *
31
 * Note, there are quite a few PHPMD warnings, however it needs to respect the legacy interface so they are being
32
 * suppressed
33
 *
34
 * @package EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories
35
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
36
 * @SuppressWarnings(PHPMD.ExcessivePublicCount)
37
 * @SuppressWarnings(PHPMD.NumberOfChildren)
38
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
39
 */
40
abstract class AbstractEntityRepository implements EntityRepositoryInterface
41
{
42
    /**
43
     * @var EntityValidatorFactory
44
     */
45
    private static $entityValidatorFactory;
46
    /**
47
     * @var EntityManager
48
     */
49
    protected $entityManager;
50
    /**
51
     * @var EntityRepository
52
     */
53
    protected $entityRepository;
54
    /**
55
     * @var string
56
     */
57
    protected $repositoryFactoryFqn;
58
    /**
59
     * @var ClassMetadata|null
60
     */
61
    protected $metaData;
62
    /**
63
     * @var NamespaceHelper
64
     */
65
    protected $namespaceHelper;
66
67
    /**
68
     * AbstractEntityRepositoryFactory constructor.
69
     *
70
     * @param EntityManager        $entityManager
71
     * @param ClassMetadata|null   $metaData
72
     * @param NamespaceHelper|null $namespaceHelper
73 43
     */
74
    public function __construct(
75
        EntityManagerInterface $entityManager,
76
        ?ClassMetadata $metaData = null,
77
        ?NamespaceHelper $namespaceHelper = null
78 43
    ) {
79 43
        $this->entityManager   = $entityManager;
0 ignored issues
show
Documentation Bug introduced by
$entityManager is of type Doctrine\ORM\EntityManagerInterface, but the property $entityManager was declared to be of type Doctrine\ORM\EntityManager. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
80 43
        $this->metaData        = $metaData;
81 43
        $this->namespaceHelper = ($namespaceHelper ?? new NamespaceHelper());
82 43
        $this->initRepository();
83
    }
84 43
85
    protected function initRepository(): void
86 43
    {
87 1
        if (null === $this->metaData) {
88 1
            $entityFqn      = $this->getEntityFqn();
89
            $this->metaData = $this->entityManager->getClassMetadata($entityFqn);
90
        }
91 43
92 43
        $this->entityRepository = new EntityRepository($this->entityManager, $this->metaData);
93
    }
94 1
95
    protected function getEntityFqn(): string
96 1
    {
97
        return '\\' . \str_replace(
98 1
            [
99
                    'Entity\\Repositories',
100
                ],
101 1
            [
102
                    'Entities',
103 1
                ],
104
            $this->namespaceHelper->cropSuffix(static::class, 'Repository')
105
        );
106
    }
107 1
108
    public function find($id, ?int $lockMode = null, ?int $lockVersion = null): ?EntityInterface
109 1
    {
110 1
        $entity = $this->entityRepository->find($id, $lockMode, $lockVersion);
111 1
        if (null === $entity || $entity instanceof EntityInterface) {
112
            return $this->injectValidatorIfNotNull($entity);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->injectValidatorIfNotNull($entity) targeting EdmondsCommerce\Doctrine...ectValidatorIfNotNull() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
113
        }
114
        throw new \TypeError('Returned result is neither null nor an instance of EntityInterface');
115
    }
116 36
117
    private function injectValidatorIfNotNull(?EntityInterface $entity): ?EntityInterface
118 36
    {
119 36
        if (null !== $entity) {
120
            $entity->injectValidator(self::getEntityValidatorFactory()->getEntityValidator());
121
        }
122 36
123
        return $entity;
124
    }
125 36
126
    private static function getEntityValidatorFactory(): EntityValidatorFactory
127 36
    {
128
        if (null === self::$entityValidatorFactory) {
129
            /**
130
             * Can't use DI because Doctrine uses it's own factory method for repositories
131 1
             */
132 1
            self::$entityValidatorFactory = new EntityValidatorFactory(
133 1
                new DoctrineCache(
134
                    new ArrayCache()
135
                )
136
            );
137
        }
138 36
139
        return self::$entityValidatorFactory;
140
    }
141
142
    /**
143
     * @return array|EntityInterface[]
144 32
     */
145
    public function findAll(): array
146 32
    {
147
        $collection = $this->entityRepository->findAll();
148 32
149
        return $this->injectValidatorToCollection($collection);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->injectVali...Collection($collection) returns the type iterable which is incompatible with the type-hinted return array.
Loading history...
150
    }
151 34
152
    private function injectValidatorToCollection(iterable $collection)
153 34
    {
154 34
        foreach ($collection as $entity) {
155
            $this->injectValidatorIfNotNull($entity);
156
        }
157 34
158
        return $collection;
159
    }
160
161
    /**
162
     * @return array|EntityInterface[]
163 1
     */
164
    public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
165 1
    {
166
        $collection = $this->entityRepository->findBy($criteria, $orderBy, $limit, $offset);
167 1
168
        return $this->injectValidatorToCollection($collection);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->injectVali...Collection($collection) returns the type iterable which is incompatible with the type-hinted return array.
Loading history...
169
    }
170 1
171
    public function findOneBy(array $criteria, ?array $orderBy = null): ?EntityInterface
172 1
    {
173 1
        $entity = $this->entityRepository->findOneBy($criteria, $orderBy);
174 1
        if (null === $entity || $entity instanceof EntityInterface) {
175
            return $this->injectValidatorIfNotNull($entity);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->injectValidatorIfNotNull($entity) targeting EdmondsCommerce\Doctrine...ectValidatorIfNotNull() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
176
        }
177
        throw new \TypeError('Returned result is neither null nor an instance of EntityInterface');
178
    }
179 2
180
    public function getClassName(): string
181 2
    {
182
        return $this->entityRepository->getClassName();
183
    }
184 1
185
    public function matching(Criteria $criteria): LazyCriteriaCollection
186 1
    {
187 1
        $collection = $this->entityRepository->matching($criteria);
188 1
        if ($collection instanceof LazyCriteriaCollection) {
0 ignored issues
show
introduced by
$collection is always a sub-type of Doctrine\ORM\LazyCriteriaCollection.
Loading history...
189
            return $this->injectValidatorToCollection($collection);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->injectVali...Collection($collection) returns the type iterable which is incompatible with the type-hinted return Doctrine\ORM\LazyCriteriaCollection.
Loading history...
190
        }
191
        throw new \TypeError('Returned result is not an instance of LazyCriteriaCollection');
192
    }
193 1
194
    public function createQueryBuilder(string $alias, string $indexBy = null): QueryBuilder
195 1
    {
196
        return $this->entityRepository->createQueryBuilder($alias, $indexBy);
197
    }
198 1
199
    public function createResultSetMappingBuilder(string $alias): Query\ResultSetMappingBuilder
200 1
    {
201
        return $this->entityRepository->createResultSetMappingBuilder($alias);
202
    }
203
204
    public function createNamedQuery(string $queryName): Query
205
    {
206
        return $this->entityRepository->createNamedQuery($queryName);
207
    }
208
209
    public function createNativeNamedQuery(string $queryName): NativeQuery
210
    {
211
        return $this->entityRepository->createNativeNamedQuery($queryName);
212
    }
213
214
    /**
215
     *
216 1
     */
217
    public function clear(): void
218 1
    {
219 1
        $this->entityRepository->clear();
220
    }
221 1
222
    public function count(array $criteria): int
223 1
    {
224
        return $this->entityRepository->count($criteria);
225
    }
226
}
227