| Conditions | 17 |
| Paths | 41 |
| Total Lines | 51 |
| Code Lines | 31 |
| 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 |
||
| 45 | */ |
||
| 46 | public static function xlsx($input, array $keys = [], $header = true) |
||
| 47 | { |
||
| 48 | $input = self::transformInput($input); |
||
| 49 | |||
| 50 | $array = self::generateContentArray($input, $keys, $header); |
||
| 51 | |||
| 52 | $writer = new XLSXWriter(); |
||
| 53 | $writer->writeSheet($array); |
||
| 54 | |||
| 55 | return $writer->writeToString(); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Check type of input and return correct array. |
||
| 60 | * |
||
| 61 | * @param array|object $input |
||
| 62 | * @return array |
||
| 63 | * @since 1.0.4 |
||
| 64 | */ |
||
| 65 | protected static function transformInput($input) |
||
| 66 | { |
||
| 67 | if ($input instanceof ActiveQueryInterface) { |
||
| 68 | return $input->all(); |
||
| 69 | } |
||
| 70 | |||
| 71 | return $input; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Generate content by rows. |
||
| 76 | * |
||
| 77 | * @param array $contentRows |
||
| 78 | * @param string$delimiter |
||
| 79 | * @param string $keys |
||
| 80 | * @param bool $generateHeader |
||
| 81 | * @return array |
||
| 82 | * @throws Exception |
||
| 83 | * @since 1.0.4 |
||
| 84 | */ |
||
| 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 |
||
| 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: