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.
Passed
Pull Request — master (#106)
by Ross
18:39 queued 15:49
created

AbstractEntityRepository::getOneBy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 0
cts 5
cp 0
cc 2
nc 2
nop 2
crap 6
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories;
4
5
use Doctrine\Common\Collections\Criteria;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Doctrine\ORM\EntityRepository;
8
use Doctrine\ORM\LazyCriteriaCollection;
9
use Doctrine\ORM\Mapping\ClassMetadata;
10
use Doctrine\ORM\NativeQuery;
11
use Doctrine\ORM\Query;
12
use Doctrine\ORM\QueryBuilder;
13
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
14
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
15
16
/**
17
 * Class AbstractEntityRepository
18
 *
19
 * This provides a base class that handles instantiating the correctly configured EntityRepository and provides an
20
 * extensible baseline for further customisation
21
 *
22
 * We have extracted an interface from the standard Doctrine EntityRepository and implemented that
23
 * However, so we can add type safety, we can't "actually" implement it
24
 *
25
 * We have also deliberately left out the magic calls. Please make real methods in your concrete repository class
26
 *
27
 * Note, there are quite a few PHPMD warnings, however it needs to respect the legacy interface so they are being
28
 * suppressed
29
 *
30
 * @package EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories
31
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
32
 * @SuppressWarnings(PHPMD.ExcessivePublicCount)
33
 * @SuppressWarnings(PHPMD.NumberOfChildren)
34
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
35
 */
36
abstract class AbstractEntityRepository implements EntityRepositoryInterface
37
{
38
    /**
39
     * @var EntityManagerInterface
40
     */
41
    protected $entityManager;
42
    /**
43
     * @var EntityRepository
44
     */
45
    protected $entityRepository;
46
    /**
47
     * @var string
48
     */
49
    protected $repositoryFactoryFqn;
50
    /**
51
     * @var ClassMetadata|null
52
     */
53
    protected $metaData;
54
    /**
55
     * @var NamespaceHelper
56
     */
57
    protected $namespaceHelper;
58
59
    /**
60
     * AbstractEntityRepositoryFactory constructor.
61
     *
62
     * @param EntityManagerInterface $entityManager
63
     * @param ClassMetadata|null     $metaData
64
     * @param NamespaceHelper|null   $namespaceHelper
65
     */
66 3
    public function __construct(
67
        EntityManagerInterface $entityManager,
68
        ?ClassMetadata $metaData = null,
69
        ?NamespaceHelper $namespaceHelper = null
70
    ) {
71 3
        $this->entityManager   = $entityManager;
72 3
        $this->metaData        = $metaData;
73 3
        $this->namespaceHelper = ($namespaceHelper ?? new NamespaceHelper());
74 3
        $this->initRepository();
75 3
    }
76
77 1
    protected function initRepository(): void
78
    {
79 1
        if (null === $this->metaData) {
80 1
            $entityFqn      = $this->getEntityFqn();
81 1
            $this->metaData = $this->entityManager->getClassMetadata($entityFqn);
82
        }
83
84 1
        $this->entityRepository = new EntityRepository($this->entityManager, $this->metaData);
85 1
    }
86
87 1
    protected function getEntityFqn(): string
88
    {
89 1
        return '\\' . \str_replace(
90
            [
91 1
                    'Entity\\Repositories',
92
                ],
93
            [
94 1
                    'Entities',
95
                ],
96 1
            $this->namespaceHelper->cropSuffix(static::class, 'Repository')
97
        );
98
    }
99
100
    /**
101
     * @param mixed    $id
102
     * @param int|null $lockMode
103
     * @param int|null $lockVersion
104
     *
105
     * @return EntityInterface|null
106
     */
107 1
    public function find($id, ?int $lockMode = null, ?int $lockVersion = null)
108
    {
109 1
        $entity = $this->entityRepository->find($id, $lockMode, $lockVersion);
110 1
        if (null === $entity || $entity instanceof EntityInterface) {
111 1
            return $entity;
112
        }
113
    }
114
115
    /**
116
     * @return array|EntityInterface[]
117
     */
118 1
    public function findAll(): array
119
    {
120 1
        return $this->entityRepository->findAll();
121
    }
122
123
    /**
124
     * @return array|EntityInterface[]
125
     */
126 1
    public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
127
    {
128 1
        return $this->entityRepository->findBy($criteria, $orderBy, $limit, $offset);
129
    }
130
131
    /**
132
     * @param array      $criteria
133
     * @param array|null $orderBy
134
     *
135
     * @return EntityInterface|null
136
     */
137 1
    public function findOneBy(array $criteria, ?array $orderBy = null)
138
    {
139 1
        $entity = $this->entityRepository->findOneBy($criteria, $orderBy);
140 1
        if (null === $entity || $entity instanceof EntityInterface) {
141 1
            return $entity;
142
        }
143
    }
144
145
    /**
146
     * @param mixed    $id
147
     * @param int|null $lockMode
148
     * @param int|null $lockVersion
149
     *
150
     * @return EntityInterface
151
     */
152
    public function get($id, ?int $lockMode = null, ?int $lockVersion = null)
153
    {
154
        $entity = $this->find($id, $lockMode, $lockVersion);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $entity is correct as $this->find($id, $lockMode, $lockVersion) targeting EdmondsCommerce\Doctrine...ntityRepository::find() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
155
        if ($entity === null) {
0 ignored issues
show
introduced by
The condition $entity === null is always true.
Loading history...
156
            throw new \RuntimeException('Could not find the entity');
157
        }
158
159
        return $entity;
160
    }
161
162
    /**
163
     * @param array      $criteria
164
     * @param array|null $orderBy
165
     *
166
     * @return EntityInterface
167
     */
168
    public function getOneBy(array $criteria, ?array $orderBy = null)
169
    {
170
        $result = $this->findOneBy($criteria, $orderBy);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->findOneBy($criteria, $orderBy) targeting EdmondsCommerce\Doctrine...Repository::findOneBy() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
171
        if ($result === null) {
0 ignored issues
show
introduced by
The condition $result === null is always true.
Loading history...
172
            throw new \RuntimeException('Could not find the entity');
173
        }
174
175
        return $result;
176
    }
177
178 1
    public function getClassName(): string
179
    {
180 1
        return $this->entityRepository->getClassName();
181
    }
182
183 1
    public function matching(Criteria $criteria): LazyCriteriaCollection
184
    {
185 1
        $collection = $this->entityRepository->matching($criteria);
186 1
        if ($collection instanceof LazyCriteriaCollection) {
0 ignored issues
show
introduced by
$collection is always a sub-type of Doctrine\ORM\LazyCriteriaCollection.
Loading history...
187 1
            return $collection;
188
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 186 is false. This is incompatible with the type-hinted return Doctrine\ORM\LazyCriteriaCollection. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
189
    }
190
191 1
    public function createQueryBuilder(string $alias, string $indexBy = null): QueryBuilder
192
    {
193 1
        return $this->entityRepository->createQueryBuilder($alias, $indexBy);
194
    }
195
196 1
    public function createResultSetMappingBuilder(string $alias): Query\ResultSetMappingBuilder
197
    {
198 1
        return $this->entityRepository->createResultSetMappingBuilder($alias);
199
    }
200
201
    public function createNamedQuery(string $queryName): Query
202
    {
203
        return $this->entityRepository->createNamedQuery($queryName);
204
    }
205
206
    public function createNativeNamedQuery(string $queryName): NativeQuery
207
    {
208
        return $this->entityRepository->createNativeNamedQuery($queryName);
209
    }
210
211 1
    public function clear(): void
212
    {
213 1
        $this->entityRepository->clear();
214 1
    }
215
216 1
    public function count(array $criteria): int
217
    {
218 1
        return $this->entityRepository->count($criteria);
219
    }
220
}
221