• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

keradus / PHP-CS-Fixer / 16999983712

15 Aug 2025 09:42PM UTC coverage: 94.75% (-0.09%) from 94.839%
16999983712

push

github

keradus
ci: more self-fixing checks on lowest/highest PHP

28263 of 29829 relevant lines covered (94.75%)

45.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.79
/src/DocBlock/DocBlock.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of PHP CS Fixer.
7
 *
8
 * (c) Fabien Potencier <fabien@symfony.com>
9
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14

15
namespace PhpCsFixer\DocBlock;
16

17
use PhpCsFixer\Preg;
18
use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceAnalysis;
19
use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceUseAnalysis;
20

21
/**
22
 * This class represents a docblock.
23
 *
24
 * It internally splits it up into "lines" that we can manipulate.
25
 *
26
 * @author Graham Campbell <hello@gjcampbell.co.uk>
27
 */
28
final class DocBlock
29
{
30
    /**
31
     * @var list<Line>
32
     */
33
    private array $lines = [];
34

35
    /**
36
     * @var null|list<Annotation>
37
     */
38
    private ?array $annotations = null;
39

40
    private ?NamespaceAnalysis $namespace;
41

42
    /**
43
     * @var list<NamespaceUseAnalysis>
44
     */
45
    private array $namespaceUses;
46

47
    /**
48
     * @param list<NamespaceUseAnalysis> $namespaceUses
49
     */
50
    public function __construct(string $content, ?NamespaceAnalysis $namespace = null, array $namespaceUses = [])
51
    {
52
        foreach (Preg::split('/([^\n\r]+\R*)/', $content, -1, \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_DELIM_CAPTURE) as $line) {
21✔
53
            $this->lines[] = new Line($line);
20✔
54
        }
55

56
        $this->namespace = $namespace;
21✔
57
        $this->namespaceUses = $namespaceUses;
21✔
58
    }
59

60
    public function __toString(): string
61
    {
62
        return $this->getContent();
1✔
63
    }
64

65
    /**
66
     * Get this docblock's lines.
67
     *
68
     * @return list<Line>
69
     */
70
    public function getLines(): array
71
    {
72
        return $this->lines;
1✔
73
    }
74

75
    /**
76
     * Get a single line.
77
     */
78
    public function getLine(int $pos): ?Line
79
    {
80
        return $this->lines[$pos] ?? null;
6✔
81
    }
82

83
    /**
84
     * Get this docblock's annotations.
85
     *
86
     * @return list<Annotation>
87
     */
88
    public function getAnnotations(): array
89
    {
90
        if (null !== $this->annotations) {
5✔
91
            return $this->annotations;
1✔
92
        }
93

94
        $this->annotations = [];
5✔
95
        $total = \count($this->lines);
5✔
96

97
        for ($index = 0; $index < $total; ++$index) {
5✔
98
            if ($this->lines[$index]->containsATag()) {
5✔
99
                // get all the lines that make up the annotation
100
                $lines = \array_slice($this->lines, $index, $this->findAnnotationLength($index), true);
5✔
101
                \assert([] !== $lines);
5✔
102
                $annotation = new Annotation($lines, $this->namespace, $this->namespaceUses);
5✔
103
                // move the index to the end of the annotation to avoid
104
                // checking it again because we know the lines inside the
105
                // current annotation cannot be part of another annotation
106
                $index = $annotation->getEnd();
5✔
107
                // add the current annotation to the list of annotations
108
                $this->annotations[] = $annotation;
5✔
109
            }
110
        }
111

112
        return $this->annotations;
5✔
113
    }
114

115
    public function isMultiLine(): bool
116
    {
117
        return 1 !== \count($this->lines);
13✔
118
    }
119

120
    /**
121
     * Take a one line doc block, and turn it into a multi line doc block.
122
     */
123
    public function makeMultiLine(string $indent, string $lineEnd): void
124
    {
125
        if ($this->isMultiLine()) {
6✔
126
            return;
3✔
127
        }
128

129
        $lineContent = $this->getSingleLineDocBlockEntry($this->lines[0]);
3✔
130

131
        if ('' === $lineContent) {
3✔
132
            $this->lines = [
×
133
                new Line('/**'.$lineEnd),
×
134
                new Line($indent.' *'.$lineEnd),
×
135
                new Line($indent.' */'),
×
136
            ];
×
137

138
            return;
×
139
        }
140

141
        $this->lines = [
3✔
142
            new Line('/**'.$lineEnd),
3✔
143
            new Line($indent.' * '.$lineContent.$lineEnd),
3✔
144
            new Line($indent.' */'),
3✔
145
        ];
3✔
146
    }
147

148
    public function makeSingleLine(): void
149
    {
150
        if (!$this->isMultiLine()) {
6✔
151
            return;
1✔
152
        }
153

154
        $usefulLines = array_filter(
5✔
155
            $this->lines,
5✔
156
            static fn (Line $line): bool => $line->containsUsefulContent()
5✔
157
        );
5✔
158

159
        if (1 < \count($usefulLines)) {
5✔
160
            return;
1✔
161
        }
162

163
        $lineContent = '';
4✔
164
        if (\count($usefulLines) > 0) {
4✔
165
            $lineContent = $this->getSingleLineDocBlockEntry(array_shift($usefulLines));
3✔
166
        }
167

168
        $this->lines = [new Line('/** '.$lineContent.' */')];
4✔
169
    }
170

171
    public function getAnnotation(int $pos): ?Annotation
172
    {
173
        $annotations = $this->getAnnotations();
1✔
174

175
        return $annotations[$pos] ?? null;
1✔
176
    }
177

178
    /**
179
     * Get specific types of annotations only.
180
     *
181
     * @param list<string>|string $types
182
     *
183
     * @return list<Annotation>
184
     */
185
    public function getAnnotationsOfType($types): array
186
    {
187
        $typesToSearchFor = (array) $types;
4✔
188

189
        $annotations = [];
4✔
190

191
        foreach ($this->getAnnotations() as $annotation) {
4✔
192
            $tagName = $annotation->getTag()->getName();
4✔
193
            if (\in_array($tagName, $typesToSearchFor, true)) {
4✔
194
                $annotations[] = $annotation;
3✔
195
            }
196
        }
197

198
        return $annotations;
4✔
199
    }
200

201
    /**
202
     * Get the actual content of this docblock.
203
     */
204
    public function getContent(): string
205
    {
206
        return implode('', $this->lines);
14✔
207
    }
208

209
    private function findAnnotationLength(int $start): int
210
    {
211
        $index = $start;
5✔
212

213
        while (($line = $this->getLine(++$index)) !== null) {
5✔
214
            if ($line->containsATag()) {
5✔
215
                // we've 100% reached the end of the description if we get here
216
                break;
5✔
217
            }
218

219
            if (!$line->containsUsefulContent()) {
5✔
220
                // if next line is also non-useful, or contains a tag, then we're done here
221
                $next = $this->getLine($index + 1);
5✔
222
                if (null === $next || !$next->containsUsefulContent() || $next->containsATag()) {
5✔
223
                    break;
5✔
224
                }
225
                // otherwise, continue, the annotation must have contained a blank line in its description
226
            }
227
        }
228

229
        return $index - $start;
5✔
230
    }
231

232
    private function getSingleLineDocBlockEntry(Line $line): string
233
    {
234
        $lineString = $line->getContent();
6✔
235

236
        if ('' === $lineString) {
6✔
237
            return $lineString;
×
238
        }
239

240
        $lineString = str_replace('*/', '', $lineString);
6✔
241
        $lineString = trim($lineString);
6✔
242

243
        if (str_starts_with($lineString, '/**')) {
6✔
244
            $lineString = substr($lineString, 3);
3✔
245
        } elseif (str_starts_with($lineString, '*')) {
3✔
246
            $lineString = substr($lineString, 1);
3✔
247
        }
248

249
        return trim($lineString);
6✔
250
    }
251
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc