Skip to content

Commit

Permalink
Provide a simplified way to get the parameter type string for PHP 7.0+
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed May 15, 2020
1 parent 93146e5 commit cbc3361
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Phug\DependencyInjection;

use ReflectionFunction;
use ReflectionParameter;

class FunctionWrapper extends ReflectionFunction
{
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
5 changes: 3 additions & 2 deletions tests/Phug/AbstractCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 16 additions & 0 deletions tests/Phug/AbstractDependencyInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
7 changes: 4 additions & 3 deletions tests/Phug/Compiler/NodeCompiler/ForNodeCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Phug\Compiler;
use Phug\Compiler\NodeCompiler\ForNodeCompiler;
use Phug\CompilerException;
use Phug\Parser\Node\ElementNode;
use Phug\Test\AbstractCompilerTest;

Expand Down Expand Up @@ -143,14 +144,14 @@ public function testCompile()
}

/**
* @covers ::<public>
* @expectedException \Phug\CompilerException
* @covers ::<public>
*/
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());
Expand Down
20 changes: 12 additions & 8 deletions tests/Phug/Compiler/NodeCompiler/MixinCallNodeCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -321,30 +323,32 @@ public function testMixinBlocks()
}

/**
* @group mixins
* @covers ::<public>
* @expectedException \Phug\CompilerException
* @group mixins
*
* @covers ::<public>
*/
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());
$mixinCallCompiler->compileNode(new ElementNode());
}

/**
* @group mixins
* @covers ::<public>
* @expectedException \InvalidArgumentException
* @group mixins
*
* @covers ::<public>
*/
public function testUnknownMixin()
{
$this->expectMessageToBeThrown(
'Unknown undef mixin called.'
'Unknown undef mixin called.',
InvalidArgumentException::class
);

$php = (new Compiler([
Expand Down
33 changes: 21 additions & 12 deletions tests/Phug/DependencyInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Phug\Test;

use Phug\DependencyException;
use Phug\DependencyInjection;
use Phug\Util\UnorderedArguments;

Expand Down Expand Up @@ -97,27 +98,33 @@ 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]);
$injector->setAsRequired('foo');
}

/**
* @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');
}
Expand Down Expand Up @@ -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', '-');
}
Expand Down

0 comments on commit cbc3361

Please sign in to comment.