Issues (33)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/FinderAggregateRepository.php (10 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace T4webInfrastructure;
4
5
use ArrayObject;
6
use Zend\Db\TableGateway\TableGateway;
7
use Zend\Db\Sql\Select;
8
use T4webDomainInterface\Infrastructure\RepositoryInterface;
9
use T4webDomainInterface\Infrastructure\CriteriaInterface;
10
use T4webDomainInterface\EntityInterface;
11
use T4webDomainInterface\EntityFactoryInterface;
12
13
class FinderAggregateRepository implements RepositoryInterface
14
{
15
    /**
16
     * @var TableGateway
17
     */
18
    private $tableGateway;
19
20
    /**
21
     * @var Mapper
22
     */
23
    private $mapper;
24
25
    /**
26
     * @var EntityFactoryInterface
27
     */
28
    private $entityFactory;
29
30
    /**
31
     * @var RepositoryInterface
32
     */
33
    private $entityRepository;
34
35
    /**
36
     * @var RepositoryInterface[]
37
     */
38
    private $relatedRepository;
39
40
    /**
41
     * @var array
42
     */
43
    private $relationsConfig;
44
45
    /**
46
     * @var ArrayObject[]
47
     */
48
    private $with;
49
50
    /**
51
     * FinderAggregateRepository constructor.
52
     * @param TableGateway $tableGateway
53
     * @param Mapper $mapper
54
     * @param EntityFactoryInterface $entityFactory
55
     * @param RepositoryInterface $entityRepository
56
     * @param RepositoryInterface[] $relatedRepository
57
     * @param array $relationsConfig
58
     */
59
    public function __construct(
60
        TableGateway $tableGateway,
61
        Mapper $mapper,
62
        EntityFactoryInterface $entityFactory,
63
        RepositoryInterface $entityRepository,
64
        array $relatedRepository,
65
        array $relationsConfig)
66
    {
67
        $this->tableGateway = $tableGateway;
68
        $this->mapper = $mapper;
69
        $this->entityFactory = $entityFactory;
70
        $this->entityRepository = $entityRepository;
71
        $this->relatedRepository = $relatedRepository;
72
        $this->relationsConfig = $relationsConfig;
73
    }
74
75
    /**
76
     * @param string $entityName
77
     * @return $this
78
     */
79
    public function with($entityName)
80
    {
81
        if (!isset($this->relatedRepository[$entityName])) {
82
            throw new \RuntimeException(get_class($this) . ": related $entityName repository not exists");
83
        }
84
85
        $this->with[$entityName] = [];
86
87
        return $this;
88
    }
89
90
    /**
91
     * @param CriteriaInterface|array $criteria
92
     * @return EntityInterface|null
93
     */
94
    public function find($criteria)
95
    {
96
        if (is_array($criteria)) {
97
            $criteria = $this->createCriteria($criteria);
98
        }
99
100
        if (empty($this->with)) {
101
            return $this->entityRepository->find($criteria);
102
        }
103
104
        /** @var Select $select */
105
        $select = $criteria->getQuery();
106
107
        $select->limit(1)->offset(0);
108
        $result = $this->tableGateway->selectWith($select)->toArray();
109
110
        if (!isset($result[0])) {
111
            return;
112
        }
113
114
        $row = $result[0];
115
116
        $relatedEntityIds = [];
117 View Code Duplication
        foreach ($this->with as $relatedEntityName => $cascadeWith) {
0 ignored issues
show
This code seems to be duplicated across 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...
118
            $relatedField = $this->getRelatedField($relatedEntityName);
119
120
            if (!array_key_exists($relatedField, $row)) {
121
                throw new \RuntimeException(get_class($this) . ": relation field $relatedEntityName not fetched");
122
            }
123
124
            if (!isset($relatedEntityIds[$relatedEntityName])) {
125
                $relatedEntityIds[$relatedEntityName] = new ArrayObject();
126
            }
127
128
            if (!in_array($row[$relatedField], (array)$relatedEntityIds[$relatedEntityName])) {
129
                $relatedEntityIds[$relatedEntityName]->append($row[$relatedField]);
130
            }
131
        }
132
133
        $relatedEntities = [];
134 View Code Duplication
        foreach ($this->with as $relatedEntityName => $cascadeWith) {
0 ignored issues
show
This code seems to be duplicated across 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...
135
            if (empty((array)$relatedEntityIds[$relatedEntityName])) {
136
                continue;
137
            }
138
            
139
            $criteria = $this->relatedRepository[$relatedEntityName]->createCriteria(['id.in' => (array)$relatedEntityIds[$relatedEntityName]]);
140
            $relatedEntities[$relatedEntityName] = $this->relatedRepository[$relatedEntityName]->findMany($criteria);
141
        }
142
143
        $relatedField = $this->getRelatedField($relatedEntityName);
0 ignored issues
show
The variable $relatedEntityName does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
144
145
        $entityArgs = [
146
            'data' => $this->mapper->fromTableRow($row)
147
        ];
148
149 View Code Duplication
        foreach ($this->relationsConfig as $entityName => $joinRule) {
0 ignored issues
show
This code seems to be duplicated across 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...
150
            if (isset($relatedEntities[$entityName])) {
151
                if (isset($relatedEntities[$entityName][$row[$relatedField]])) {
152
                    $entityArgs['aggregateItems'][] = $relatedEntities[$entityName][$row[$relatedField]];
153
                } else {
154
                    $entityArgs['aggregateItems'][] = null;
155
                }
156
            } else {
157
                $entityArgs['aggregateItems'][] = null;
158
            }
159
        }
160
161
        $entity = $this->entityFactory->create($entityArgs);
162
163
        $this->with = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array<integer,object<ArrayObject>> of property $with.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
164
165
        return $entity;
166
    }
167
168
    /**
169
     * @param CriteriaInterface|array $criteria
170
     * @return EntityInterface[]
171
     */
172
    public function findMany($criteria)
173
    {
174
        if (is_array($criteria)) {
175
            $criteria = $this->createCriteria($criteria);
176
        }
177
178
        if (empty($this->with)) {
179
            return $this->entityRepository->findMany($criteria);
180
        }
181
182
        /** @var Select $select */
183
        $select = $criteria->getQuery();
184
185
        $rows = $this->tableGateway->selectWith($select)->toArray();
186
        
187
        if (empty($rows)) {
188
            return [];
189
        }
190
191
        $relatedEntityIds = [];
192 View Code Duplication
        foreach ($rows as $row) {
0 ignored issues
show
This code seems to be duplicated across 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...
193
            foreach ($this->with as $relatedEntityName => $cascadeWith) {
194
                $relatedField = $this->getRelatedField($relatedEntityName);
195
196
                if (!array_key_exists($relatedField, $row)) {
197
                    throw new \RuntimeException(get_class($this) . ": relation field $relatedEntityName not fetched");
198
                }
199
200
                if (!isset($relatedEntityIds[$relatedEntityName])) {
201
                    $relatedEntityIds[$relatedEntityName] = new ArrayObject();
202
                }
203
204
                if (!in_array($row[$relatedField], (array)$relatedEntityIds[$relatedEntityName])) {
205
                    $relatedEntityIds[$relatedEntityName]->append($row[$relatedField]);
206
                }
207
            }
208
        }
209
210
        $relatedEntities = [];
211 View Code Duplication
        foreach ($this->with as $relatedEntityName => $cascadeWith) {
0 ignored issues
show
This code seems to be duplicated across 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...
212
            if (empty((array)$relatedEntityIds[$relatedEntityName])) {
213
                continue;
214
            }
215
            
216
            $criteria = $this->relatedRepository[$relatedEntityName]->createCriteria(['id.in' => (array)$relatedEntityIds[$relatedEntityName]]);
217
            $relatedEntities[$relatedEntityName] = $this->relatedRepository[$relatedEntityName]->findMany($criteria);
218
        }
219
220
        $entitiesArgs = [];
221
        foreach ($rows as &$row) {
222
            $entityArgs = [
223
                'data' => $this->mapper->fromTableRow($row)
224
            ];
225
226 View Code Duplication
            foreach ($this->relationsConfig as $entityName => $joinRule) {
0 ignored issues
show
This code seems to be duplicated across 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...
227
                $relatedField = $this->getRelatedField($entityName);
228
                
229
                if (isset($relatedEntities[$entityName])) {
230
                    if (isset($relatedEntities[$entityName][$row[$relatedField]])) {
231
                        $entityArgs['aggregateItems'][] = $relatedEntities[$entityName][$row[$relatedField]];
232
                    } else {
233
                        $entityArgs['aggregateItems'][] = null;
234
                    }
235
                } else {
236
                    $entityArgs['aggregateItems'][] = null;
237
                }
238
            }
239
240
            $entitiesArgs[] = $entityArgs;
241
        }
242
243
        $entities = $this->entityFactory->createCollection($entitiesArgs);
244
245
        $this->with = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array<integer,object<ArrayObject>> of property $with.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
246
247
        return $entities;
248
    }
249
250
    /**
251
     * @param mixed $id
252
     * @return EntityInterface|null
253
     */
254
    public function findById($id)
255
    {
256
        $criteria = $this->createCriteria();
257
        $criteria->equalTo('id', $id);
258
259
        return $this->find($criteria);
260
    }
261
262
    private function getRelatedField($entityName)
263
    {
264
        if (!isset($this->relationsConfig[$entityName])) {
265
            throw new \RuntimeException(get_class($this) . ": relation $entityName not exists");
266
        }
267
268
        list($table, $field) = explode('.', $this->relationsConfig[$entityName][0]);
0 ignored issues
show
The assignment to $table is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
269
270
        return $field;
271
    }
272
273
    /**
274
     * @param mixed $criteria
275
     * @return int
276
     */
277
    public function count($criteria)
278
    {
279
        return $this->entityRepository->count($criteria);
280
    }
281
282
    /**
283
     * @param array $filter
284
     * @return CriteriaInterface
285
     */
286
    public function createCriteria(array $filter = [])
287
    {
288
        return $this->entityRepository->createCriteria($filter);
289
    }
290
291
    /**
292
     * @param EntityInterface $entity
293
     * @return EntityInterface|int|null
294
     */
295
    public function add(EntityInterface $entity)
296
    {
297
        throw new \RuntimeException(get_class($this) . ' cannot adding');
298
    }
299
300
    /**
301
     * @param EntityInterface $entity
302
     * @return void
303
     */
304
    public function remove(EntityInterface $entity)
305
    {
306
        throw new \RuntimeException(get_class($this) . ' cannot removing');
307
    }
308
}
309