Skip to content

Commit

Permalink
Merge pull request #70 from Chemaclass/refactor/add-phpstan-level-7
Browse files Browse the repository at this point in the history
Upgrade phpstan level 7
  • Loading branch information
smeghead authored Jun 10, 2024
2 parents c6e7408 + 9fcf148 commit be5c491
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 23 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
parameters:
level: 6
level: 7
paths:
- src
2 changes: 1 addition & 1 deletion src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private function findEntries(Finder $finder): array
foreach ($finder as $file) {
try {
$reflections = PhpReader::parseFile(
realpath($this->directory),
(string)realpath($this->directory),
$file->getRealPath(),
$this->options
);
Expand Down
8 changes: 4 additions & 4 deletions src/Php/PhpAccessModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ final class PhpAccessModifier
public function __construct(ClassConst|Property|ClassMethod|Param $stmt)
{
if ($stmt instanceof Param) {
$this->public = boolval($stmt->flags & Class_::MODIFIER_PUBLIC);
$this->protected = boolval($stmt->flags & Class_::MODIFIER_PROTECTED);
$this->private = boolval($stmt->flags & Class_::MODIFIER_PRIVATE);
$this->public = (bool)($stmt->flags & Class_::MODIFIER_PUBLIC);
$this->protected = (bool)($stmt->flags & Class_::MODIFIER_PROTECTED);
$this->private = (bool)($stmt->flags & Class_::MODIFIER_PRIVATE);
} else {
$this->public = $stmt->isPublic();
$this->protected = $stmt->isProtected();
$this->private = $stmt->isPrivate();
$this->static = $stmt->isStatic();
if ($stmt instanceof ClassMethod) {
$this->static = $stmt->isStatic();
$this->abstract = $stmt->isAbstract();
}
}
Expand Down
29 changes: 18 additions & 11 deletions src/Php/PhpClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Smeghead\PhpClassDiagram\Php;

use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\{
Namespace_,
Expand All @@ -21,22 +22,23 @@

final class PhpClass
{
/** @var string[] directory parts */
/** @var list<string> directory parts */
private array $dirs;
private ClassLike $syntax;
/** @var \PhpParser\Node[] */
/** @var list<Node> */
private array $full;

/**
* @param \PhpParser\Node[] $full
* @param list<Node> $full
*/
public function __construct(string $filename, ClassLike $syntax, array $full)
{
$relativePath = dirname($filename);
if ($relativePath === '.') {
$dirs = [];
} else {
$dirs = preg_split('/[\\\\\/]/', $relativePath);
/** @var list<string> $dirs */
$dirs = (array)preg_split('/[\\\\\/]/', $relativePath);
}
$this->dirs = $dirs;
$this->syntax = $syntax;
Expand Down Expand Up @@ -124,7 +126,7 @@ public function getUses(): array
}

/**
* @param \PhpParser\Node[] $stmts Stmts
* @param list<Node> $stmts Stmts
* @param PhpType[] $uses
* @return PhpType[]
*/
Expand Down Expand Up @@ -180,7 +182,7 @@ public function getExtends(): array
$extends[] = new PhpType(
array_slice($extend->parts, 0, count($extend->parts) - 1),
'',
end($extend->parts)
(string)end($extend->parts)
);
}
}
Expand All @@ -191,7 +193,7 @@ public function getExtends(): array
$extends[] = new PhpType(
array_slice($implement->parts, 0, count($implement->parts) - 1),
'',
end($implement->parts)
(string)end($implement->parts)
);
}
}
Expand All @@ -218,16 +220,21 @@ public function getEnumCases(): array

public function getDescription(): string
{
$doc = new PhpDocComment($this->syntax);
return $doc->getDescription();
return (new PhpDocComment($this->syntax))
->getDescription();
}

/**
* @return PhpType[] using types.
* @return list<PhpType>
*/
public function getUsingTypes(): array
{
$finder = new FindUsePhpTypes($this->syntax);
return array_map(fn(FullyQualified $x) => new PhpType(array_slice($x->parts, 0, count($x->parts) - 1), '', end($x->parts)), $finder->collectTypes());
return array_map(
fn(FullyQualified $x) => new PhpType(
array_slice($x->parts, 0, count($x->parts) - 1), '', (string)end($x->parts)
),
$finder->collectTypes()
);
}
}
12 changes: 10 additions & 2 deletions src/Php/PhpMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ final class PhpMethod
public function __construct(ClassMethod $method, PhpClass $class)
{
$params = array_map(function (Param $x) use ($class, $method) {
$type = PhpTypeExpression::buildByMethodParam($x, $class->getNamespace(), $method, $x->var->name, $class->getUses());
return new PhpMethodParameter($x->var->name, $type);
/** @var string $varName */
$varName = $x->var->name; /** @phpstan-ignore-line */
$type = PhpTypeExpression::buildByMethodParam(
$x,
$class->getNamespace(),
$method,
$varName,
$class->getUses()
);
return new PhpMethodParameter($varName, $type);
}, $method->getParams());
$this->name = $method->name->toString();
$this->type = $this->getTypeFromMethod($method, $class);
Expand Down
12 changes: 10 additions & 2 deletions src/Php/PhpProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ public static function buildByProperty(Property $p, PhpClass $class): self
public static function buildByParam(Param $param, ClassMethod $method, PhpClass $class): self
{
$instance = new self();
$instance->name = $param->var->name;
$instance->type = PhpTypeExpression::buildByMethodParam($param, $class->getNamespace(), $method, $param->var->name, $class->getUses());
/** @var string $varName */
$varName = $param->var->name; // @phpstan-ignore-line
$instance->name = $varName;
$instance->type = PhpTypeExpression::buildByMethodParam(
$param,
$class->getNamespace(),
$method,
$varName,
$class->getUses()
);
$instance->accessModifier = new PhpAccessModifier($param);
return $instance;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Php/PhpReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function getInfo(): PhpClass
*/
public static function parseFile(string $directory, string $filename, Options $options): array
{
$code = file_get_contents($filename);
$code = (string)file_get_contents($filename);

$targetVersion = match ($options->phpVersion()) {
'php5' => ParserFactory::PREFER_PHP5,
Expand Down
10 changes: 9 additions & 1 deletion src/Php/PhpTypeExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,16 @@ private function parseType(Property|Identifier|NullableType|Name|UnionType|Inter
}
}
}
/** @var list<string> $parts */
$typeName = array_pop($parts);
return new PhpType($parts, empty($type) ? '' : $type->getType(), $typeName ?? '', null, $nullable);

return new PhpType(
namespace: $parts,
meta: empty($type) ? '' : $type->getType(),
name: $typeName ?? '',
alias: null,
nullable: $nullable
);
}

/**
Expand Down

0 comments on commit be5c491

Please sign in to comment.