Passed
Push — main ( 073c01...b49c3d )
by Dimitri
03:21
created

Validation::discoverRules()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 14
rs 9.6111
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
     * @param array<AbstractRule> $rules
32
     */
33
    protected function registerRules(array $rules): void
34
    {
35
        foreach ($rules as $key => $value) {
36
            if (is_int($key)) {
37
                $name = $value::name();
38
                $rule = $value;
39
            } else {
40
                $name = $value;
41
                $rule = $key;
42
            }
43
44
            $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

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