1 | <?php |
||||
2 | |||||
3 | namespace Konsulting\Laravel\RuleRepository; |
||||
4 | |||||
5 | use Konsulting\Laravel\RuleRepository\Exceptions\RuleRepositoryTraitNotUsed; |
||||
6 | |||||
7 | /** |
||||
8 | * @method static array validationRules(string $state = '') |
||||
9 | * @method static array transformerRules(string $state = '') |
||||
10 | */ |
||||
11 | trait RepositoryMagicMethods |
||||
12 | { |
||||
13 | /** |
||||
14 | * @param string $method |
||||
15 | * @param array $args |
||||
16 | * @return array |
||||
17 | */ |
||||
18 | 4 | public static function __callStatic($method, $args) |
|||
19 | { |
||||
20 | 4 | if ($rules = static::getRulesMagically($method, $args)) { |
|||
21 | 3 | return $rules; |
|||
22 | } |
||||
23 | 1 | if (get_parent_class(static::class)) { |
|||
24 | 1 | return parent::__callStatic($method, $args); |
|||
25 | } |
||||
26 | } |
||||
27 | |||||
28 | /** |
||||
29 | * @param string $method |
||||
30 | * @param array $args |
||||
31 | * @return array |
||||
32 | */ |
||||
33 | 3 | public function __call($method, $args) |
|||
34 | { |
||||
35 | 3 | if ($rules = static::getRulesMagically($method, $args)) { |
|||
36 | 1 | return $rules; |
|||
37 | } |
||||
38 | 1 | if (get_parent_class($this)) { |
|||
39 | 1 | return parent::__call($method, $args); |
|||
40 | } |
||||
41 | } |
||||
42 | |||||
43 | /** |
||||
44 | * Check that the undefined method matches the repository rules pattern, and if so return the rules for that |
||||
45 | * repository. |
||||
46 | * |
||||
47 | * @param string $method |
||||
48 | * @param array $args |
||||
49 | * @return array |
||||
50 | */ |
||||
51 | 5 | private static function getRulesMagically($method, $args) |
|||
52 | { |
||||
53 | 5 | static::checkForRuleRepositoryTrait(); |
|||
54 | 3 | $repositoryName = static::extractFromString($method, |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
55 | 3 | config('rule_repository.method_suffix', 'Rules')); |
|||
56 | |||||
57 | 3 | return $repositoryName |
|||
58 | 3 | ? static::getRules($repositoryName, ...$args) |
|||
0 ignored issues
–
show
The method
getRules() does not exist on Konsulting\Laravel\RuleR...\RepositoryMagicMethods . Since you implemented __callStatic , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
59 | 3 | : null; |
|||
60 | } |
||||
61 | |||||
62 | |||||
63 | /** |
||||
64 | * Throw an exception if the target class does not also use the rule repository trait. |
||||
65 | * |
||||
66 | * @throws RuleRepositoryTraitNotUsed |
||||
67 | */ |
||||
68 | 5 | private static function checkForRuleRepositoryTrait() |
|||
69 | { |
||||
70 | 5 | $traits = class_uses(static::class); |
|||
71 | |||||
72 | 5 | if ( ! in_array(RuleRepositoryTrait::class, $traits)) { |
|||
73 | 2 | throw new RuleRepositoryTraitNotUsed; |
|||
74 | } |
||||
75 | 3 | } |
|||
76 | } |
||||
77 |