MockAnnotation   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 86
rs 10
wmc 10

5 Methods

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