Conditions | 16 |
Paths | 21 |
Total Lines | 47 |
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 |
||
84 | protected static function generateContentArray($contentRows, $keys, $generateHeader = true) |
||
85 | { |
||
86 | if (is_scalar($contentRows)) { |
||
87 | throw new Exception("Content must be either an array, object or traversable"); |
||
88 | } |
||
89 | |||
90 | $attributeKeys = $keys; |
||
91 | $header = []; |
||
92 | $rows = []; |
||
93 | $i = 0; |
||
94 | foreach ($contentRows as $content) { |
||
95 | // handle rows content |
||
96 | if (!empty($keys) && is_array($content)) { |
||
97 | foreach ($content as $k => $v) { |
||
98 | if (!in_array($k, $keys)) { |
||
99 | unset($content[$k]); |
||
100 | } |
||
101 | } |
||
102 | } elseif (!empty($keys) && is_object($content)) { |
||
103 | $attributeKeys[get_class($content)] = $keys; |
||
104 | } |
||
105 | $rows[$i] = ArrayHelper::toArray($content, $attributeKeys, false); |
||
106 | |||
107 | // handler header |
||
108 | if ($i == 0 && $generateHeader) { |
||
109 | if ($content instanceof ActiveRecordInterface) { |
||
110 | foreach ($content as $k => $v) { |
||
111 | if (empty($keys)) { |
||
112 | $header[] = $content->getAttributeLabel($k); |
||
113 | } elseif (in_array($k, $keys)) { |
||
114 | $header[] = $content->getAttributeLabel($k); |
||
115 | } |
||
116 | } |
||
117 | } else { |
||
118 | $header = array_keys($rows[0]); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | $i++; |
||
123 | } |
||
124 | |||
125 | if ($generateHeader) { |
||
126 | return ArrayHelper::merge([$header], $rows); |
||
127 | } |
||
128 | |||
129 | return $rows; |
||
130 | } |
||
131 | |||
173 |
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: