Skip to content

Commit 129d0ad

Browse files
committed
Improve exception message and add test
1 parent cca65d8 commit 129d0ad

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/PhpGenerator/ClassLike.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,28 @@ abstract class ClassLike
3939

4040
public static function from(string|object $class, bool $withBodies = false): static
4141
{
42-
$class = (new Factory)
42+
$instance = (new Factory)
4343
->fromClassReflection(new \ReflectionClass($class), $withBodies);
4444

45-
if (!$class instanceof static) {
46-
throw new Nette\InvalidArgumentException("Object '$class' is not an instance of " . static::class);
45+
if (!$instance instanceof static) {
46+
$class = is_object($class) ? get_class($class) : $class;
47+
throw new Nette\InvalidArgumentException("'$class' cannot be represented with " . static::class . ". Call " . get_class($instance) . "::" . __FUNCTION__ . "() or " . __METHOD__ . "() instead.");
4748
}
4849

49-
return $class;
50+
return $instance;
5051
}
5152

5253

5354
public static function fromCode(string $code): static
5455
{
55-
$class = (new Factory)
56+
$instance = (new Factory)
5657
->fromClassCode($code);
5758

58-
if (!$class instanceof static) {
59-
throw new Nette\InvalidArgumentException("Object '$class' is not an instance of " . static::class);
59+
if (!$instance instanceof static) {
60+
throw new Nette\InvalidArgumentException("Provided code cannot be represented with " . static::class . ". Call " . get_class($instance) . "::" . __FUNCTION__ . "() or " . __METHOD__ . "() instead.");
6061
}
6162

62-
return $class;
63+
return $instance;
6364
}
6465

6566

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\PhpGenerator\ClassType;
6+
use Nette\PhpGenerator\EnumType;
7+
use Nette\PhpGenerator\InterfaceType;
8+
use Nette\PhpGenerator\TraitType;
9+
use Tester\Assert;
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
require __DIR__ . '/fixtures/classes.php';
13+
14+
Assert::exception(function () {
15+
ClassType::from(Abc\Interface1::class);
16+
}, Nette\InvalidArgumentException::class, "'Abc\\Interface1' cannot be represented with Nette\\PhpGenerator\\ClassType. Call Nette\\PhpGenerator\\InterfaceType::from() or Nette\\PhpGenerator\\ClassLike::from() instead.");
17+
18+
Assert::exception(function () {
19+
TraitType::from(Abc\Class1::class);
20+
}, Nette\InvalidArgumentException::class, "'Abc\\Class1' cannot be represented with Nette\\PhpGenerator\\TraitType. Call Nette\\PhpGenerator\\ClassType::from() or Nette\\PhpGenerator\\ClassLike::from() instead.");
21+
22+
Assert::exception(function () {
23+
ClassType::fromCode("<?php interface I {}");
24+
}, Nette\InvalidArgumentException::class, "Provided code cannot be represented with Nette\\PhpGenerator\\ClassType. Call Nette\\PhpGenerator\\InterfaceType::fromCode() or Nette\\PhpGenerator\\ClassLike::fromCode() instead.");
25+
26+
Assert::exception(function () {
27+
InterfaceType::fromCode("<?php trait T {}");
28+
}, Nette\InvalidArgumentException::class, "Provided code cannot be represented with Nette\\PhpGenerator\\InterfaceType. Call Nette\\PhpGenerator\\TraitType::fromCode() or Nette\\PhpGenerator\\ClassLike::fromCode() instead.");

0 commit comments

Comments
 (0)