Skip to content

Commit cc4af30

Browse files
committed
cs
1 parent d4d36c8 commit cc4af30

File tree

3 files changed

+25
-50
lines changed

3 files changed

+25
-50
lines changed

README.md

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,38 @@
1-
# Require Minimal Type Coverage
1+
# Keep Cognitive Complexity Down
22

33
<br>
44

5-
<div align="center">
6-
<img src="/docs/required_type_level.jpg" style="width: 25em" alt="AI abilities sea level rising... as way to rise type coverage for class elements">
7-
</div>
5+
Cognitive complexity tells us, how difficult code is to understand by a reader.
86

9-
<br>
10-
11-
PHPStan uses type declarations to determine the type of variables, properties and other expression. Sometimes it's hard to see what PHPStan errors are the important ones among thousands of others.
12-
13-
Instead of fixing all PHPStan errors at once, we can start with minimal require type coverage.
14-
15-
<br>
16-
17-
What is the type coverage you ask? We have 3 type possible declarations in total here:
7+
**How is cognitive compleixty measured?**
188

199
```php
20-
final class ConferenceFactory
21-
{
22-
private $talkFactory;
23-
24-
public function createConference(array $data)
25-
{
26-
$talks = $this->talkFactory->create($data);
27-
28-
return new Conference($talks);
10+
function sumOfPrimes($max) {
11+
$total = 0;
12+
for ($i = 1; $i < $max; ++$i) { // +1
13+
for ($j = 2; $j < $i; ++$j) { // +2
14+
if ($i % $j === 0) { // +3
15+
continue 2; // +1
16+
}
17+
}
18+
19+
$total += $i;
2920
}
21+
22+
return $total;
3023
}
3124
```
3225

33-
The param type is defined, but property and return types are missing.
34-
35-
* 1 out of 3 = 33 % coverage
36-
37-
How do we get to the 100 %?
38-
39-
```diff
40-
final class ConferenceFactory
41-
{
42-
- private $talkFactory;
43-
+ private TalkFactory $talkFactory;
44-
45-
- public function createConference(array $data)
46-
+ public function createConference(array $data): Conference
47-
{
48-
$talks = $this->talkFactory->create($data);
49-
50-
return new Conference($talks);
51-
}
52-
}
53-
```
26+
This function uses nesting, conditions and continue back and forth. It's hard to read and has complexity of 7.
5427

55-
This technique is very simple and useful to start with even on legacy project. You also know, how high coverage your project has right now.
28+
How to keep it down and what else is included in measurements? Check [Is Your Code Readable By Humans?](https://tomasvotruba.com/blog/2018/05/21/is-your-code-readable-by-humans-cognitive-complexity-tells-you/) post to learn it.
5629

5730
<br>
5831

5932
## Install
6033

6134
```bash
62-
composer require tomasvotruba/type-coverage --dev
35+
composer require tomasvotruba/cognitive-complexity --dev
6336
```
6437

6538
The package is available on PHP 7.2-8.1 versions in tagged releases.
@@ -75,9 +48,7 @@ Enable each item on their own with simple configuration:
7548
```neon
7649
# phpstan.neon
7750
parameters:
78-
type_coverage:
79-
return_type: 50
80-
param_type: 30
81-
property_type: 70
82-
print_suggestions: false
51+
cognitive_complexity:
52+
class: 50
53+
function: 8
8354
```

docs/required_type_level.jpg

-61.2 KB
Binary file not shown.

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ parameters:
2323
excludePaths:
2424
- "*/Fixture/*"
2525
- "*/Source/*"
26+
27+
ignoreErrors:
28+
# skip as always string
29+
- '#Parameter \#1 \$currentWorkingDirectory of class PHPStan\\DependencyInjection\\ContainerFactory constructor expects string, string\|false given#'

0 commit comments

Comments
 (0)