diff --git a/src/Phug/DependencyInjection/DependencyInjection/FunctionWrapper.php b/src/Phug/DependencyInjection/DependencyInjection/FunctionWrapper.php index 027e5421..5bd04496 100644 --- a/src/Phug/DependencyInjection/DependencyInjection/FunctionWrapper.php +++ b/src/Phug/DependencyInjection/DependencyInjection/FunctionWrapper.php @@ -3,6 +3,7 @@ namespace Phug\DependencyInjection; use ReflectionFunction; +use ReflectionParameter; class FunctionWrapper extends ReflectionFunction { @@ -16,18 +17,22 @@ public function dumpParameters() $parameters = []; foreach ($this->getParameters() as $parameter) { $string = ''; - if ($parameter->isArray()) { - $string .= 'array '; - } elseif ($parameter->getClass()) { - $string .= $parameter->getClass()->name.' '; + $type = $this->getTypeAsString($parameter); + + if ($type) { + $string .= "$type "; } + if ($parameter->isPassedByReference()) { $string .= '&'; } + $string .= '$'.$parameter->name; + if ($parameter->isOptional()) { $string .= ' = '.var_export($parameter->getDefaultValue(), true); } + $parameters[] = $string; } @@ -59,4 +64,28 @@ public function dumpBody() return $code; } + + /** + * Return the type as a string in a way compatible from PHP 5.5 to 8.0. + * + * @param ReflectionParameter $parameter + * + * @return string|null + */ + protected function getTypeAsString(ReflectionParameter $parameter) + { + if (method_exists($parameter, 'getType')) { + /** @var mixed $parameter ReflectionParameter has hasType and getType methods since PHP 7.0. */ + + return $parameter->hasType() ? strval($parameter->getType()) : null; + } + + if ($parameter->isArray()) { + return 'array'; + } + + $class = $parameter->getClass(); + + return $class ? $class->name : null; + } } diff --git a/tests/Phug/AbstractCompilerTest.php b/tests/Phug/AbstractCompilerTest.php index fc2eb64c..46306d3c 100644 --- a/tests/Phug/AbstractCompilerTest.php +++ b/tests/Phug/AbstractCompilerTest.php @@ -15,15 +15,16 @@ abstract class AbstractCompilerTest extends TestCase */ protected $compiler; - protected function expectMessageToBeThrown($message) + protected function expectMessageToBeThrown($message, $type = null) { if (method_exists($this, 'expectExceptionMessage')) { $this->expectExceptionMessage($message); + $this->expectException($type ?: Exception::class); return; } - $this->setExpectedException(Exception::class, $message, null); + $this->setExpectedException($type ?: Exception::class, $message, null); } protected function setUp() diff --git a/tests/Phug/AbstractDependencyInjectionTest.php b/tests/Phug/AbstractDependencyInjectionTest.php index fbc09e9f..91acf5e4 100644 --- a/tests/Phug/AbstractDependencyInjectionTest.php +++ b/tests/Phug/AbstractDependencyInjectionTest.php @@ -21,4 +21,20 @@ public static function assertSameLines($expected, $actual) self::assertSame($expected, $actual); } + + protected function expectMessageToBeThrown($type, $message, $code = null) + { + if (method_exists($this, 'expectExceptionMessage')) { + $this->expectException($type); + $this->expectExceptionMessage($message); + + if ($code !== null) { + $this->expectExceptionCode($code); + } + + return; + } + + $this->setExpectedException($type, $message, $code); + } } diff --git a/tests/Phug/Compiler/NodeCompiler/ForNodeCompilerTest.php b/tests/Phug/Compiler/NodeCompiler/ForNodeCompilerTest.php index 62cb86d5..73476094 100644 --- a/tests/Phug/Compiler/NodeCompiler/ForNodeCompilerTest.php +++ b/tests/Phug/Compiler/NodeCompiler/ForNodeCompilerTest.php @@ -4,6 +4,7 @@ use Phug\Compiler; use Phug\Compiler\NodeCompiler\ForNodeCompiler; +use Phug\CompilerException; use Phug\Parser\Node\ElementNode; use Phug\Test\AbstractCompilerTest; @@ -143,14 +144,14 @@ public function testCompile() } /** - * @covers :: - * @expectedException \Phug\CompilerException + * @covers :: */ public function testException() { $this->expectMessageToBeThrown( 'Unexpected Phug\Parser\Node\ElementNode '. - 'given to for compiler.' + 'given to for compiler.', + CompilerException::class ); $forCompiler = new ForNodeCompiler(new Compiler()); diff --git a/tests/Phug/Compiler/NodeCompiler/MixinCallNodeCompilerTest.php b/tests/Phug/Compiler/NodeCompiler/MixinCallNodeCompilerTest.php index f6033c9e..9becafc1 100644 --- a/tests/Phug/Compiler/NodeCompiler/MixinCallNodeCompilerTest.php +++ b/tests/Phug/Compiler/NodeCompiler/MixinCallNodeCompilerTest.php @@ -2,8 +2,10 @@ namespace Phug\Test\Compiler\NodeCompiler; +use InvalidArgumentException; use Phug\Compiler; use Phug\Compiler\NodeCompiler\MixinCallNodeCompiler; +use Phug\CompilerException; use Phug\Parser\Node\ElementNode; use Phug\Test\AbstractCompilerTest; @@ -321,15 +323,16 @@ public function testMixinBlocks() } /** - * @group mixins - * @covers :: - * @expectedException \Phug\CompilerException + * @group mixins + * + * @covers :: */ public function testException() { $this->expectMessageToBeThrown( 'Unexpected Phug\Parser\Node\ElementNode '. - 'given to mixin call compiler.' + 'given to mixin call compiler.', + CompilerException::class, ); $mixinCallCompiler = new MixinCallNodeCompiler(new Compiler()); @@ -337,14 +340,15 @@ public function testException() } /** - * @group mixins - * @covers :: - * @expectedException \InvalidArgumentException + * @group mixins + * + * @covers :: */ public function testUnknownMixin() { $this->expectMessageToBeThrown( - 'Unknown undef mixin called.' + 'Unknown undef mixin called.', + InvalidArgumentException::class ); $php = (new Compiler([ diff --git a/tests/Phug/DependencyInjectionTest.php b/tests/Phug/DependencyInjectionTest.php index c7b17914..7d311b69 100644 --- a/tests/Phug/DependencyInjectionTest.php +++ b/tests/Phug/DependencyInjectionTest.php @@ -2,6 +2,7 @@ namespace Phug\Test; +use Phug\DependencyException; use Phug\DependencyInjection; use Phug\Util\UnorderedArguments; @@ -97,13 +98,16 @@ public function testImport() } /** - * @covers \Phug\DependencyInjection::setAsRequired - * @expectedException \Phug\DependencyException - * @expectedExceptionCode 2 - * @expectedExceptionMessage Dependency not found: baz < bar < foo + * @covers \Phug\DependencyInjection::setAsRequired */ public function testRequiredFailure() { + $this->expectMessageToBeThrown( + DependencyException::class, + 'Dependency not found: baz < bar < foo', + 2 + ); + $injector = new DependencyInjection(); $injector->provider('bar', ['baz', 1]); $injector->provider('foo', ['bar', 2]); @@ -111,13 +115,16 @@ public function testRequiredFailure() } /** - * @covers \Phug\DependencyInjection::getProvider - * @expectedException \Phug\DependencyException - * @expectedExceptionCode 1 - * @expectedExceptionMessage foobar dependency not found. + * @covers \Phug\DependencyInjection::getProvider */ public function testGetProviderException() { + $this->expectMessageToBeThrown( + DependencyException::class, + 'foobar dependency not found.', + 1 + ); + $injector = new DependencyInjection(); $injector->getProvider('foobar'); } @@ -248,13 +255,15 @@ public function testDumpDependency() } /** - * @covers \Phug\DependencyInjection::provider - * @expectedException \Phug\DependencyException - * @expectedExceptionMessage Invalid provider passed to foobar, - * @expectedExceptionMessage it must be an array or a callable function. + * @covers \Phug\DependencyInjection::provider */ public function testProviderException() { + $this->expectMessageToBeThrown( + DependencyException::class, + 'Invalid provider passed to foobar, it must be an array or a callable function.' + ); + $injector = new DependencyInjection(); $injector->provider('foobar', '-'); }