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