ParamsAnnotation   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getParameters() 0 3 1
A compile() 0 12 3
A getType() 0 3 1
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\Util\Json;
18
use Respect\Validation\Validator;
19
20
/**
21
 * Class ParamsAnnotation.
22
 *
23
 * @author     Paul Thébaud <[email protected]>.
24
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
25
 * @license    https://opensource.org/licenses/MIT The MIT license.
26
 * @link       https://github.com/paul-thebaud/phpunit-generator
27
 * @since      Class available since Release 2.0.0.
28
 */
29
class ParamsAnnotation extends AbstractAnnotation
30
{
31
    /**
32
     * @var string[] $parameters The method call parameters.
33
     */
34
    private $parameters;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getType(): int
40
    {
41
        return AnnotationInterface::TYPE_PARAMS;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function compile(): void
48
    {
49
        // Decode JSON content
50
        try {
51
            $decoded = Json::decode('[' . $this->getStringContent() . ']');
52
        } catch (JsonException $exception) {
53
            throw new AnnotationParseException('"params" annotation content is invalid (invalid JSON content)');
54
        }
55
        if (! Validator::arrayVal()->each(Validator::stringType(), Validator::intType())->validate($decoded)) {
56
            throw new AnnotationParseException('"params" annotation content is invalid (must contains strings only)');
57
        }
58
        $this->parameters = $decoded;
59
    }
60
61
    /**
62
     * @return string[] The constructor parameters.
63
     */
64
    public function getParameters(): array
65
    {
66
        return $this->parameters;
67
    }
68
}
69