1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBoot\Console\Annotations; |
4
|
|
|
|
5
|
|
|
use PhpBoot\Annotation\AnnotationBlock; |
6
|
|
|
use PhpBoot\Annotation\AnnotationTag; |
7
|
|
|
use PhpBoot\Console\ConsoleContainer; |
8
|
|
|
use PhpBoot\Exceptions\AnnotationSyntaxException; |
9
|
|
|
use PhpBoot\Utils\AnnotationParams; |
10
|
|
|
use PhpBoot\Utils\Logger; |
11
|
|
|
use PhpBoot\Validator\Validator; |
12
|
|
|
|
13
|
|
View Code Duplication |
class ValidateAnnotationHandler |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param ConsoleContainer $container |
17
|
|
|
* @param AnnotationBlock|AnnotationTag $ann |
18
|
|
|
*/ |
19
|
|
|
public function __invoke(ConsoleContainer $container, $ann) |
20
|
|
|
{ |
21
|
|
|
if(!$ann->parent || !$ann->parent->parent){ |
22
|
|
|
Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()} should be used with parent parent"); |
23
|
|
|
return; |
24
|
|
|
} |
25
|
|
|
$target = $ann->parent->parent->name; |
26
|
|
|
$command = $container->getCommand($target); |
27
|
|
|
if(!$command){ |
28
|
|
|
Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()}::$target should be used with parent parent"); |
29
|
|
|
return ; |
30
|
|
|
} |
31
|
|
|
$params = new AnnotationParams($ann->description, 2); |
32
|
|
|
|
33
|
|
|
count($params)>0 or \PhpBoot\abort(new AnnotationSyntaxException("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()}::$target require 1 param, {$params->count()} given")); |
|
|
|
|
34
|
|
|
|
35
|
|
|
if($ann->parent->name == 'param'){ |
36
|
|
|
list($paramType, $paramName, $paramDoc) = ParamAnnotationHandler::getParamInfo($ann->parent->description); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$paramMeta = $command->getParamMeta($paramName); |
39
|
|
|
if($params->count()>1){ |
40
|
|
|
$paramMeta->validation = [$params[0], $params[1]]; |
41
|
|
|
}else{ |
42
|
|
|
$paramMeta->validation = $params[0]; |
43
|
|
|
if($paramMeta->validation) { |
44
|
|
|
$v = new Validator(); |
45
|
|
|
$v->rule($paramMeta->validation, $paramMeta->name); |
46
|
|
|
if ($v->hasRule('optional', $paramMeta->name)) { |
47
|
|
|
$paramMeta->isOptional = true; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()}::$target should be used with parent parent"); |
55
|
|
|
} |
56
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.