Skip to content

Commit 9fcf148

Browse files
committed
refactor: upgrade phpstan level 7
1 parent c6e7408 commit 9fcf148

File tree

8 files changed

+54
-23
lines changed

8 files changed

+54
-23
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
parameters:
2-
level: 6
2+
level: 7
33
paths:
44
- src

src/Main.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private function findEntries(Finder $finder): array
5151
foreach ($finder as $file) {
5252
try {
5353
$reflections = PhpReader::parseFile(
54-
realpath($this->directory),
54+
(string)realpath($this->directory),
5555
$file->getRealPath(),
5656
$this->options
5757
);

src/Php/PhpAccessModifier.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ final class PhpAccessModifier
2424
public function __construct(ClassConst|Property|ClassMethod|Param $stmt)
2525
{
2626
if ($stmt instanceof Param) {
27-
$this->public = boolval($stmt->flags & Class_::MODIFIER_PUBLIC);
28-
$this->protected = boolval($stmt->flags & Class_::MODIFIER_PROTECTED);
29-
$this->private = boolval($stmt->flags & Class_::MODIFIER_PRIVATE);
27+
$this->public = (bool)($stmt->flags & Class_::MODIFIER_PUBLIC);
28+
$this->protected = (bool)($stmt->flags & Class_::MODIFIER_PROTECTED);
29+
$this->private = (bool)($stmt->flags & Class_::MODIFIER_PRIVATE);
3030
} else {
3131
$this->public = $stmt->isPublic();
3232
$this->protected = $stmt->isProtected();
3333
$this->private = $stmt->isPrivate();
34-
$this->static = $stmt->isStatic();
3534
if ($stmt instanceof ClassMethod) {
35+
$this->static = $stmt->isStatic();
3636
$this->abstract = $stmt->isAbstract();
3737
}
3838
}

src/Php/PhpClass.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Smeghead\PhpClassDiagram\Php;
66

7+
use PhpParser\Node;
78
use PhpParser\Node\Name\FullyQualified;
89
use PhpParser\Node\Stmt\{
910
Namespace_,
@@ -21,22 +22,23 @@
2122

2223
final class PhpClass
2324
{
24-
/** @var string[] directory parts */
25+
/** @var list<string> directory parts */
2526
private array $dirs;
2627
private ClassLike $syntax;
27-
/** @var \PhpParser\Node[] */
28+
/** @var list<Node> */
2829
private array $full;
2930

3031
/**
31-
* @param \PhpParser\Node[] $full
32+
* @param list<Node> $full
3233
*/
3334
public function __construct(string $filename, ClassLike $syntax, array $full)
3435
{
3536
$relativePath = dirname($filename);
3637
if ($relativePath === '.') {
3738
$dirs = [];
3839
} else {
39-
$dirs = preg_split('/[\\\\\/]/', $relativePath);
40+
/** @var list<string> $dirs */
41+
$dirs = (array)preg_split('/[\\\\\/]/', $relativePath);
4042
}
4143
$this->dirs = $dirs;
4244
$this->syntax = $syntax;
@@ -124,7 +126,7 @@ public function getUses(): array
124126
}
125127

126128
/**
127-
* @param \PhpParser\Node[] $stmts Stmts
129+
* @param list<Node> $stmts Stmts
128130
* @param PhpType[] $uses
129131
* @return PhpType[]
130132
*/
@@ -180,7 +182,7 @@ public function getExtends(): array
180182
$extends[] = new PhpType(
181183
array_slice($extend->parts, 0, count($extend->parts) - 1),
182184
'',
183-
end($extend->parts)
185+
(string)end($extend->parts)
184186
);
185187
}
186188
}
@@ -191,7 +193,7 @@ public function getExtends(): array
191193
$extends[] = new PhpType(
192194
array_slice($implement->parts, 0, count($implement->parts) - 1),
193195
'',
194-
end($implement->parts)
196+
(string)end($implement->parts)
195197
);
196198
}
197199
}
@@ -218,16 +220,21 @@ public function getEnumCases(): array
218220

219221
public function getDescription(): string
220222
{
221-
$doc = new PhpDocComment($this->syntax);
222-
return $doc->getDescription();
223+
return (new PhpDocComment($this->syntax))
224+
->getDescription();
223225
}
224226

225227
/**
226-
* @return PhpType[] using types.
228+
* @return list<PhpType>
227229
*/
228230
public function getUsingTypes(): array
229231
{
230232
$finder = new FindUsePhpTypes($this->syntax);
231-
return array_map(fn(FullyQualified $x) => new PhpType(array_slice($x->parts, 0, count($x->parts) - 1), '', end($x->parts)), $finder->collectTypes());
233+
return array_map(
234+
fn(FullyQualified $x) => new PhpType(
235+
array_slice($x->parts, 0, count($x->parts) - 1), '', (string)end($x->parts)
236+
),
237+
$finder->collectTypes()
238+
);
232239
}
233240
}

