Conditions | 21 |
Paths | 1159 |
Total Lines | 73 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
40 | public static function export(mixed $value, ?bool &$isStaticValue = null, array &$foundClasses = []): string |
||
41 | { |
||
42 | $isStaticValue = true; |
||
43 | |||
44 | if (!\is_object($value) && !(\is_array($value) && $value) && !\is_resource($value) || $value instanceof \UnitEnum) { |
||
45 | return Exporter::export($value); |
||
46 | } |
||
47 | |||
48 | $objectsPool = new \SplObjectStorage(); |
||
49 | $refsPool = []; |
||
50 | $objectsCount = 0; |
||
51 | |||
52 | try { |
||
53 | $value = Exporter::prepare([$value], $objectsPool, $refsPool, $objectsCount, $isStaticValue)[0]; |
||
54 | } finally { |
||
55 | $references = []; |
||
56 | foreach ($refsPool as $i => $v) { |
||
57 | if ($v[0]->count) { |
||
58 | $references[1 + $i] = $v[2]; |
||
59 | } |
||
60 | $v[0] = $v[1]; |
||
61 | } |
||
62 | } |
||
63 | |||
64 | if ($isStaticValue) { |
||
65 | return Exporter::export($value); |
||
66 | } |
||
67 | |||
68 | $classes = []; |
||
69 | $values = []; |
||
70 | $states = []; |
||
71 | foreach ($objectsPool as $i => $v) { |
||
72 | [, $class, $values[], $wakeup] = $objectsPool[$v]; |
||
73 | $foundClasses[$class] = $classes[] = $class; |
||
74 | |||
75 | if (0 < $wakeup) { |
||
76 | $states[$wakeup] = $i; |
||
77 | } elseif (0 > $wakeup) { |
||
78 | $states[-$wakeup] = [$i, array_pop($values)]; |
||
79 | $values[] = []; |
||
80 | } |
||
81 | } |
||
82 | ksort($states); |
||
83 | |||
84 | $wakeups = [null]; |
||
85 | foreach ($states as $v) { |
||
86 | if (\is_array($v)) { |
||
87 | $wakeups[-$v[0]] = $v[1]; |
||
88 | } else { |
||
89 | $wakeups[] = $v; |
||
90 | } |
||
91 | } |
||
92 | |||
93 | if (null === $wakeups[0]) { |
||
94 | unset($wakeups[0]); |
||
95 | } |
||
96 | |||
97 | $properties = []; |
||
98 | foreach ($values as $i => $vars) { |
||
99 | foreach ($vars as $class => $values) { |
||
100 | foreach ($values as $name => $v) { |
||
101 | $properties[$class][$name][$i] = $v; |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | |||
106 | if ($classes || $references) { |
||
107 | $value = new Hydrator(new Registry($classes), $references ? new Values($references) : null, $properties, $value, $wakeups); |
||
108 | } else { |
||
109 | $isStaticValue = true; |
||
110 | } |
||
111 | |||
112 | return Exporter::export($value); |
||
113 | } |
||
115 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: