Passed
Push — main ( c1749a...759bea )
by Dimitri
12:54
created

Validation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 2
cp 0
crap 2
rs 10
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\Validation;
13
14
use BlitzPHP\Autoloader\Locator;
15
use BlitzPHP\Container\Services;
16
use BlitzPHP\Validation\Rules\AbstractRule;
17
use Dimtrovich\Validation\Validation as BaseValidation;
18
19
class Validation extends BaseValidation
20
{
21
    public function __construct()
22
    {
23
        parent::__construct(config('app.language'));
0 ignored issues
show
Bug introduced by
config('app.language') of type BlitzPHP\Config\Config|null is incompatible with the type string expected by parameter $locale of Dimtrovich\Validation\Validation::__construct(). ( Ignorable by Annotation )

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

23
        parent::__construct(/** @scrutinizer ignore-type */ config('app.language'));
Loading history...
24
25
        $this->discoverRules();
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    public function errors(): ErrorBag
32
    {
33
        return new ErrorBag(parent::errors()->toArray());
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     *
39
     * @param array<AbstractRule> $rules
40
     */
41
    protected function registerRules(array $rules): void
42
    {
43
        foreach ($rules as $key => $value) {
44
            if (is_int($key)) {
45
                $name = $value::name();
46
                $rule = $value;
47
            } else {
48
                $name = $value;
49
                $rule = $key;
50
            }
51
52
            $this->addValidator($name, Services::container()->get($rule));
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type BlitzPHP\Validation\Rules\AbstractRule; however, parameter $name of Dimtrovich\Validation\Validation::addValidator() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

52
            $this->addValidator(/** @scrutinizer ignore-type */ $name, Services::container()->get($rule));
Loading history...
53
        }
54
    }
55
56
    /**
57
     * Definie les fichiers qui pourront etre considerer comme regles de validations
58
     *
59
     * @return string[] Chemins absolus des fichiers
60
     */
61
    protected function files(Locator $locator): array
62
    {
63
        $files = array_merge(
64
            $locator->listFiles('Rules/'), // Regles de l'application ou des fournisseurs
65
            $locator->listFiles('Validation/Rules/') // Regles internes du framework
66
        );
67
68
        return array_unique($files);
69
    }
70
71
    /**
72
     * Recherche toutes les regles de validation dans le framework et dans le code de l'utilisateur
73
     * et collecte leurs instances pour fonctionner avec eux.
74
     */
75
    private function discoverRules()
76
    {
77
        $files = $this->files($locator = Services::locator());
78
        $rules = [];
79
80
        foreach ($files as $file) {
81
            $className = $locator->getClassname($file);
82
83
            if ($className !== '' && class_exists($className) && is_subclass_of($className, AbstractRule::class)) {
84
                $rules[] = $className;
85
            }
86
        }
87
88
        $this->registerRules($rules);
89
    }
90
}
91