src/Php/PhpMethod.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,16 @@ final class PhpMethod
2222
public function __construct(ClassMethod $method, PhpClass $class)
2323
{
2424
$params = array_map(function (Param $x) use ($class, $method) {
25-
$type = PhpTypeExpression::buildByMethodParam($x, $class->getNamespace(), $method, $x->var->name, $class->getUses());
26-
return new PhpMethodParameter($x->var->name, $type);
25+
/** @var string $varName */
26+
$varName = $x->var->name; /** @phpstan-ignore-line */
27+
$type = PhpTypeExpression::buildByMethodParam(
28+
$x,
29+
$class->getNamespace(),
30+
$method,
31+
$varName,
32+
$class->getUses()
33+
);
34+
return new PhpMethodParameter($varName, $type);
2735
}, $method->getParams());
2836
$this->name = $method->name->toString();
2937
$this->type = $this->getTypeFromMethod($method, $class);

src/Php/PhpProperty.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@ public static function buildByProperty(Property $p, PhpClass $class): self
3232
public static function buildByParam(Param $param, ClassMethod $method, PhpClass $class): self
3333
{
3434
$instance = new self();
35-
$instance->name = $param->var->name;
36-
$instance->type = PhpTypeExpression::buildByMethodParam($param, $class->getNamespace(), $method, $param->var->name, $class->getUses());
35+
/** @var string $varName */
36+
$varName = $param->var->name; // @phpstan-ignore-line
37+
$instance->name = $varName;
38+
$instance->type = PhpTypeExpression::buildByMethodParam(
39+
$param,
40+
$class->getNamespace(),
41+
$method,
42+
$varName,
43+
$class->getUses()
44+
);
3745
$instance->accessModifier = new PhpAccessModifier($param);
3846
return $instance;
3947
}

src/Php/PhpReader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function getInfo(): PhpClass
3131
*/
3232
public static function parseFile(string $directory, string $filename, Options $options): array
3333
{
34-
$code = file_get_contents($filename);
34+
$code = (string)file_get_contents($filename);
3535

3636
$targetVersion = match ($options->phpVersion()) {
3737
'php5' => ParserFactory::PREFER_PHP5,

src/Php/PhpTypeExpression.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,16 @@ private function parseType(Property|Identifier|NullableType|Name|UnionType|Inter
194194
}
195195
}
196196
}
197+
/** @var list<string> $parts */
197198
$typeName = array_pop($parts);
198-
return new PhpType($parts, empty($type) ? '' : $type->getType(), $typeName ?? '', null, $nullable);
199+
200+
return new PhpType(
201+
namespace: $parts,
202+
meta: empty($type) ? '' : $type->getType(),
203+
name: $typeName ?? '',
204+
alias: null,
205+
nullable: $nullable
206+
);
199207
}
200208

201209
/**

0 commit comments

Comments
 (0)