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

keradus / PHP-CS-Fixer / 17319949156

29 Aug 2025 09:20AM UTC coverage: 94.696% (-0.05%) from 94.744%
17319949156

push

github

keradus
CS

28333 of 29920 relevant lines covered (94.7%)

45.63 hits per line

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

95.24
/src/AbstractPhpdocTypesFixer.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;
16

17
use PhpCsFixer\DocBlock\Annotation;
18
use PhpCsFixer\DocBlock\DocBlock;
19
use PhpCsFixer\DocBlock\TypeExpression;
20
use PhpCsFixer\Tokenizer\Token;
21
use PhpCsFixer\Tokenizer\Tokens;
22

23
/**
24
 * This abstract fixer provides a base for fixers to fix types in PHPDoc.
25
 *
26
 * @author Graham Campbell <hello@gjcampbell.co.uk>
27
 *
28
 * @internal
29
 *
30
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
31
 */
32
abstract class AbstractPhpdocTypesFixer extends AbstractFixer
33
{
34
    public function isCandidate(Tokens $tokens): bool
35
    {
36
        return $tokens->isTokenKindFound(\T_DOC_COMMENT);
48✔
37
    }
38

39
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
40
    {
41
        foreach ($tokens as $index => $token) {
47✔
42
            if (!$token->isGivenKind(\T_DOC_COMMENT)) {
47✔
43
                continue;
47✔
44
            }
45

46
            $doc = new DocBlock($token->getContent());
47✔
47
            $annotations = $doc->getAnnotationsOfType(Annotation::TAGS_WITH_TYPES);
47✔
48

49
            if (0 === \count($annotations)) {
47✔
50
                continue;
3✔
51
            }
52

53
            foreach ($annotations as $annotation) {
44✔
54
                $this->fixType($annotation);
44✔
55
            }
56

57
            $tokens[$index] = new Token([\T_DOC_COMMENT, $doc->getContent()]);
44✔
58
        }
59
    }
60

61
    /**
62
     * Actually normalize the given type.
63
     */
64
    abstract protected function normalize(string $type): string;
65

66
    /**
67
     * Fix the type at the given line.
68
     *
69
     * We must be super careful not to modify parts of words.
70
     *
71
     * This will be nicely handled behind the scenes for us by the annotation class.
72
     */
73
    private function fixType(Annotation $annotation): void
74
    {
75
        $typeExpression = $annotation->getTypeExpression();
44✔
76

77
        if (null === $typeExpression) {
44✔
78
            return;
×
79
        }
80

81
        $newTypeExpression = $typeExpression->mapTypes(function (TypeExpression $type) {
44✔
82
            if (!$type->isCompositeType()) {
44✔
83
                $value = $this->normalize($type->toString());
44✔
84

85
                return new TypeExpression($value, null, []);
44✔
86
            }
87

88
            return $type;
15✔
89
        });
44✔
90

91
        $annotation->setTypes([$newTypeExpression->toString()]);
44✔
92
    }
93
}
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