This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Psi\Bridge\ObjectAgent\Doctrine\Collections; |
||
6 | |||
7 | use Doctrine\Common\Collections\ArrayCollection; |
||
8 | use Doctrine\Common\Collections\Criteria; |
||
9 | use Psi\Component\ObjectAgent\AgentInterface; |
||
10 | use Psi\Component\ObjectAgent\Capabilities; |
||
11 | use Psi\Component\ObjectAgent\Exception\BadMethodCallException; |
||
12 | use Psi\Component\ObjectAgent\Exception\ObjectNotFoundException; |
||
13 | use Psi\Component\ObjectAgent\Query\Comparison; |
||
14 | use Psi\Component\ObjectAgent\Query\Query; |
||
15 | |||
16 | class CollectionsAgent implements AgentInterface |
||
17 | { |
||
18 | private $store; |
||
19 | |||
20 | public function __construct(Store $store) |
||
21 | { |
||
22 | $this->store = $store; |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | public function supports(string $classFqn): bool |
||
29 | { |
||
30 | return $this->store->hasCollection($classFqn); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * {@inheritdoc} |
||
35 | */ |
||
36 | public function getCapabilities(): Capabilities |
||
37 | { |
||
38 | return Capabilities::create([ |
||
39 | 'can_query_join' => false, |
||
40 | 'can_set_parent' => false, |
||
41 | 'can_query_count' => true, |
||
42 | 'supported_comparators' => [ |
||
43 | Comparison::EQUALS, |
||
44 | Comparison::NOT_EQUALS, |
||
45 | Comparison::LESS_THAN, |
||
46 | Comparison::LESS_THAN_EQUAL, |
||
47 | Comparison::GREATER_THAN, |
||
48 | Comparison::GREATER_THAN_EQUAL, |
||
49 | Comparison::IN, |
||
50 | Comparison::NOT_IN, |
||
51 | Comparison::CONTAINS, |
||
52 | Comparison::NULL, |
||
53 | ], |
||
54 | ]); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | View Code Duplication | public function find($identifier, string $classFqn = null) |
|
0 ignored issues
–
show
|
|||
61 | { |
||
62 | if (null === $classFqn) { |
||
63 | throw BadMethodCallException::classArgumentIsMandatory(__CLASS__); |
||
64 | } |
||
65 | |||
66 | $object = $this->store->find($classFqn, $identifier); |
||
67 | |||
68 | if (null === $object) { |
||
69 | throw ObjectNotFoundException::forClassAndIdentifier($classFqn, $identifier); |
||
70 | } |
||
71 | |||
72 | return $object; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | public function findMany(array $identifiers, string $classFqn = null) |
||
79 | { |
||
80 | $collection = []; |
||
81 | foreach ($identifiers as $identifier) { |
||
82 | $collection[] = $this->find($identifier, $classFqn); |
||
83 | } |
||
84 | |||
85 | return new ArrayCollection($collection); |
||
0 ignored issues
–
show
The return type of
return new \Doctrine\Com...ollection($collection); (Doctrine\Common\Collections\ArrayCollection ) is incompatible with the return type declared by the interface Psi\Component\ObjectAgent\AgentInterface::findMany of type object[] .
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design. Let’s take a look at an example: class Author {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
abstract class Post {
public function getAuthor() {
return 'Johannes';
}
}
class BlogPost extends Post {
public function getAuthor() {
return new Author('Johannes');
}
}
class ForumPost extends Post { /* ... */ }
function my_function(Post $post) {
echo strtoupper($post->getAuthor());
}
Our function ![]() |
|||
86 | } |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | public function persist($object) |
||
92 | { |
||
93 | // do nothing ... |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | public function flush() |
||
100 | { |
||
101 | // do nothing ... |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function remove($object) |
||
108 | { |
||
109 | $this->store->remove($object); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | public function query(Query $query): \Traversable |
||
116 | { |
||
117 | return $this->doQuery( |
||
118 | $query, |
||
119 | $query->getFirstResult(), |
||
120 | $query->getMaxResults() |
||
121 | ); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | public function queryCount(Query $query): int |
||
128 | { |
||
129 | return count($this->doQuery($query, 0)); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | public function getIdentifier($object) |
||
136 | { |
||
137 | foreach ($this->store->getCollection(get_class($object)) as $identifier => $element) { |
||
138 | if ($element === $object) { |
||
139 | return $identifier; |
||
140 | } |
||
141 | } |
||
142 | |||
143 | throw new \RuntimeException(sprintf( |
||
144 | 'Could not find identifier for object of class "%s"', |
||
145 | get_class($object) |
||
146 | )); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | public function setParent($object, $parent) |
||
153 | { |
||
154 | throw BadMethodCallException::setParentNotSupported(__CLASS__); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | public function getCanonicalClassFqn(string $classFqn): string |
||
161 | { |
||
162 | return $classFqn; |
||
163 | } |
||
164 | |||
165 | private function doQuery(Query $query, int $firstResult = null, int $maxResults = null) |
||
166 | { |
||
167 | $collection = $this->store->getCollection($query->getClassFqn()); |
||
168 | |||
169 | $doctrineExpression = null; |
||
170 | if ($query->hasExpression()) { |
||
171 | $expressionBuilder = Criteria::expr(); |
||
172 | $visitor = new CollectionsVisitor($expressionBuilder); |
||
173 | $doctrineExpression = $visitor->dispatch($query->getExpression()); |
||
174 | } |
||
175 | |||
176 | $criteria = new Criteria( |
||
177 | $doctrineExpression, |
||
178 | $query->getOrderings(), |
||
179 | $firstResult, |
||
180 | $maxResults |
||
181 | ); |
||
182 | |||
183 | return $collection->matching($criteria); |
||
184 | } |
||
185 | } |
||
186 |
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.