Completed
Push — master ( 956b44...75fb58 )
by Gytis
04:44 queued 01:49
created

AbstractTypeSubject::hasDefinedDocType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
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 DocBlock */
36
    protected $docBlock;
37
38
    /** @var string[] */
39
    protected $docTypeWarnings = [];
40
41
    /** @var string[] */
42
    protected $fnTypeWarnings = [];
43
44 13
    public function __construct(
45
        ?TypeInterface $docType,
46
        TypeInterface $fnType,
47
        ?TypeInterface $valueType,
48
        ?int $docTypeLine,
49
        int $fnTypeLine,
50
        string $name,
51
        DocBlock $docBlock
52
    ) {
53 13
        $this->docType = $docType;
54 13
        $this->fnType = $fnType;
55 13
        $this->valueType = $valueType;
56 13
        $this->docTypeLine = $docTypeLine;
57 13
        $this->fnTypeLine = $fnTypeLine;
58 13
        $this->name = $name;
59 13
        $this->docBlock = $docBlock;
60 13
    }
61
62 13
    public function getDocType(): ?TypeInterface
63
    {
64 13
        return $this->docType;
65
    }
66
67 13
    public function getFnType(): TypeInterface
68
    {
69 13
        return $this->fnType;
70
    }
71
72 13
    public function getValueType(): ?TypeInterface
73
    {
74 13
        return $this->valueType;
75
    }
76
77 1
    public function getDocTypeLine(): ?int
78
    {
79 1
        return $this->docTypeLine;
80
    }
81
82 1
    public function getFnTypeLine(): int
83
    {
84 1
        return $this->fnTypeLine;
85
    }
86
87 1
    public function getName(): string
88
    {
89 1
        return $this->name;
90
    }
91
92 10
    public function getDocBlock(): DocBlock
93
    {
94 10
        return $this->docBlock;
95
    }
96
97
    /**
98
     * @return string[]
99
     */
100 1
    public function getDocTypeWarnings(): array
101
    {
102 1
        return $this->docTypeWarnings;
103
    }
104
105
    /**
106
     * @return string[]
107
     */
108 1
    public function getFnTypeWarnings(): array
109
    {
110 1
        return $this->fnTypeWarnings;
111
    }
112
113 12
    public function hasDefinedDocType(): bool
114
    {
115 12
        return $this->docType && !($this->docType instanceof UndefinedType);
116
    }
117
118 7
    public function hasDefinedFnType(): bool
119
    {
120 7
        return $this->fnType && !($this->fnType instanceof UndefinedType);
121
    }
122
123 12
    public function hasDefinedDocBlock(): bool
124
    {
125 12
        return !($this->docBlock instanceof UndefinedDocBlock);
126
    }
127
128 11
    public function addDocTypeWarning(string $warning): void
129
    {
130 11
        $this->docTypeWarnings[] = $warning;
131 11
    }
132
133 7
    public function addFnTypeWarning(string $warning): void
134
    {
135 7
        $this->fnTypeWarnings[] = $warning;
136 7
    }
137
138
    /**
139
     * @deprecated Use ::writeViolationsTo()
140
     * @param File   $file
141
     * @param string $sniffCode
142
     */
143
    public function writeWarningsTo(File $file, string $sniffCode): void
144
    {
145
        $this->writeViolationsTo($file, $sniffCode, 'warning');
146
    }
147
148 12
    public function writeViolationsTo(File $file, string $sniffCode, string $reportType): void
149
    {
150 12
        $ucName = ucfirst($this->name);
151 12
        foreach ($this->docTypeWarnings as $docTypeWarning) {
152 10
            $warning = str_replace([':subject:', ':Subject:'], [$this->name, $ucName], $docTypeWarning);
153 10
            SniffHelper::addViolation($file, $warning, $this->docTypeLine ?? $this->fnTypeLine, $sniffCode, $reportType);
154
        }
155
156 12
        foreach ($this->fnTypeWarnings as $fnTypeWarning) {
157 6
            $warning = str_replace([':subject:', ':Subject:'], [$this->name, $ucName], $fnTypeWarning);
158 6
            SniffHelper::addViolation($file, $warning, $this->fnTypeLine, $sniffCode, $reportType);
159
        }
160 12
    }
161
162 9
    public function hasDocTypeTag(): bool
163
    {
164 9
        return null !== $this->docType;
165
    }
166
}
167