Skip to content

Commit

Permalink
Merge pull request #1 from Stillat/analysis-EA9779
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
JohnathonKoster authored Oct 23, 2021
2 parents c7b8f08 + 31f4d77 commit e7bb118
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 31 deletions.
7 changes: 3 additions & 4 deletions src/DirectiveContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class DirectiveContainer
{

/**
* The parameters.
*
Expand All @@ -15,7 +14,7 @@ class DirectiveContainer
/**
* The directive's parameters.
*
* @param array $parameters
* @param array $parameters
*/
public function setParameters($parameters)
{
Expand All @@ -25,7 +24,7 @@ public function setParameters($parameters)
/**
* Replaces parameters within the code sample with their PHP equivalents.
*
* @param string $code
* @param string $code
* @return string
*/
public function compile($code)
Expand All @@ -47,4 +46,4 @@ public function compile($code)

return strtr($code, $escaped);
}
}
}
24 changes: 12 additions & 12 deletions src/DirectiveFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
namespace Stillat\BladeDirectives;

use Illuminate\Support\Facades\Blade;
use ReflectionException;
use ReflectionFunction;
use ReflectionParameter;
use ReflectionException;
use Stillat\Primitives\Parser;

class DirectiveFactory
{

/**
* The Parser instance.
*
Expand All @@ -28,8 +27,8 @@ public function __construct(Parser $parser)
*
* Directive handlers will have access to the provided parameters through $this->parameters.
*
* @param string $name
* @param callable $handler
* @param string $name
* @param callable $handler
*/
public function params($name, callable $handler)
{
Expand All @@ -39,8 +38,8 @@ public function params($name, callable $handler)
/**
* Register a handler for a custom Blade directive.
*
* @param string $name
* @param callable $handler
* @param string $name
* @param callable $handler
*/
public function make($name, callable $handler)
{
Expand All @@ -50,8 +49,8 @@ public function make($name, callable $handler)
/**
* Register a handler for a custom compiled Blade directive.
*
* @param string $name
* @param callable $handler
* @param string $name
* @param callable $handler
*/
public function compile($name, $handler)
{
Expand Down Expand Up @@ -83,7 +82,7 @@ private function getFactoryClosure($handler, $compileResult)
$paramCompiler->setParameters($associatedParams);
$handler = $handler->bindTo($paramCompiler);

$result = call_user_func_array($handler, $associatedParams);
$result = call_user_func_array($handler, $associatedParams);

if ($compileResult) {
return $paramCompiler->compile($result);
Expand All @@ -96,9 +95,10 @@ private function getFactoryClosure($handler, $compileResult)
/**
* Associates the reflected parameters with their parsed values.
*
* @param ReflectionParameter[] $directiveParameters
* @param string[] $expressionParameters
* @param ReflectionParameter[] $directiveParameters
* @param string[] $expressionParameters
* @return array
*
* @throws ReflectionException
*/
private function associateParameters($directiveParameters, $expressionParameters)
Expand Down Expand Up @@ -138,4 +138,4 @@ private function associateParameters($directiveParameters, $expressionParameters

return $associatedParameters;
}
}
}
5 changes: 1 addition & 4 deletions src/Support/Facades/Directive.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Support\Facades\Facade;
use Stillat\BladeDirectives\DirectiveFactory;


/**
* @method static void make(string $name, callable $handler)
* @method static void compile(string $name, callable $handler)
Expand All @@ -14,10 +13,8 @@
*/
class Directive extends Facade
{

protected static function getFacadeAccessor()
{
return DirectiveFactory::class;
}

}
}
17 changes: 6 additions & 11 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?php

use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Blade;
use Stillat\BladeDirectives\Support\Facades\Directive;
use Orchestra\Testbench\TestCase;
use Stillat\BladeDirectives\DirectiveContainer;
use Stillat\BladeDirectives\Support\Facades\Directive;

class ParserTest extends TestCase
{

public function test_directives_receive_parameters()
{
Directive::make('test', function ($param1, $param2, $param3, $param4) {
return $param1.'---'.$param2.'---'.$param3.'---'.$param4;
});
Directive::make('test', function ($param1, $param2, $param3, $param4) {
return $param1.'---'.$param2.'---'.$param3.'---'.$param4;
});

$template = <<<'BLADE'
@test('hello', 'world', [1,2,3,'four', 'five' => [1, env('something', 'default')]], 'six')
Expand All @@ -30,7 +29,6 @@ public function test_param_directives_receives_expression_but_has_access_to_para

Directive::params('test', function ($expression) use (&$directiveExpression, &$directiveParams) {
/** @var $this DirectiveContainer */

$directiveExpression = $expression;
$directiveParams = $this->parameters;

Expand Down Expand Up @@ -63,12 +61,10 @@ public function test_directives_can_define_default_values()

$this->assertSame('$varName---((null) ?? (\'1234\'))', $result);


$result = Blade::compileString('@test($varName, $anotherVarName)');

$this->assertSame('$varName---(($anotherVarName) ?? (\'1234\'))', $result);


Directive::make('test', function ($name, $default = '1234', $another = 1234) {
return $name.'---'.$default.'---'.$another;
});
Expand All @@ -77,7 +73,6 @@ public function test_directives_can_define_default_values()

$this->assertSame('$varName---((null) ?? (\'1234\'))---((null) ?? (1234))', $result);


$result = Blade::compileString('@test($varName, "test")');

$this->assertSame('$varName---(("test") ?? (\'1234\'))---((null) ?? (1234))', $result);
Expand All @@ -89,7 +84,7 @@ public function test_directives_can_define_default_values()
public function test_parameters_can_be_compiled()
{
Directive::compile('test', function ($value = []) {
return '<?php foreach ($value as $testVar) {
return '<?php foreach ($value as $testVar) {
echo $testVar;
}';
});
Expand Down

0 comments on commit e7bb118

Please sign in to comment.