Completed
Push — develop ( 10d533...fd1d54 )
by Paul
02:00
created

SetAnnotation   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getProperty() 0 3 1
A getType() 0 3 1
A compile() 0 18 4
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\Util\Json;
9
10
/**
11
 * Class SetAnnotation.
12
 *
13
 * @author     Paul Thébaud <[email protected]>.
14
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
15
 * @license    https://opensource.org/licenses/MIT The MIT license.
16
 * @link       https://github.com/paul-thebaud/phpunit-generator
17
 * @since      Class available since Release 2.0.0.
18
 */
19
class SetAnnotation extends AbstractAnnotation
20
{
21
    /**
22
     * @var string $property The name of the property to get.
23
     */
24
    private $property;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getType(): int
30
    {
31
        return AnnotationInterface::TYPE_SET;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function compile(): void
38
    {
39
        if (strlen($this->getStringContent()) > 0) {
40
            // Decode JSON content
41
            try {
42
                $decoded = Json::decode($this->getStringContent());
43
            } catch (JsonException $exception) {
44
                throw new AnnotationParseException('"setter" annotation content is invalid (invalid JSON content)');
45
            }
46
            if (! is_string($decoded)) {
47
                throw new AnnotationParseException(
48
                    '"setter" annotation content is invalid (property name must be a string)'
49
                );
50
            }
51
            $this->property = $decoded;
52
        } else {
53
            $this->property = preg_replace('/^set/', '', $this->getParentNode()->getName());
0 ignored issues
show
Bug introduced by
The method getName() does not exist on PhpUnitGen\Model\PropertyInterface\NodeInterface. It seems like you code against a sub-type of said class. However, the method does not exist in PhpUnitGen\Model\Propert...face\ClassLikeInterface or PhpUnitGen\Model\Propert...\DocumentationInterface or PhpUnitGen\Model\PropertyInterface\TypeInterface or PhpUnitGen\Model\Propert...face\ClassLikeInterface or PhpUnitGen\Model\ModelIn...ce\ReturnModelInterface or PhpUnitGen\Model\ReturnModel. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
            $this->property = preg_replace('/^set/', '', $this->getParentNode()->/** @scrutinizer ignore-call */ getName());
Loading history...
54
            $this->property = lcfirst($this->property);
55
        }
56
    }
57
58
    /**
59
     * @return string The name of the property to get.
60
     */
61
    public function getProperty(): string
62
    {
63
        return $this->property;
64
    }
65
}
66