| Total Complexity | 100 |
| Total Lines | 498 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Services 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 Services, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 62 | class Services |
||
| 63 | { |
||
| 64 | /** |
||
| 65 | * Cache des instances des services demander comme instance "partagee". |
||
| 66 | * La cle est le FQCN du service. |
||
| 67 | */ |
||
| 68 | protected static array $instances = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Cache d'autres classe de que nous avons trouver via la methode discoverService. |
||
| 72 | */ |
||
| 73 | protected static array $services = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return Injector |
||
| 77 | */ |
||
| 78 | public static function injector() |
||
| 79 | { |
||
| 80 | return Injector::instance(); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return \DI\Container |
||
| 85 | */ |
||
| 86 | public static function container() |
||
| 87 | { |
||
| 88 | return Injector::container(); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * La classe Autoloader permet de charger les fichiers simplement. |
||
| 93 | */ |
||
| 94 | public static function autoloader(bool $shared = true): Autoloader |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * La classe de cache fournit un moyen simple de stocker et de récupérer |
||
| 108 | * données complexes pour plus tard |
||
| 109 | */ |
||
| 110 | public static function cache(?array $config = null, bool $shared = true): Cache |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * La clase Config offre une api fluide por gerer les configurations de l'application |
||
| 130 | */ |
||
| 131 | public static function config(bool $shared = true): Config |
||
| 132 | { |
||
| 133 | if (true === $shared && isset(static::$instances[Config::class])) { |
||
| 134 | return static::$instances[Config::class]; |
||
| 135 | } |
||
| 136 | |||
| 137 | return static::$instances[Config::class] = new Config(); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Émetteur de réponse au client |
||
| 142 | */ |
||
| 143 | public static function emitter(bool $shared = true): ResponseEmitter |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Gestionnaire d'evenement |
||
| 154 | */ |
||
| 155 | public static function event(bool $shared = true): EventManager |
||
| 156 | { |
||
| 157 | if (true === $shared && isset(static::$instances[EventManager::class])) { |
||
| 158 | return static::$instances[EventManager::class]; |
||
| 159 | } |
||
| 160 | |||
| 161 | return static::$instances[EventManager::class] = static::factory(EventManager::class); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * System de gestion de fichier |
||
| 166 | */ |
||
| 167 | public static function fs(bool $shared = true): Filesystem |
||
| 168 | { |
||
| 169 | if (true === $shared && isset(static::$instances[Filesystem::class])) { |
||
| 170 | return static::$instances[Filesystem::class]; |
||
| 171 | } |
||
| 172 | |||
| 173 | return static::$instances[Filesystem::class] = static::factory(Filesystem::class); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Le client HTTP fourni une interface simple pour interagir avec d'autres serveurs. |
||
| 178 | * Typiquement a traver des APIs. |
||
| 179 | */ |
||
| 180 | public static function httpclient(?string $baseUrl = null, bool $shared = true): ClientRequest |
||
| 181 | { |
||
| 182 | if (true === $shared && isset(static::$instances[ClientRequest::class])) { |
||
| 183 | return static::$instances[ClientRequest::class]->baseUrl((string) $baseUrl); |
||
| 184 | } |
||
| 185 | |||
| 186 | return static::$instances[ClientRequest::class] = static::factory(ClientRequest::class, ['event' => static::event()])->baseUrl((string) $baseUrl); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Responsable du chargement des traductions des chaînes de langue. |
||
| 191 | */ |
||
| 192 | public static function language(?string $locale = null, bool $shared = true): Translate |
||
| 193 | { |
||
| 194 | if (empty($locale)) { |
||
| 195 | if (empty($locale = static::$instances[Translate::class . 'locale'] ?? null)) { |
||
| 196 | $config = static::config()->get('app'); |
||
| 197 | |||
| 198 | if (empty($locale = static::negotiator()->language($config['supported_locales']))) { |
||
| 199 | $locale = $config['language']; |
||
| 200 | } |
||
| 201 | |||
| 202 | static::$instances[Translate::class . 'locale'] = $locale; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | if (true === $shared && isset(static::$instances[Translate::class])) { |
||
| 207 | return static::$instances[Translate::class]->setLocale($locale); |
||
| 208 | } |
||
| 209 | |||
| 210 | return static::$instances[Translate::class] = static::factory(Translate::class, ['locale' => $locale, 'locator' => static::locator()]); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Le file locator fournit des methodes utilitaire pour chercher les fichiers non-classes |
||
| 215 | * dans les dossiers de namespace. C'est une excelente methode pour charger les 'vues', 'helpers', et 'libraries'. |
||
| 216 | */ |
||
| 217 | public static function locator(bool $shared = true): Locator |
||
| 218 | { |
||
| 219 | if ($shared && isset(static::$instances[Locator::class])) { |
||
| 220 | return static::$instances[Locator::class]; |
||
| 221 | } |
||
| 222 | |||
| 223 | return static::$instances[Locator::class] = static::factory(Locator::class, ['autoloader' => static::autoloader()]); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * La classe Logger est une classe Logging compatible PSR-3 qui prend en charge |
||
| 228 | * plusieurs gestionnaires qui traitent la journalisation réelle. |
||
| 229 | */ |
||
| 230 | public static function logger(bool $shared = true): Logger |
||
| 231 | { |
||
| 232 | if ($shared && isset(static::$instances[Logger::class])) { |
||
| 233 | return static::$instances[Logger::class]; |
||
| 234 | } |
||
| 235 | |||
| 236 | return static::$instances[Logger::class] = static::factory(Logger::class); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * La classe de mail vous permet d'envoyer par courrier électronique via mail, sendmail, SMTP. |
||
| 241 | */ |
||
| 242 | public static function mail(?array $config = null, bool $shared = true): Mail |
||
| 243 | { |
||
| 244 | if (empty($config)) { |
||
| 245 | $config = static::config()->get('mail'); |
||
| 246 | } |
||
| 247 | |||
| 248 | if (true === $shared && isset(static::$instances[Mail::class])) { |
||
| 249 | /** @var Mail $instance */ |
||
| 250 | $instance = static::$instances[Mail::class]; |
||
| 251 | if (empty(func_get_args()[0])) { |
||
| 252 | return $instance; |
||
| 253 | } |
||
| 254 | |||
| 255 | return $instance->merge($config); |
||
| 256 | } |
||
| 257 | |||
| 258 | return static::$instances[Mail::class] = static::factory(Mail::class, compact('config')); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * La classe Input générale modélise une requête HTTP. |
||
| 263 | */ |
||
| 264 | public static function negotiator(?ServerRequest $request = null, bool $shared = true): Negotiator |
||
| 265 | { |
||
| 266 | if (empty($request)) { |
||
| 267 | $request = static::request(true); |
||
| 268 | } |
||
| 269 | |||
| 270 | if (true === $shared && isset(static::$instances[Negotiator::class])) { |
||
| 271 | $instance = static::$instances[Negotiator::class]; |
||
| 272 | if (empty(func_get_args()[0])) { |
||
| 273 | return $instance; |
||
| 274 | } |
||
| 275 | |||
| 276 | return $instance->setRequest($request); |
||
| 277 | } |
||
| 278 | |||
| 279 | return static::$instances[Negotiator::class] = static::factory(Negotiator::class, compact('request')); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * La classe des redirections HTTP |
||
| 284 | */ |
||
| 285 | public static function redirection(bool $shared = true): Redirection |
||
| 286 | { |
||
| 287 | if (true === $shared && isset(static::$instances[Redirection::class])) { |
||
| 288 | return static::$instances[Redirection::class]; |
||
| 289 | } |
||
| 290 | |||
| 291 | return static::$instances[Redirection::class] = static::factory(Redirection::class); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * La classe Resquest modélise une reqûete HTTP. |
||
| 296 | */ |
||
| 297 | public static function request(bool $shared = true): Request |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * La classe Response modélise une réponse HTTP. |
||
| 308 | */ |
||
| 309 | public static function response(bool $shared = true): Response |
||
| 310 | { |
||
| 311 | if (true === $shared && isset(static::$instances[Response::class])) { |
||
| 312 | return static::$instances[Response::class]; |
||
| 313 | } |
||
| 314 | |||
| 315 | return static::$instances[Response::class] = static::factory(Response::class); |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Le service Routes est une classe qui permet de construire facilement |
||
| 320 | * une collection d'itinéraires. |
||
| 321 | */ |
||
| 322 | public static function routes(bool $shared = true): RouteCollection |
||
| 323 | { |
||
| 324 | if (true === $shared && isset(static::$instances[RouteCollection::class])) { |
||
| 325 | return static::$instances[RouteCollection::class]; |
||
| 326 | } |
||
| 327 | |||
| 328 | return static::$instances[RouteCollection::class] = static::factory(RouteCollection::class); |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * La classe Router utilise le tableau de routes d'une RouteCollection et détermine |
||
| 333 | * le contrôleur et la méthode corrects à exécuter. |
||
| 334 | */ |
||
| 335 | public static function router(?RouteCollection $routes = null, ?ServerRequest $request = null, bool $shared = true): Router |
||
| 336 | { |
||
| 337 | if (true === $shared) { |
||
| 338 | return static::singleton(Router::class); |
||
| 339 | } |
||
| 340 | if (empty($routes)) { |
||
| 341 | $routes = static::routes(true); |
||
| 342 | } |
||
| 343 | if (empty($request)) { |
||
| 344 | $request = static::request(true); |
||
| 345 | } |
||
| 346 | |||
| 347 | return static::factory(Router::class, compact('routes', 'request')); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Retourne le gestionnaire de session. |
||
| 352 | */ |
||
| 353 | public static function session(bool $shared = true): Session |
||
| 354 | { |
||
| 355 | if (true === $shared && isset(static::$instances[Session::class])) { |
||
| 356 | return static::$instances[Session::class]; |
||
| 357 | } |
||
| 358 | |||
| 359 | $config = static::config()->get('session'); |
||
| 360 | $db = null; |
||
| 361 | |||
| 362 | if (Text::contains($config['handler'], [DatabaseSessionHandler::class, 'database'])) { |
||
| 363 | $group = $config['group'] ?? static::config()->get('database.connection'); |
||
| 364 | $db = DatabaseConfig::connect($group); |
||
| 365 | |||
| 366 | $driver = $db->getPlatform(); |
||
| 367 | |||
| 368 | if (Text::contains($driver, ['mysql', MySQLSessionHandler::class])) { |
||
| 369 | $config['handler'] = MySQLSessionHandler::class; |
||
| 370 | } elseif (Text::contains($driver, ['postgre', PostgreSessionHandler::class])) { |
||
| 371 | $config['handler'] = PostgreSessionHandler::class; |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | Cookie::setDefaults($cookies = static::config()->get('cookie')); |
||
| 376 | $session = new Session($config, $cookies, Helpers::ipAddress()); |
||
| 377 | $session->setLogger(static::logger()); |
||
| 378 | $session->setDatabase($db); |
||
| 379 | |||
| 380 | if (session_status() === PHP_SESSION_NONE) { |
||
| 381 | $session->start(); |
||
| 382 | } |
||
| 383 | |||
| 384 | return static::$instances[Session::class] = $session; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * System de gestion de fichier par disque |
||
| 389 | */ |
||
| 390 | public static function storage(bool $shared = true): FilesystemManager |
||
| 391 | { |
||
| 392 | if ($shared && isset(static::$instances[FilesystemManager::class])) { |
||
| 393 | return static::$instances[FilesystemManager::class]; |
||
| 394 | } |
||
| 395 | |||
| 396 | return static::$instances[FilesystemManager::class] = new FilesystemManager(static::config()->get('filesystems')); |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * La classe Timer fournit un moyen simple d'évaluer des parties de votre application. |
||
| 401 | */ |
||
| 402 | public static function timer(bool $shared = true): Timer |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Renvoie la barre d'outils de débogage. |
||
| 413 | */ |
||
| 414 | public static function toolbar(?stdClass $config = null, bool $shared = true): Toolbar |
||
| 415 | { |
||
| 416 | if ($shared && isset(static::$instances[Toolbar::class])) { |
||
| 417 | return static::$instances[Toolbar::class]; |
||
| 418 | } |
||
| 419 | |||
| 420 | $config ??= (object) config('toolbar'); |
||
| 421 | |||
| 422 | return static::$instances[Toolbar::class] = static::factory(Toolbar::class, compact('config')); |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * La classe URI fournit un moyen de modéliser et de manipuler les URI. |
||
| 427 | */ |
||
| 428 | public static function uri(?string $uri = null, bool $shared = true): Uri |
||
| 429 | { |
||
| 430 | if (true === $shared && isset(static::$instances[Uri::class])) { |
||
| 431 | return static::$instances[Uri::class]->setURI($uri); |
||
| 432 | } |
||
| 433 | |||
| 434 | return static::$instances[Uri::class] = static::factory(Uri::class, compact('uri')); |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * La classe Renderer est la classe qui affiche réellement un fichier à l'utilisateur. |
||
| 439 | * La classe View par défaut dans BlitzPHP est intentionnellement simple, mais |
||
| 440 | * le service peut facilement être remplacé par un moteur de modèle si l'utilisateur en a besoin. |
||
| 441 | */ |
||
| 442 | public static function viewer(bool $shared = true): View |
||
| 443 | { |
||
| 444 | if (true === $shared && isset(static::$instances[View::class])) { |
||
| 445 | return static::$instances[View::class]; |
||
| 446 | } |
||
| 447 | |||
| 448 | return static::$instances[View::class] = static::factory(View::class); |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Offre la possibilité d'effectuer des appels insensibles à la casse des noms de service. |
||
| 453 | * |
||
| 454 | * @return mixed |
||
| 455 | */ |
||
| 456 | public static function __callStatic(string $name, array $arguments) |
||
| 457 | { |
||
| 458 | if (method_exists(static::class, $name)) { |
||
| 459 | return static::$name(...$arguments); |
||
| 460 | } |
||
| 461 | |||
| 462 | return static::discoverServices($name, $arguments); |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Essaie d'obtenir un service à partir du conteneur |
||
| 467 | * |
||
| 468 | * @return mixed |
||
| 469 | */ |
||
| 470 | protected static function discoverServices(string $name, array $arguments) |
||
| 471 | { |
||
| 472 | $shared = array_pop($arguments); |
||
| 473 | if ($shared !== true) { |
||
| 474 | return static::discoverServiceFactory($name, $arguments); |
||
| 475 | } |
||
| 476 | |||
| 477 | return static::discoverServiceSingleton($name, ...$arguments); |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Essaie d'obtenir un service à partir du conteneur |
||
| 482 | * |
||
| 483 | * @return mixed |
||
| 484 | */ |
||
| 485 | private static function discoverServiceFactory(string $name, array $arguments) |
||
| 494 | } |
||
| 495 | } |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Essaie de trouver un seul service |
||
| 500 | * |
||
| 501 | * @return mixed |
||
| 502 | */ |
||
| 503 | private static function discoverServiceSingleton(string $name) |
||
| 504 | { |
||
| 505 | $arguments = func_get_args(); |
||
| 506 | $name = array_shift($arguments); |
||
| 507 | |||
| 508 | try { |
||
| 509 | return static::singleton($name, ...$arguments); |
||
| 510 | } catch (NotFoundException $e) { |
||
| 511 | try { |
||
| 512 | return static::singleton($name . 'Service', ...$arguments); |
||
| 513 | } catch (NotFoundException $ex) { |
||
| 514 | throw $e; |
||
| 515 | } |
||
| 516 | } |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Injecter une seule instance de la classe donnée |
||
| 521 | * |
||
| 522 | * @return mixed |
||
| 523 | */ |
||
| 524 | public static function singleton(string $name) |
||
| 525 | { |
||
| 526 | $arguments = func_get_args(); |
||
| 527 | $name = array_shift($arguments); |
||
| 528 | |||
| 529 | if (empty(static::$instances[$name])) { |
||
| 530 | if (! empty($arguments)) { |
||
| 531 | static::$instances[$name] = static::factory($name, $arguments); |
||
| 532 | } else { |
||
| 533 | static::$instances[$name] = static::injector()->get($name); |
||
| 534 | } |
||
| 535 | } |
||
| 536 | |||
| 537 | return static::$instances[$name]; |
||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Injecter une nouvelle instance de la classe donnée |
||
| 542 | * |
||
| 543 | * @return mixed |
||
| 544 | */ |
||
| 545 | public static function factory(string $name, array $arguments = []) |
||
| 546 | { |
||
| 547 | return static::injector()->make($name, $arguments); |
||
| 548 | } |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Définissez un objet ou une valeur dans le conteneur. |
||
| 552 | * |
||
| 553 | * @param string $name Nom de l'entrée |
||
| 554 | * @param mixed $value utilisez les aides à la définition pour définir les objets |
||
| 555 | */ |
||
| 556 | public static function set(string $name, $value) |
||
| 560 | } |
||
| 561 | } |
||
| 562 |
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