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 (#101)
by joseph
18:59
created

AbstractEntityRepository::createNamedQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 2
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 1
    public function find($id, ?int $lockMode = null, ?int $lockVersion = null): ?EntityInterface
101
    {
102 1
        $entity = $this->entityRepository->find($id, $lockMode, $lockVersion);
103 1
        if (null === $entity || $entity instanceof EntityInterface) {
104 1
            return $entity;
105
        }
106
    }
107
108
    /**
109
     * @return array|EntityInterface[]
110
     */
111 1
    public function findAll(): array
112
    {
113 1
        return $this->entityRepository->findAll();
114
    }
115
116
    /**
117
     * @return array|EntityInterface[]
118
     */
119 1
    public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
120
    {
121 1
        return $this->entityRepository->findBy($criteria, $orderBy, $limit, $offset);
122
    }
123
124 1
    public function findOneBy(array $criteria, ?array $orderBy = null): ?EntityInterface
125
    {
126 1
        $entity = $this->entityRepository->findOneBy($criteria, $orderBy);
127 1
        if (null === $entity || $entity instanceof EntityInterface) {
128 1
            return $entity;
129
        }
130
    }
131
132 1
    public function getClassName(): string
133
    {
134 1
        return $this->entityRepository->getClassName();
135
    }
136
137 1
    public function matching(Criteria $criteria): LazyCriteriaCollection
138
    {
139 1
        $collection = $this->entityRepository->matching($criteria);
140 1
        if ($collection instanceof LazyCriteriaCollection) {
0 ignored issues
show
introduced by
$collection is always a sub-type of Doctrine\ORM\LazyCriteriaCollection.
Loading history...
141 1
            return $collection;
142
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 140 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...
143
    }
144
145 1
    public function createQueryBuilder(string $alias, string $indexBy = null): QueryBuilder
146
    {
147 1
        return $this->entityRepository->createQueryBuilder($alias, $indexBy);
148
    }
149
150 1
    public function createResultSetMappingBuilder(string $alias): Query\ResultSetMappingBuilder
151
    {
152 1
        return $this->entityRepository->createResultSetMappingBuilder($alias);
153
    }
154
155
    public function createNamedQuery(string $queryName): Query
156
    {
157
        return $this->entityRepository->createNamedQuery($queryName);
158
    }
159
160
    public function createNativeNamedQuery(string $queryName): NativeQuery
161
    {
162
        return $this->entityRepository->createNativeNamedQuery($queryName);
163
    }
164
165 1
    public function clear(): void
166
    {
167 1
        $this->entityRepository->clear();
168 1
    }
169
170 1
    public function count(array $criteria): int
171
    {
172 1
        return $this->entityRepository->count($criteria);
173
    }
174
}
175