Total Complexity | 104 |
Total Lines | 553 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Helpers often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Helpers, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Helpers |
||
20 | { |
||
21 | /** |
||
22 | * Détermine si la version actuelle de PHP est égale ou supérieure à la valeur fournie |
||
23 | */ |
||
24 | public static function isPhp(string $version): bool |
||
25 | { |
||
26 | static $_is_php; |
||
27 | |||
28 | if (! isset($_is_php[$version])) { |
||
29 | $_is_php[$version] = version_compare(PHP_VERSION, $version, '>='); |
||
30 | } |
||
31 | |||
32 | return $_is_php[$version]; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Tester si une application s'exécute en local ou en ligne |
||
37 | */ |
||
38 | public static function isOnline(): bool |
||
39 | { |
||
40 | $host = explode(':', $_SERVER['HTTP_HOST'] ?? '')[0]; |
||
41 | |||
42 | return |
||
43 | ! empty($host) // Si c'est vide, ca veut certainement dire qu'on est en CLI, or le CLI << n'est pas >> utilisé en ligne |
||
44 | && ! in_array($host, ['localhost', '127.0.0.1'], true) |
||
45 | && ! preg_match('#\.dev$#', $host) |
||
46 | && ! preg_match('#\.test$#', $host) |
||
47 | && ! preg_match('#\.lab$#', $host) |
||
48 | && ! preg_match('#\.loc(al)?$#', $host) |
||
49 | && ! preg_match('#^192\.168#', $host); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Tests d'inscriptibilité des fichiers |
||
54 | * |
||
55 | * is_writable() renvoie TRUE sur les serveurs Windows lorsque vous ne pouvez vraiment pas écrire |
||
56 | * le fichier, basé sur l'attribut en lecture seule. is_writable() n'est pas non plus fiable |
||
57 | * sur les serveurs Unix si safe_mode est activé. |
||
58 | * |
||
59 | * @see https://bugs.php.net/bug.php?id=54709 |
||
60 | * |
||
61 | * @throws Exception |
||
62 | * @codeCoverageIgnore Pas pratique à tester, car travis fonctionne sous linux |
||
63 | */ |
||
64 | public static function isReallyWritable(string $file): bool |
||
65 | { |
||
66 | // If we're on a Unix server with safe_mode off we call is_writable |
||
67 | if (DIRECTORY_SEPARATOR === '/' || ! ini_get('safe_mode')) { |
||
68 | return is_writable($file); |
||
69 | } |
||
70 | |||
71 | /* Pour les serveurs Windows et les installations safe_mode "on", nous allons en fait |
||
72 | * écrire un fichier puis le lire. Bah... |
||
73 | */ |
||
74 | if (is_dir($file)) { |
||
75 | $file = rtrim($file, '/') . '/' . bin2hex(random_bytes(16)); |
||
76 | if (($fp = @fopen($file, 'ab')) === false) { |
||
77 | return false; |
||
78 | } |
||
79 | |||
80 | fclose($fp); |
||
81 | @chmod($file, 0777); |
||
82 | @unlink($file); |
||
83 | |||
84 | return true; |
||
85 | } |
||
86 | if (! is_file($file) || ($fp = @fopen($file, 'ab')) === false) { |
||
87 | return false; |
||
88 | } |
||
89 | |||
90 | fclose($fp); |
||
91 | |||
92 | return true; |
||
93 | } |
||
94 | |||
95 | public static function cleanUrl(string $url): string |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Supprimer les caractères invisibles |
||
131 | * |
||
132 | * Cela empêche de prendre en sandwich des caractères nuls |
||
133 | * entre les caractères ascii, comme Java\0script. |
||
134 | */ |
||
135 | public static function removeInvisibleCharacters(string $str, bool $url_encoded = true): string |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Effectue un simple échappement automatique des données pour des raisons de sécurité. |
||
156 | * Pourrait envisager de rendre cela plus complexe à une date ultérieure. |
||
157 | * |
||
158 | * Si $data est une chaîne, il suffit alors de l'échapper et de la renvoyer. |
||
159 | * Si $data est un tableau, alors il boucle dessus, s'échappant de chaque |
||
160 | * 'valeur' des paires clé/valeur. |
||
161 | * |
||
162 | * Valeurs de contexte valides : html, js, css, url, attr, raw, null |
||
163 | * |
||
164 | * @param array|string $data |
||
165 | * |
||
166 | * @throws InvalidArgumentException |
||
167 | * |
||
168 | * @return array|string |
||
169 | */ |
||
170 | public static function esc($data, ?string $context = 'html', ?string $encoding = null) |
||
171 | { |
||
172 | if (is_array($data)) { |
||
173 | foreach ($data as $key => &$value) { |
||
174 | $value = self::esc($value, $context); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | if (is_string($data)) { |
||
179 | $context = strtolower($context); |
||
180 | |||
181 | // Fournit un moyen de NE PAS échapper aux données depuis |
||
182 | // cela pourrait être appelé automatiquement par |
||
183 | // la bibliothèque View. |
||
184 | if (empty($context) || $context === 'raw') { |
||
185 | return $data; |
||
186 | } |
||
187 | |||
188 | if (! in_array($context, ['html', 'js', 'css', 'url', 'attr'], true)) { |
||
189 | throw new InvalidArgumentException('Invalid escape context provided.'); |
||
190 | } |
||
191 | |||
192 | if ($context === 'attr') { |
||
193 | $method = 'escapeHtmlAttr'; |
||
194 | } else { |
||
195 | $method = 'escape' . ucfirst($context); |
||
196 | } |
||
197 | |||
198 | static $escaper; |
||
199 | if (! $escaper) { |
||
200 | $escaper = new \Laminas\Escaper\Escaper($encoding); |
||
201 | } |
||
202 | |||
203 | if ($encoding && $escaper->getEncoding() !== $encoding) { |
||
204 | $escaper = new \Laminas\Escaper\Escaper($encoding); |
||
205 | } |
||
206 | |||
207 | $data = $escaper->{$method}($data); |
||
208 | } |
||
209 | |||
210 | return $data; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Méthode pratique pour htmlspecialchars. |
||
215 | * |
||
216 | * @param mixed $text Texte à envelopper dans htmlspecialchars. Fonctionne également avec des tableaux et des objets. |
||
217 | * Les tableaux seront mappés et tous leurs éléments seront échappés. Les objets seront transtypés s'ils |
||
218 | * implémenter une méthode `__toString`. Sinon, le nom de la classe sera utilisé. |
||
219 | * Les autres types de scalaires seront renvoyés tels quels. |
||
220 | * @param bool $double Encodez les entités html existantes. |
||
221 | * @param string|null $charset Jeu de caractères à utiliser lors de l'échappement. La valeur par défaut est la valeur de configuration dans `mb_internal_encoding()` ou 'UTF-8'. |
||
222 | * |
||
223 | * @return mixed Texte enveloppé. |
||
224 | * @credit CackePHP (https://cakephp.org) |
||
225 | */ |
||
226 | public static function h($text, bool $double = true, ?string $charset = null) |
||
227 | { |
||
228 | if (is_string($text)) { |
||
229 | // optimize for strings |
||
230 | } elseif (is_array($text)) { |
||
231 | $texts = []; |
||
232 | |||
233 | foreach ($text as $k => $t) { |
||
234 | $texts[$k] = self::h($t, $double, $charset); |
||
235 | } |
||
236 | |||
237 | return $texts; |
||
238 | } elseif (is_object($text)) { |
||
239 | if (method_exists($text, '__toString')) { |
||
240 | $text = (string) $text; |
||
241 | } else { |
||
242 | $text = '(object)' . get_class($text); |
||
243 | } |
||
244 | } elseif ($text === null || is_scalar($text)) { |
||
245 | return $text; |
||
246 | } |
||
247 | |||
248 | static $defaultCharset = false; |
||
249 | if ($defaultCharset === false) { |
||
250 | $defaultCharset = mb_internal_encoding(); |
||
251 | if ($defaultCharset === null) { |
||
252 | $defaultCharset = 'UTF-8'; |
||
253 | } |
||
254 | } |
||
255 | if (is_string($double)) { |
||
256 | self::deprecationWarning( |
||
257 | 'Passing charset string for 2nd argument is deprecated. ' . |
||
258 | 'Use the 3rd argument instead.' |
||
259 | ); |
||
260 | $charset = $double; |
||
261 | $double = true; |
||
262 | } |
||
263 | |||
264 | return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, $charset ?: $defaultCharset, $double); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Garantit qu'une extension se trouve à la fin d'un nom de fichier |
||
269 | */ |
||
270 | public static function ensureExt(string $path, string $ext = 'php'): string |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Purifiez l'entrée à l'aide de la classe autonome HTMLPurifier. |
||
285 | * Utilisez facilement plusieurs configurations de purificateur. |
||
286 | * |
||
287 | * @param string|string[] $dirty_html |
||
288 | * @param false|string $config |
||
289 | * |
||
290 | * @return string|string[] |
||
291 | */ |
||
292 | public static function purify($dirty_html, $config = false, string $charset = 'UTF-8') |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Chaîner les attributs à utiliser dans les balises HTML. |
||
330 | * |
||
331 | * Fonction d'assistance utilisée pour convertir une chaîne, un tableau ou un objet |
||
332 | * d'attributs à une chaîne. |
||
333 | * |
||
334 | * @param array|object|string $attributes |
||
335 | */ |
||
336 | public static function stringifyAttributes($attributes, bool $js = false): string |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Obtient une variable d'environnement à partir des sources disponibles et fournit une émulation |
||
359 | * pour les variables d'environnement non prises en charge ou incohérentes (c'est-à-dire DOCUMENT_ROOT sur |
||
360 | * IIS, ou SCRIPT_NAME en mode CGI). Expose également quelques coutumes supplémentaires |
||
361 | * informations sur l'environnement. |
||
362 | * |
||
363 | * @param string $key Nom de la variable d'environnement |
||
364 | * @param mixed|null $default Spécifiez une valeur par défaut au cas où la variable d'environnement n'est pas définie. |
||
365 | * |
||
366 | * @return string Paramétrage des variables d'environnement. |
||
367 | * @credit CakePHP - http://book.cakephp.org/4.0/en/core-libraries/global-constants-and-functions.html#env |
||
368 | */ |
||
369 | public static function env(string $key, $default = null) |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Recherche l'URL de base de l'application independamment de la configuration de l'utilisateur |
||
420 | */ |
||
421 | public static function findBaseUrl(): string |
||
422 | { |
||
423 | if (isset($_SERVER['SERVER_ADDR'])) { |
||
424 | $server_addr = $_SERVER['HTTP_HOST'] ?? ((strpos($_SERVER['SERVER_ADDR'], ':') !== false) ? '[' . $_SERVER['SERVER_ADDR'] . ']' : $_SERVER['SERVER_ADDR']); |
||
425 | |||
426 | if (isset($_SERVER['SERVER_PORT'])) { |
||
427 | $server_addr .= ':' . ((! preg_match('#:' . $_SERVER['SERVER_PORT'] . '$#', $server_addr)) ? $_SERVER['SERVER_PORT'] : '80'); |
||
428 | } |
||
429 | |||
430 | if ( |
||
431 | (! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') |
||
432 | || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https') |
||
433 | || (! empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') |
||
434 | ) { |
||
435 | $base_url = 'https'; |
||
436 | } else { |
||
437 | $base_url = 'http'; |
||
438 | } |
||
439 | |||
440 | $base_url .= '://' . $server_addr . dirname(substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])))); |
||
441 | } else { |
||
442 | $base_url = 'http://localhost:' . ($_SERVER['SERVER_PORT'] ?? '80'); |
||
443 | } |
||
444 | |||
445 | return $base_url; |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Jolie fonction de commodité d'impression JSON. |
||
450 | * |
||
451 | * Dans les terminaux, cela agira de la même manière que json_encode() avec JSON_PRETTY_PRINT directement, lorsqu'il n'est pas exécuté sur cli |
||
452 | * enveloppera également les balises <pre> autour de la sortie de la variable donnée. Similaire à pr(). |
||
453 | * |
||
454 | * Cette fonction renvoie la même variable qui a été transmise. |
||
455 | * |
||
456 | * @param mixed $var Variable à imprimer. |
||
457 | * |
||
458 | * @return mixed le même $var qui a été passé à cette fonction |
||
459 | * |
||
460 | * @see pr() |
||
461 | */ |
||
462 | public static function pj($var) |
||
463 | { |
||
464 | $template = (PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg') ? '<pre class="pj">%s</pre>' : "\n%s\n\n"; |
||
465 | printf($template, trim(json_encode($var, JSON_PRETTY_PRINT))); |
||
466 | |||
467 | return $var; |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * Méthode d'assistance pour générer des avertissements d'obsolescence |
||
472 | * |
||
473 | * @param string $message Le message à afficher comme avertissement d'obsolescence. |
||
474 | * @param int $stackFrame Le cadre de pile à inclure dans l'erreur. Par défaut à 1 |
||
475 | * car cela devrait pointer vers le code de l'application/du plugin. |
||
476 | * |
||
477 | * @return void |
||
478 | */ |
||
479 | public static function deprecationWarning(string $message, int $stackFrame = 1) |
||
480 | { |
||
481 | if (! (error_reporting() & E_USER_DEPRECATED)) { |
||
482 | return; |
||
483 | } |
||
484 | |||
485 | $trace = debug_backtrace(); |
||
486 | if (isset($trace[$stackFrame])) { |
||
487 | $frame = $trace[$stackFrame]; |
||
488 | $frame += ['file' => '[internal]', 'line' => '??']; |
||
489 | |||
490 | $message = sprintf( |
||
491 | '%s - %s, line: %s' . "\n" . |
||
492 | ' You can disable deprecation warnings by setting `Error.errorLevel` to' . |
||
493 | ' `E_ALL & ~E_USER_DEPRECATED` in your config/app.php.', |
||
494 | $message, |
||
495 | $frame['file'], |
||
496 | $frame['line'] |
||
497 | ); |
||
498 | } |
||
499 | |||
500 | @trigger_error($message, E_USER_DEPRECATED); |
||
501 | } |
||
502 | |||
503 | /** |
||
504 | * Déclenche un E_USER_WARNING. |
||
505 | */ |
||
506 | public static function triggerWarning(string $message) |
||
507 | { |
||
508 | $stackFrame = 1; |
||
509 | $trace = debug_backtrace(); |
||
510 | if (isset($trace[$stackFrame])) { |
||
511 | $frame = $trace[$stackFrame]; |
||
512 | $frame += ['file' => '[internal]', 'line' => '??']; |
||
513 | $message = sprintf( |
||
514 | '%s - %s, line: %s', |
||
515 | $message, |
||
516 | $frame['file'], |
||
517 | $frame['line'] |
||
518 | ); |
||
519 | } |
||
520 | trigger_error($message, E_USER_WARNING); |
||
521 | } |
||
522 | |||
523 | /** |
||
524 | * Divise un nom de plugin de syntaxe à points en son plugin et son nom de classe. |
||
525 | * Si $name n'a pas de point, alors l'index 0 sera nul. |
||
526 | * |
||
527 | * Couramment utilisé comme |
||
528 | * ``` |
||
529 | * list($plugin, $name) = Helpers::pluginSplit($name); |
||
530 | * ``` |
||
531 | * |
||
532 | * @param string $name Le nom que vous voulez diviser en plugin. |
||
533 | * @param bool $dotAppend Définir sur true si vous voulez que le plugin ait un '.' qui y est annexé. |
||
534 | * @param string|null $plugin Plugin optionnel par défaut à utiliser si aucun plugin n'est trouvé. La valeur par défaut est nulle. |
||
535 | * |
||
536 | * @return array Tableau avec 2 index. 0 => nom du plugin, 1 => nom de la classe. |
||
537 | * @credit <a href="https://book.cakephp.org/4/en/core-libraries/global-constants-and-functions.html#pluginSplit">CakePHP</a> |
||
538 | * @psalm-return array{string|null, string} |
||
539 | */ |
||
540 | public static function pluginSplit(string $name, bool $dotAppend = false, ?string $plugin = null): array |
||
553 | } |
||
554 | |||
555 | /** |
||
556 | * Séparez l'espace de noms du nom de classe. |
||
557 | * |
||
558 | * Couramment utilisé comme `list($namespace, $className) = Helpers::namespaceSplit($class);`. |
||
559 | * |
||
560 | * @param string $class Le nom complet de la classe, ie `BlitzPHP\Core\App`. |
||
561 | * |
||
562 | * @return array<string> Tableau avec 2 index. 0 => namespace, 1 => nom de la classe. |
||
563 | */ |
||
564 | public static function namespaceSplit(string $class): array |
||
572 | } |
||
573 | } |
||
574 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths