Completed
Branch 0.16.0-dev (b16ca7)
by Gytis
04:00
created

AbstractTypeSubject::writeWarningsTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Gskema\TypeSniff\Inspection\Subject;
4
5
use Gskema\TypeSniff\Core\DocBlock\DocBlock;
6
use Gskema\TypeSniff\Core\DocBlock\UndefinedDocBlock;
7
use Gskema\TypeSniff\Core\SniffHelper;
8
use Gskema\TypeSniff\Core\Type\Common\UndefinedType;
9
use Gskema\TypeSniff\Core\Type\TypeInterface;
10
use PHP_CodeSniffer\Files\File;
11
12
/**
13
 * @see AbstractTypeSubjectTest
14
 */
15
abstract class AbstractTypeSubject
16
{
17
    /** @var TypeInterface|null */
18
    protected $docType; // null = missing in PHPDoc
19
20
    /** @var TypeInterface */
21
    protected $fnType;
22
23
    /** @var TypeInterface|null */
24
    protected $valueType; // null = could not be detected
25
26
    /** @var int|null */
27
    protected $docTypeLine; // null = missing in PHPDoc
28
29
    /** @var int */
30
    protected $fnTypeLine;
31
32
    /** @var string */
33
    protected $name; // "parameter $param1", "property $prop1", "constant CONST1"
34
35
    /** @var string */
36
    protected $id; // TestClass::method1(), etc.
37
38
    /** @var DocBlock */
39
    protected $docBlock;
40
41
    /** @var string[] */
42
    protected $docTypeWarnings = [];
43
44
    /** @var string[] */
45
    protected $fnTypeWarnings = [];
46
47 14
    public function __construct(
48
        ?TypeInterface $docType,
49
        TypeInterface $fnType,
50
        ?TypeInterface $valueType,
51
        ?int $docTypeLine,
52
        int $fnTypeLine,
53
        string $name,
54
        DocBlock $docBlock,
55
        string $id
56
    ) {
57 14
        $this->docType = $docType;
58 14
        $this->fnType = $fnType;
59 14
        $this->valueType = $valueType;
60 14
        $this->docTypeLine = $docTypeLine;
61 14
        $this->fnTypeLine = $fnTypeLine;
62 14
        $this->name = $name;
63 14
        $this->docBlock = $docBlock;
64 14
        $this->id = $id;
65 14
    }
66
67 14
    public function getDocType(): ?TypeInterface
68
    {
69 14
        return $this->docType;
70
    }
71
72 14
    public function getFnType(): TypeInterface
73
    {
74 14
        return $this->fnType;
75
    }
76
77 14
    public function getValueType(): ?TypeInterface
78
    {
79 14
        return $this->valueType;
80
    }
81
82 1
    public function getDocTypeLine(): ?int
83
    {
84 1
        return $this->docTypeLine;
85
    }
86
87 1
    public function getFnTypeLine(): int
88
    {
89 1
        return $this->fnTypeLine;
90
    }
91
92 1
    public function getName(): string
93
    {
94 1
        return $this->name;
95
    }
96
97 1
    public function getId(): string
98
    {
99 1
        return $this->id;
100
    }
101
102 11
    public function getDocBlock(): DocBlock
103
    {
104 11
        return $this->docBlock;
105
    }
106
107
    /**
108
     * @return string[]
109
     */
110 1
    public function getDocTypeWarnings(): array
111
    {
112 1
        return $this->docTypeWarnings;
113
    }
114
115
    /**
116
     * @return string[]
117
     */
118 1
    public function getFnTypeWarnings(): array
119
    {
120 1
        return $this->fnTypeWarnings;
121
    }
122
123 13
    public function hasDefinedDocType(): bool
124
    {
125 13
        return $this->docType && !($this->docType instanceof UndefinedType);
126
    }
127
128 8
    public function hasDefinedFnType(): bool
129
    {
130 8
        return $this->fnType && !($this->fnType instanceof UndefinedType);
131
    }
132
133 13
    public function hasDefinedDocBlock(): bool
134
    {
135 13
        return !($this->docBlock instanceof UndefinedDocBlock);
136
    }
137
138 12
    public function addDocTypeWarning(string $warning): void
139
    {
140 12
        $this->docTypeWarnings[] = $warning;
141 12
    }
142
143 8
    public function addFnTypeWarning(string $warning): void
144
    {
145 8
        $this->fnTypeWarnings[] = $warning;
146 8
    }
147
148 13
    public function writeViolationsTo(File $file, string $sniffCode, string $reportType, bool $addViolationId): void
149
    {
150 13
        $originId = $addViolationId ? $this->getId() : null;
151
152 13
        $ucName = ucfirst($this->name);
153 13
        foreach ($this->docTypeWarnings as $docTypeWarning) {
154 11
            $warning = str_replace([':subject:', ':Subject:'], [$this->name, $ucName], $docTypeWarning);
155 11
            SniffHelper::addViolation($file, $warning, $this->docTypeLine ?? $this->fnTypeLine, $sniffCode, $reportType, $originId);
156
        }
157
158 13
        foreach ($this->fnTypeWarnings as $fnTypeWarning) {
159 7
            $warning = str_replace([':subject:', ':Subject:'], [$this->name, $ucName], $fnTypeWarning);
160 7
            SniffHelper::addViolation($file, $warning, $this->fnTypeLine, $sniffCode, $reportType, $originId);
161
        }
162 13
    }
163
164 10
    public function hasDocTypeTag(): bool
165
    {
166 10
        return null !== $this->docType;
167
    }
168
}
169