Passed
Pull Request — main (#18)
by Dimitri
03:55
created

DebugTraceableTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 34
ccs 5
cts 5
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A lang() 0 5 1
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Exceptions;
13
14
use BlitzPHP\Loader\Services;
15
use Throwable;
16
17
/**
18
 * Ce trait fournit aux exceptions du cadre la possibilité d'identifier
19
 * précisément où l'exception a été déclenchée plutôt qu'instanciée.
20
 *
21
 * Ceci est principalement utilisé pour les exceptions instanciées dans les factories.
22
 */
23
trait DebugTraceableTrait
24
{
25
    /**
26
     * Ajuste le constructeur de l'exception pour assigner le fichier/la ligne à où
27
     * il est réellement déclenché plutôt que d'être instancié.
28
     */
29
    final public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null)
30
    {
31 66
        parent::__construct($message, $code, $previous);
32
33 66
        $trace = $this->getTrace()[0];
0 ignored issues
show
Bug introduced by
It seems like getTrace() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        $trace = $this->/** @scrutinizer ignore-call */ getTrace()[0];
Loading history...
34
35
        if (isset($trace['class']) && $trace['class'] === static::class) {
36
            [
37
                'line' => $this->line,
0 ignored issues
show
Bug Best Practice introduced by
The property line does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
                'file' => $this->file,
0 ignored issues
show
Bug Best Practice introduced by
The property file does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
39 66
            ] = $trace;
40
        }
41
    }
42
43
    /**
44
     * Obtenir le message système traduit
45
     *
46
     * Utilisez une instance de langue non partagée dans les services.
47
     * Si une instance partagée est créée, la langue
48
     * ont les paramètres régionaux actuels, donc même si les utilisateurs appellent
49
     * `$this->request->setLocale()` dans le contrôleur ensuite,
50
     * les paramètres régionaux de la langue ne seront pas modifiés.
51
     */
52
    protected static function lang(string $line, array $args = []): string
53
    {
54 2
        $lang = Services::language(null, false);
0 ignored issues
show
Deprecated Code introduced by
The function BlitzPHP\Container\Services::language() has been deprecated: 0.9 use translators instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

54
        $lang = /** @scrutinizer ignore-deprecated */ Services::language(null, false);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
55
56 2
        return $lang->getLine($line, $args);
57
    }
58
}
59