Passed
Push — main ( ec2ade...f9de31 )
by mikhail
03:35
created

methodRequiresAdditionalDocBlock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 3
c 1
b 1
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 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 51
        if ($node instanceof ClassMethod && $this->config->function) {
56 26
            if ($this->config->requireForAllMethods) {
57 6
                return true;
58
            }
59 20
            return $this->methodRequiresAdditionalDocBlock($node);
60
        }
61
62 51
        if ($node instanceof Property && $this->config->property) {
63 5
            return true;
64
        }
65
66 51
        if ($node instanceof ClassConst && $this->config->constant) {
67 1
            return true;
68
        }
69
70 51
        return false;
71
    }
72
73
    /**
74
     * here we want to find methods that have uncaught throw statements or their return type will be better
75
     * described as generic
76
     */
77 20
    private function methodRequiresAdditionalDocBlock(Node $node): bool
78
    {
79 20
        $this->throwsUncaught = $this->methodAnalyzer->methodThrowsUncaughtExceptions($node);
80 20
        $this->needsGeneric = $this->methodAnalyzer->methodNeedsGeneric($node);
81
82 20
        return $this->throwsUncaught || $this->needsGeneric;
83
    }
84
85 31
    public function determineMissingContent(): string
86
    {
87 31
        if ($this->needsGeneric && $this->throwsUncaught) {
88
            $this->needsGeneric = false;
89
            $this->throwsUncaught = false;
90
            return self::MISSING_THROWS_TAG . ' and ' . self::MISSING_GENERIC;
91
        }
92
93 31
        if ($this->needsGeneric) {
94 9
            $this->needsGeneric = false;
95 9
            return self::MISSING_GENERIC;
96
        }
97
98 22
        if ($this->throwsUncaught) {
99 3
            $this->throwsUncaught = false;
100 3
            return self::MISSING_THROWS_TAG;
101
        }
102
103 19
        return self::MISSING_DOC;
104
    }
105
}
106