AssertAnnotation   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getExpected() 0 3 1
A getType() 0 3 1
A compile() 0 15 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\Util\Json;
18
19
/**
20
 * Class AssertAnnotation.
21
 *
22
 * @author     Paul Thébaud <[email protected]>.
23
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
24
 * @license    https://opensource.org/licenses/MIT The MIT license.
25
 * @link       https://github.com/paul-thebaud/phpunit-generator
26
 * @since      Class available since Release 2.0.0.
27
 */
28
class AssertAnnotation extends AbstractAnnotation
29
{
30
    /**
31
     * @var string|null $expected The expected value, null if none.
32
     */
33
    private $expected;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getType(): int
39
    {
40
        return AnnotationInterface::TYPE_ASSERT;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function compile(): void
47
    {
48
        if (strlen($this->getStringContent()) > 0) {
49
            // Decode JSON content
50
            try {
51
                $decoded = Json::decode($this->getStringContent());
52
            } catch (JsonException $exception) {
53
                throw new AnnotationParseException('"assertion" annotation content is invalid (invalid JSON content)');
54
            }
55
            if (! is_string($decoded)) {
56
                throw new AnnotationParseException(
57
                    '"assertion" annotation content is invalid (expected value must be a string)'
58
                );
59
            }
60
            $this->expected = $decoded;
61
        }
62
    }
63
64
    /**
65
     * @return string The expected value, null if none.
66
     */
67
    public function getExpected(): ?string
68
    {
69
        return $this->expected;
70
    }
71
}
72