Passed
Push — main ( e2cb9c...09e2a2 )
by mikhail
03:47
created

DocBlockChecker   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 27
eloc 41
c 2
b 2
f 0
dl 0
loc 89
ccs 36
cts 39
cp 0.9231
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A determineMissingContent() 0 19 5
D requiresDocBlock() 0 41 19
A methodRequiresAdditionalDocBlock() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\MissingDocblock;
6
7
use PhpParser\Node;
8
use PhpParser\Node\Stmt\Class_;
9
use PhpParser\Node\Stmt\ClassConst;
10
use PhpParser\Node\Stmt\ClassMethod;
11
use PhpParser\Node\Stmt\Enum_;
12
use PhpParser\Node\Stmt\Function_;
13
use PhpParser\Node\Stmt\Interface_;
14
use PhpParser\Node\Stmt\Property;
15
use PhpParser\Node\Stmt\Trait_;
16
use SavinMikhail\CommentsDensity\DTO\Input\MissingDocblockConfigDTO;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...issingDocblockConfigDTO was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
final class DocBlockChecker
19
{
20
    private const MISSING_DOC = 'missing doc';
21
    private const MISSING_THROWS_TAG = 'missing @throws tag';
22
    private const MISSING_GENERIC = 'missing generic';
23
24
    private bool $needsGeneric = false;
25
    private bool $throwsUncaught = false;
26
27 51
    public function __construct(
28
        private readonly MissingDocblockConfigDTO $config,
29
        private readonly MethodAnalyzer $methodAnalyzer,
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...Docblock\MethodAnalyzer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
    ) {
31 51
    }
32
33 51
    public function requiresDocBlock(Node $node): bool
34
    {
35 51
        if ($node instanceof Class_ && $this->config->class) {
36 18
            return !$node->isAnonymous();
37
        }
38
39 51
        if ($node instanceof Trait_ && $this->config->trait) {
40 1
            return true;
41
        }
42
43 51
        if ($node instanceof Interface_ && $this->config->interface) {
44 1
            return true;
45
        }
46
47 51
        if ($node instanceof Enum_ && $this->config->enum) {
48 2
            return true;
49
        }
50
51 51
        if ($node instanceof Function_ && $this->config->function) {
52 1
            return true;
53
        }
54
55
        if (
56 51
            ($node instanceof ClassMethod || $node instanceof Function_)
57 51
            && $this->config->function
58
        ) {
59 26
            if ($this->config->requireForAllMethods) {
60 6
                return true;
61
            }
62 20
            return $this->methodRequiresAdditionalDocBlock($node);
63
        }
64
65 51
        if ($node instanceof Property && $this->config->property) {
66 5
            return true;
67
        }
68
69 51
        if ($node instanceof ClassConst && $this->config->constant) {
70 1
            return true;
71
        }
72
73 51
        return false;
74
    }
75
76
    /**
77
     * here we want to find methods that have uncaught throw statements or their return type will be better
78
     * described as generic
79
     */
80 20
    private function methodRequiresAdditionalDocBlock(Node $node): bool
81
    {
82 20
        $this->throwsUncaught = $this->methodAnalyzer->methodThrowsUncaughtExceptions($node);
83 20
        $this->needsGeneric = $this->methodAnalyzer->methodNeedsGeneric($node);
84
85 20
        return $this->throwsUncaught || $this->needsGeneric;
86
    }
87
88 31
    public function determineMissingContent(): string
89
    {
90 31
        if ($this->needsGeneric && $this->throwsUncaught) {
91
            $this->needsGeneric = false;
92
            $this->throwsUncaught = false;
93
            return self::MISSING_THROWS_TAG . ' and ' . self::MISSING_GENERIC;
94
        }
95
96 31
        if ($this->needsGeneric) {
97 9
            $this->needsGeneric = false;
98 9
            return self::MISSING_GENERIC;
99
        }
100
101 22
        if ($this->throwsUncaught) {
102 3
            $this->throwsUncaught = false;
103 3
            return self::MISSING_THROWS_TAG;
104
        }
105
106 19
        return self::MISSING_DOC;
107
    }
108
}
109