-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add inspection for positional macro arguments after named ones
- Loading branch information
Showing
5 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AlisQI\TwigQI\Inspection; | ||
|
||
use AlisQI\TwigQI\Helper\NodeLocation; | ||
use Twig\Environment; | ||
use Twig\Node\Expression\MacroReferenceExpression; | ||
use Twig\Node\Node; | ||
use Twig\NodeVisitor\NodeVisitorInterface; | ||
|
||
class PositionalMacroArgumentAfterNamed implements NodeVisitorInterface | ||
{ | ||
public function enterNode(Node $node, Environment $env): Node | ||
{ | ||
if ($node instanceof MacroReferenceExpression) { | ||
if (!$this->checkCall($node)) { | ||
trigger_error( | ||
sprintf("Positional macro argument after named (at %s)", new NodeLocation($node)), | ||
E_USER_ERROR | ||
); | ||
} | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
private function checkCall(MacroReferenceExpression $node): bool | ||
{ | ||
$namedArgumentEncountered = false; | ||
foreach ($node->getNode('arguments')->getKeyValuePairs() as ['key' => $key]) { | ||
$name = $key->getAttribute('name'); | ||
|
||
if (is_int($name)) { | ||
if ($namedArgumentEncountered) { | ||
return false; | ||
} | ||
} else { | ||
$namedArgumentEncountered = true; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function leaveNode(Node $node, Environment $env): ?Node | ||
{ | ||
return $node; | ||
} | ||
|
||
public function getPriority(): int | ||
{ | ||
return 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AlisQI\TwigQI\Tests; | ||
|
||
class PositionalMacroArgumentAfterNamedTest extends AbstractTestCase | ||
{ | ||
public function test_itSupportsNamedArguments(): void | ||
{ | ||
$this->env->createTemplate(<<<EOF | ||
{% macro marco(po, lo) %}{% endmacro %} | ||
{{ _self.polo(po=13, lo: 37) }} | ||
EOF); | ||
|
||
self::assertEmpty($this->errors, implode(', ', $this->errors)); | ||
} | ||
|
||
public function test_itErrorsForPositionalArgumentAfterNamed(): void | ||
{ | ||
$this->env->createTemplate(<<<EOF | ||
{% macro marco(po, lo) %}{% endmacro %} | ||
{{ _self.polo(po: 13, 37) }} | ||
EOF); | ||
|
||
self::assertNotEmpty($this->errors); | ||
} | ||
|
||
} |