Skip to content

Commit

Permalink
Improve exception message and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Mar 7, 2024
1 parent cca65d8 commit 129d0ad
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/PhpGenerator/ClassLike.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,28 @@ abstract class ClassLike

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

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

return $class;
return $instance;
}


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

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

return $class;
return $instance;
}


Expand Down
28 changes: 28 additions & 0 deletions tests/PhpGenerator/ClassLike.typecheck.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\EnumType;
use Nette\PhpGenerator\InterfaceType;
use Nette\PhpGenerator\TraitType;
use Tester\Assert;

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

Assert::exception(function () {
ClassType::from(Abc\Interface1::class);
}, Nette\InvalidArgumentException::class, "'Abc\\Interface1' cannot be represented with Nette\\PhpGenerator\\ClassType. Call Nette\\PhpGenerator\\InterfaceType::from() or Nette\\PhpGenerator\\ClassLike::from() instead.");

Assert::exception(function () {
TraitType::from(Abc\Class1::class);
}, Nette\InvalidArgumentException::class, "'Abc\\Class1' cannot be represented with Nette\\PhpGenerator\\TraitType. Call Nette\\PhpGenerator\\ClassType::from() or Nette\\PhpGenerator\\ClassLike::from() instead.");

Assert::exception(function () {
ClassType::fromCode("<?php interface I {}");
}, Nette\InvalidArgumentException::class, "Provided code cannot be represented with Nette\\PhpGenerator\\ClassType. Call Nette\\PhpGenerator\\InterfaceType::fromCode() or Nette\\PhpGenerator\\ClassLike::fromCode() instead.");

Assert::exception(function () {
InterfaceType::fromCode("<?php trait T {}");
}, Nette\InvalidArgumentException::class, "Provided code cannot be represented with Nette\\PhpGenerator\\InterfaceType. Call Nette\\PhpGenerator\\TraitType::fromCode() or Nette\\PhpGenerator\\ClassLike::fromCode() instead.");

0 comments on commit 129d0ad

Please sign in to comment.