Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ClassLike::from return type assert the subclass type #154

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/PhpGenerator/ClassLike.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check failure on line 1 in src/PhpGenerator/ClassLike.php

View workflow job for this annotation

GitHub Actions / PHPStan

Ignored error pattern #^Match expression does not handle remaining value\: true$# in path /home/runner/work/php-generator/php-generator/src/PhpGenerator/ClassLike.php was not matched in reported errors.

/**
* This file is part of the Nette Framework (https://nette.org)
Expand Down Expand Up @@ -39,15 +39,27 @@

public static function from(string|object $class, bool $withBodies = false): static
{
return (new Factory)
$class = (new Factory)
->fromClassReflection(new \ReflectionClass($class), $withBodies);

if (!$class instanceof static) {
throw new Nette\InvalidArgumentException("Object '$class' is not an instance of " . static::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message should be different so that the user understands what he is doing wrong. And please create a test case for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I'm thinking of a message that provides better guidance:

"Trait1" is not a "ClassType". Use TraitType::from() or ClassLike::from() instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test added and exception message improved. Is that all right?

}

return $class;
}


public static function fromCode(string $code): static
{
return (new Factory)
$class = (new Factory)
->fromClassCode($code);

if (!$class instanceof static) {
throw new Nette\InvalidArgumentException("Object '$class' is not an instance of " . static::class);
}

return $class;
}


Expand All @@ -60,7 +72,7 @@

public function __toString(): string
{
return (new Printer)->printClass($this, $this->namespace);

Check failure on line 75 in src/PhpGenerator/ClassLike.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $class of method Nette\PhpGenerator\Printer::printClass() expects Nette\PhpGenerator\ClassType|Nette\PhpGenerator\EnumType|Nette\PhpGenerator\InterfaceType|Nette\PhpGenerator\TraitType, $this(Nette\PhpGenerator\ClassLike) given.
}


Expand Down
Loading