Passed
Push — develop ( 4c7678...74dbd7 )
by Paul
02:25
created

MockAnnotation::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PhpUnitGen\Annotation;
4
5
use PhpUnitGen\Annotation\AnnotationInterface\AnnotationInterface;
6
use PhpUnitGen\Exception\AnnotationParseException;
7
use PhpUnitGen\Exception\JsonException;
8
use PhpUnitGen\Model\ModelInterface\FunctionModelInterface;
9
use PhpUnitGen\Parser\NodeParserUtil\RootRetrieverHelper;
10
use PhpUnitGen\Util\Json;
11
use Respect\Validation\Validator;
12
13
/**
14
 * Class MockAnnotation.
15
 *
16
 * @author     Paul Thébaud <[email protected]>.
17
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
18
 * @license    https://opensource.org/licenses/MIT The MIT license.
19
 * @link       https://github.com/paul-thebaud/phpunit-generator
20
 * @since      Class available since Release 2.0.0.
21
 */
22
class MockAnnotation extends AbstractAnnotation
23
{
24
    /**
25
     * @var string $property The name of the property which will contains the mock.
26
     */
27
    private $property;
28
29
    /**
30
     * @var string $class The class to the mock.
31
     */
32
    private $class;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getType(): int
38
    {
39
        return AnnotationInterface::TYPE_MOCK;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function compile(): void
46
    {
47
        // Decode JSON content
48
        try {
49
            $decoded = Json::decode('[' . $this->getStringContent() . ']');
50
        } catch (JsonException $exception) {
51
            throw new AnnotationParseException('"mock" annotation content is invalid (invalid JSON content)');
52
        }
53
        $this->validate($decoded);
54
55
        // Get the last name part
56
        $nameArray = explode('\\', $decoded[0]);
57
        $lastPart  = end($nameArray);
58
        // Add use to PhpFile
59
        RootRetrieverHelper::getRoot($this)->addConcreteUse($decoded[0], $lastPart);
60
        $this->class = $lastPart;
61
        $this->property = $decoded[1];
62
    }
63
64
    /**
65
     * Validate the content of annotation.
66
     *
67
     * @param mixed $decoded The annotation content.
68
     *
69
     * @throws AnnotationParseException If the content is invalid.
70
     */
71
    private function validate($decoded): void
72
    {
73
        // Validate it is an array
74
        if (! Validator::arrayType()->length(2, 2)->validate($decoded)) {
75
            throw new AnnotationParseException(
76
                '"mock" annotation content is invalid (must contains the class to mock and the property name)'
77
            );
78
        }
79
        // Validate that each value is a string
80
        if (! Validator::arrayVal()->each(Validator::stringType(), Validator::intType())->validate($decoded)) {
81
            throw new AnnotationParseException(
82
                '"mock" annotation content is invalid (class and property name must be string)'
83
            );
84
        }
85
    }
86
87
    /**
88
     * @return string The name of the property which will contains the mock.
89
     */
90
    public function getProperty(): string
91
    {
92
        return $this->property;
93
    }
94
95
    /**
96
     * @return string The class to the mock.
97
     */
98
    public function getClass(): string
99
    {
100
        return $this->class;
101
    }
102
}
103