| Conditions | 12 |
| Paths | 78 |
| Total Lines | 69 |
| Code Lines | 36 |
| 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 |
||
| 35 | public static function helper(array|string $filenames) |
||
| 36 | { |
||
| 37 | static $loaded = []; |
||
| 38 | |||
| 39 | $loader = Services::locator(); |
||
| 40 | |||
| 41 | if (! is_array($filenames)) { |
||
| 42 | $filenames = [$filenames]; |
||
| 43 | } |
||
| 44 | |||
| 45 | // Enregistrez une liste de tous les fichiers à inclure... |
||
| 46 | $includes = []; |
||
| 47 | |||
| 48 | foreach ($filenames as $filename) { |
||
| 49 | // Stockez nos versions d'helpers système et d'application afin que nous puissions contrôler l'ordre de chargement. |
||
| 50 | $systemHelper = null; |
||
| 51 | $appHelper = null; |
||
| 52 | $localIncludes = []; |
||
| 53 | |||
| 54 | // Vérifiez si ce helper a déjà été chargé |
||
| 55 | if (in_array($filename, $loaded, true)) { |
||
| 56 | continue; |
||
| 57 | } |
||
| 58 | |||
| 59 | // Si le fichier est dans un espace de noms, nous allons simplement saisir ce fichier et ne pas en rechercher d'autres |
||
| 60 | if (strpos($filename, '\\') !== false) { |
||
| 61 | $path = $loader->locateFile($filename, 'Helpers'); |
||
| 62 | |||
| 63 | if (empty($path)) { |
||
| 64 | throw LoadException::helperNotFound($filename); |
||
| 65 | } |
||
| 66 | |||
| 67 | $includes[] = $path; |
||
| 68 | $loaded[] = $filename; |
||
| 69 | } else { |
||
| 70 | // Pas d'espaces de noms, donc recherchez dans tous les emplacements disponibles |
||
| 71 | $paths = $loader->search('Helpers/' . $filename); |
||
| 72 | |||
| 73 | foreach ($paths as $path) { |
||
| 74 | if (strpos($path, APP_PATH . 'Helpers' . DS) === 0) { |
||
| 75 | $appHelper = $path; |
||
| 76 | } elseif (strpos($path, SYST_PATH . 'Helpers' . DS) === 0) { |
||
| 77 | $systemHelper = $path; |
||
| 78 | } else { |
||
| 79 | $localIncludes[] = $path; |
||
| 80 | $loaded[] = $filename; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | // Les helpers au niveau de l'application doivent remplacer tous les autres |
||
| 85 | if (! empty($appHelper)) { |
||
| 86 | $includes[] = $appHelper; |
||
| 87 | $loaded[] = $filename; |
||
| 88 | } |
||
| 89 | |||
| 90 | // Tous les fichiers avec espace de noms sont ajoutés ensuite |
||
| 91 | $includes = [...$includes, ...$localIncludes]; |
||
| 92 | |||
| 93 | // Et celui par défaut du système doit être ajouté en dernier. |
||
| 94 | if (! empty($systemHelper)) { |
||
| 95 | $includes[] = $systemHelper; |
||
| 96 | $loaded[] = $filename; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | // Incluez maintenant tous les fichiers |
||
| 102 | foreach ($includes as $path) { |
||
| 103 | include_once $path; |
||
| 104 | } |
||
| 228 |
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: