| Conditions | 16 |
| Paths | 21 |
| Total Lines | 47 |
| Code Lines | 28 |
| 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 |
||
| 85 | protected static function generateContentArray($contentRows, $keys, $generateHeader = true) |
||
| 86 | { |
||
| 87 | if (is_scalar($contentRows)) { |
||
| 88 | throw new Exception("Content must be either an array, object or traversable"); |
||
| 89 | } |
||
| 90 | |||
| 91 | $attributeKeys = $keys; |
||
| 92 | $header = []; |
||
| 93 | $rows = []; |
||
| 94 | $i = 0; |
||
| 95 | foreach ($contentRows as $content) { |
||
| 96 | // handle rows content |
||
| 97 | if (!empty($keys) && is_array($content)) { |
||
| 98 | foreach ($content as $k => $v) { |
||
| 99 | if (!in_array($k, $keys)) { |
||
| 100 | unset($content[$k]); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } elseif (!empty($keys) && is_object($content)) { |
||
| 104 | $attributeKeys[get_class($content)] = $keys; |
||
| 105 | } |
||
| 106 | $rows[$i] = ArrayHelper::toArray($content, $attributeKeys, false); |
||
| 107 | |||
| 108 | // handler header |
||
| 109 | if ($i == 0 && $generateHeader) { |
||
| 110 | if ($content instanceof ActiveRecordInterface) { |
||
| 111 | foreach ($content as $k => $v) { |
||
| 112 | if (empty($keys)) { |
||
| 113 | $header[] = $content->getAttributeLabel($k); |
||
| 114 | } elseif (in_array($k, $keys)) { |
||
| 115 | $header[] = $content->getAttributeLabel($k); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } else { |
||
| 119 | $header = array_keys($rows[0]); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | $i++; |
||
| 124 | } |
||
| 125 | |||
| 126 | if ($generateHeader) { |
||
| 127 | return ArrayHelper::merge([$header], $rows); |
||
| 128 | } |
||
| 129 | |||
| 130 | return $rows; |
||
| 131 | } |
||
| 132 | |||
| 170 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: