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
Show file tree
Hide file tree
Changes from 2 commits
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
21 changes: 16 additions & 5 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 @@ -37,18 +37,29 @@
private ?PhpNamespace $namespace;
private ?string $name;


public static function from(string|object $class, bool $withBodies = false): self
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): self
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 @@ -61,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
3 changes: 2 additions & 1 deletion tests/PhpGenerator/ClassType.from.82.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
declare(strict_types=1);

use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\TraitType;

require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/fixtures/classes.82.php';

$res[] = ClassType::from(new Abc\Class13);
$res[] = ClassType::from(Abc\Trait13::class);
$res[] = TraitType::from(Abc\Trait13::class);

sameFile(__DIR__ . '/expected/ClassType.from.82.expect', implode("\n", $res));
9 changes: 5 additions & 4 deletions tests/PhpGenerator/ClassType.from.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
declare(strict_types=1);

use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\InterfaceType;
use Nette\PhpGenerator\Factory;

require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/fixtures/classes.php';

$res[] = ClassType::from(Abc\Interface1::class);
$res[] = ClassType::from(Abc\Interface2::class);
$res[] = ClassType::from(Abc\Interface3::class);
$res[] = ClassType::from(Abc\Interface4::class);
$res[] = InterfaceType::from(Abc\Interface1::class);
$res[] = InterfaceType::from(Abc\Interface2::class);
$res[] = InterfaceType::from(Abc\Interface3::class);
$res[] = InterfaceType::from(Abc\Interface4::class);
$res[] = ClassType::from(Abc\Class1::class);
$res[] = ClassType::from(new Abc\Class2);
$obj = new Abc\Class3;
Expand Down
6 changes: 3 additions & 3 deletions tests/PhpGenerator/ClassType.from.trait.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\ClassLike;

require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/fixtures/traits.php';
Expand All @@ -19,11 +19,11 @@ $classes = [
Class5::class,
];

$res = array_map(fn($class) => ClassType::from($class), $classes);
$res = array_map(fn($class) => ClassLike::from($class), $classes);

sameFile(__DIR__ . '/expected/ClassType.from.trait-use.expect', implode("\n", $res));


$res = array_map(fn($class) => ClassType::from($class, withBodies: true), $classes);
$res = array_map(fn($class) => ClassLike::from($class, withBodies: true), $classes);

sameFile(__DIR__ . '/expected/ClassType.from.trait-use.bodies.expect', implode("\n", $res));
Loading