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 PragmaRX\Health\Support\Traits; |
||
4 | |||
5 | use Illuminate\Contracts\Support\Arrayable; |
||
6 | |||
7 | trait ToArray |
||
8 | { |
||
9 | private static $__depth; |
||
10 | |||
11 | private static $__maxDepth; |
||
12 | |||
13 | /** |
||
14 | * Reset depth and convert object to array. |
||
15 | * |
||
16 | * @param $subject |
||
17 | * @param int $maxDepth |
||
18 | * @return array|null |
||
19 | */ |
||
20 | public function __toArray($subject, $maxDepth = 3) |
||
21 | { |
||
22 | static::$__depth = 0; |
||
0 ignored issues
–
show
|
|||
23 | |||
24 | static::$__maxDepth = $maxDepth; |
||
0 ignored issues
–
show
Since
$__maxDepth is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self , or increasing the visibility of $__maxDepth to at least protected.
Let’s assume you have a class which uses late-static binding: class YourClass
{
private static $someVariable;
public static function getSomeVariable()
{
return static::$someVariable;
}
}
The code above will run fine in your PHP runtime. However, if you now create a
sub-class and call the class YourSubClass extends YourClass { }
YourSubClass::getSomeVariable(); // Will cause an access error.
In the case above, it makes sense to update class SomeClass
{
private static $someVariable;
public static function getSomeVariable()
{
return self::$someVariable; // self works fine with private.
}
}
Loading history...
|
|||
25 | |||
26 | return $this->___toArray($subject, $maxDepth); |
||
0 ignored issues
–
show
The call to
ToArray::___toArray() has too many arguments starting with $maxDepth .
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. In this case you can add the
Loading history...
|
|||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Convert object to array. |
||
31 | * |
||
32 | * @param $subject |
||
33 | * @return array|null |
||
34 | */ |
||
35 | public function ___toArray($subject) |
||
36 | { |
||
37 | $callback = $this->__getToArrayCallBack(); |
||
38 | |||
39 | return $callback($subject); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Generate a callback to transform object to array. |
||
44 | * |
||
45 | * @return \Closure |
||
46 | */ |
||
47 | public function __getToArrayCallBack() |
||
48 | { |
||
49 | return function ($subject) { |
||
50 | static::$__depth++; |
||
0 ignored issues
–
show
Since
$__depth is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self , or increasing the visibility of $__depth to at least protected.
Let’s assume you have a class which uses late-static binding: class YourClass
{
private static $someVariable;
public static function getSomeVariable()
{
return static::$someVariable;
}
}
The code above will run fine in your PHP runtime. However, if you now create a
sub-class and call the class YourSubClass extends YourClass { }
YourSubClass::getSomeVariable(); // Will cause an access error.
In the case above, it makes sense to update class SomeClass
{
private static $someVariable;
public static function getSomeVariable()
{
return self::$someVariable; // self works fine with private.
}
}
Loading history...
|
|||
51 | |||
52 | if ($subject instanceof Arrayable) { |
||
53 | $subject = $subject->toArray(); |
||
54 | } |
||
55 | |||
56 | if (is_object($subject)) { |
||
57 | $subject = get_object_vars($subject); |
||
58 | } |
||
59 | |||
60 | if (is_array($subject)) { |
||
61 | if (static::$__depth <= static::$__maxDepth) { |
||
0 ignored issues
–
show
Since
$__depth is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self , or increasing the visibility of $__depth to at least protected.
Let’s assume you have a class which uses late-static binding: class YourClass
{
private static $someVariable;
public static function getSomeVariable()
{
return static::$someVariable;
}
}
The code above will run fine in your PHP runtime. However, if you now create a
sub-class and call the class YourSubClass extends YourClass { }
YourSubClass::getSomeVariable(); // Will cause an access error.
In the case above, it makes sense to update class SomeClass
{
private static $someVariable;
public static function getSomeVariable()
{
return self::$someVariable; // self works fine with private.
}
}
Loading history...
Since
$__maxDepth is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self , or increasing the visibility of $__maxDepth to at least protected.
Let’s assume you have a class which uses late-static binding: class YourClass
{
private static $someVariable;
public static function getSomeVariable()
{
return static::$someVariable;
}
}
The code above will run fine in your PHP runtime. However, if you now create a
sub-class and call the class YourSubClass extends YourClass { }
YourSubClass::getSomeVariable(); // Will cause an access error.
In the case above, it makes sense to update class SomeClass
{
private static $someVariable;
public static function getSomeVariable()
{
return self::$someVariable; // self works fine with private.
}
}
Loading history...
|
|||
62 | $subject = array_map( |
||
63 | $this->__getToArrayCallBack(), |
||
64 | $subject |
||
65 | ); |
||
66 | } else { |
||
67 | $subject = null; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | static::$__depth--; |
||
0 ignored issues
–
show
Since
$__depth is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self , or increasing the visibility of $__depth to at least protected.
Let’s assume you have a class which uses late-static binding: class YourClass
{
private static $someVariable;
public static function getSomeVariable()
{
return static::$someVariable;
}
}
The code above will run fine in your PHP runtime. However, if you now create a
sub-class and call the class YourSubClass extends YourClass { }
YourSubClass::getSomeVariable(); // Will cause an access error.
In the case above, it makes sense to update class SomeClass
{
private static $someVariable;
public static function getSomeVariable()
{
return self::$someVariable; // self works fine with private.
}
}
Loading history...
|
|||
72 | |||
73 | return $subject; |
||
74 | }; |
||
75 | } |
||
76 | } |
||
77 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: