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 | namespace ValueObjects\Structure; |
||
4 | |||
5 | use ValueObjects\Util\Util; |
||
6 | use ValueObjects\Number\Natural; |
||
7 | use ValueObjects\StringLiteral\StringLiteral; |
||
8 | use ValueObjects\ValueObjectInterface; |
||
9 | |||
10 | class Collection implements ValueObjectInterface |
||
11 | { |
||
12 | /** @var \SplFixedArray */ |
||
13 | protected $items; |
||
14 | |||
15 | /** |
||
16 | * Returns a new Collection object |
||
17 | * |
||
18 | * @param \SplFixedArray $array |
||
0 ignored issues
–
show
|
|||
19 | * @return self |
||
20 | */ |
||
21 | 3 | public static function fromNative() |
|
22 | { |
||
23 | 3 | $array = \func_get_arg(0); |
|
24 | 3 | $items = array(); |
|
25 | |||
26 | 3 | foreach ($array as $item) { |
|
27 | 3 | if ($item instanceof \Traversable || \is_array($item)) { |
|
28 | 2 | $items[] = static::fromNative($item); |
|
29 | 2 | } else { |
|
30 | 3 | $items[] = new StringLiteral(\strval($item)); |
|
31 | } |
||
32 | 3 | } |
|
33 | |||
34 | 3 | $fixedArray = \SplFixedArray::fromArray($items); |
|
35 | |||
36 | 3 | return new static($fixedArray); |
|
37 | } |
||
38 | |||
39 | /** |
||
40 | * Returns a new Collection object |
||
41 | * |
||
42 | * @return self |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Adding a
@return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.
Adding a Please refer to the PHP core documentation on constructors. ![]() |
|||
43 | */ |
||
44 | 12 | public function __construct(\SplFixedArray $items) |
|
45 | { |
||
46 | 12 | foreach ($items as $item) { |
|
47 | 12 | if (false === $item instanceof ValueObjectInterface) { |
|
48 | 1 | $type = \is_object($item) ? \get_class($item) : \gettype($item); |
|
49 | 1 | throw new \InvalidArgumentException(\sprintf('Passed SplFixedArray object must contains "ValueObjectInterface" objects only. "%s" given.', $type)); |
|
50 | } |
||
51 | 12 | } |
|
52 | |||
53 | 12 | $this->items = $items; |
|
54 | 12 | } |
|
55 | |||
56 | /** |
||
57 | * Tells whether two Collection are equal by comparing their size and items (item order matters) |
||
58 | * |
||
59 | * @param ValueObjectInterface $collection |
||
60 | * @return bool |
||
61 | */ |
||
62 | 6 | public function sameValueAs(ValueObjectInterface $collection) |
|
63 | { |
||
64 | 6 | if (false === Util::classEquals($this, $collection) || false === $this->count()->sameValueAs($collection->count())) { |
|
0 ignored issues
–
show
It seems like you code against a concrete implementation and not the interface
ValueObjects\ValueObjectInterface as the method count() does only exist in the following implementations of said interface: ValueObjects\Structure\Collection , ValueObjects\Structure\Dictionary .
Let’s take a look at an example: interface User
{
/** @return string */
public function getPassword();
}
class MyUser implements User
{
public function getPassword()
{
// return something
}
public function getDisplayName()
{
// return some name.
}
}
class AuthSystem
{
public function authenticate(User $user)
{
$this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
// do something.
}
}
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break. Available Fixes
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
![]() |
|||
65 | 1 | return false; |
|
66 | } |
||
67 | |||
68 | 6 | $arrayCollection = $collection->toArray(); |
|
0 ignored issues
–
show
It seems like you code against a concrete implementation and not the interface
ValueObjects\ValueObjectInterface as the method toArray() does only exist in the following implementations of said interface: ValueObjects\Structure\Collection , ValueObjects\Structure\Dictionary .
Let’s take a look at an example: interface User
{
/** @return string */
public function getPassword();
}
class MyUser implements User
{
public function getPassword()
{
// return something
}
public function getDisplayName()
{
// return some name.
}
}
class AuthSystem
{
public function authenticate(User $user)
{
$this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
// do something.
}
}
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break. Available Fixes
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
![]() |
|||
69 | |||
70 | 6 | foreach ($this->items as $index => $item) { |
|
71 | 6 | if (!isset($arrayCollection[$index]) || false === $item->sameValueAs($arrayCollection[$index])) { |
|
72 | 1 | return false; |
|
73 | } |
||
74 | 6 | } |
|
75 | |||
76 | 6 | return true; |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * Returns the number of objects in the collection |
||
81 | * |
||
82 | * @return Natural |
||
83 | */ |
||
84 | 10 | public function count() |
|
85 | { |
||
86 | 10 | return new Natural($this->items->count()); |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Tells whether the Collection contains an object |
||
91 | * |
||
92 | * @param ValueObjectInterface $object |
||
93 | * @return bool |
||
94 | */ |
||
95 | 3 | public function contains(ValueObjectInterface $object) |
|
96 | { |
||
97 | 3 | foreach ($this->items as $item) { |
|
98 | 3 | if ($item->sameValueAs($object)) { |
|
99 | 3 | return true; |
|
100 | } |
||
101 | 3 | } |
|
102 | |||
103 | 3 | return false; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * Returns a native array representation of the Collection |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | 7 | public function toArray() |
|
112 | { |
||
113 | 7 | return $this->items->toArray(); |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * Returns a native string representation of the Collection object |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | 1 | public function __toString() |
|
122 | { |
||
123 | 1 | $string = \sprintf('%s(%d)', \get_class($this), $this->count()->toNative()); |
|
124 | |||
125 | 1 | return $string; |
|
126 | } |
||
127 | |||
128 | function jsonSerialize() |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
It is recommend to declare an explicit visibility for
jsonSerialize .
Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed. If you are not sure which visibility to choose, it is a good idea to start with
the most restrictive visibility, and then raise visibility as needed, i.e.
start with ![]() |
|||
129 | { |
||
130 | return $this->toArray(); |
||
131 | } |
||
132 | |||
133 | |||
134 | } |
||
135 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.