Skip to content

Commit

Permalink
minor #4250 Add return type isTraitable (ruudk)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Add return type `isTraitable`

I'd like to do this for more methods that are exposed in the compiled code. This will ease static analysis as there are less errors that are coming from Twig.

This is just a first small PR to see if this is acceptable 😊

Commits
-------

bb8b9c1 Add return type `isTraitable`
  • Loading branch information
fabpot committed Aug 29, 2024
2 parents fecfbd8 + bb8b9c1 commit 2d347b4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 44 deletions.
21 changes: 13 additions & 8 deletions src/Node/ModuleNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected function compileGetParent(Compiler $compiler)
$parent = $this->getNode('parent');

$compiler
->write("protected function doGetParent(array \$context)\n", "{\n")
->write("protected function doGetParent(array \$context): bool|string|Template|TemplateWrapper\n", "{\n")
->indent()
->addDebugInfo($parent)
->write('return ')
Expand Down Expand Up @@ -160,7 +160,9 @@ protected function compileClassHeader(Compiler $compiler)
->write("use Twig\Sandbox\SecurityNotAllowedFilterError;\n")
->write("use Twig\Sandbox\SecurityNotAllowedFunctionError;\n")
->write("use Twig\Source;\n")
->write("use Twig\Template;\n\n")
->write("use Twig\Template;\n")
->write("use Twig\TemplateWrapper;\n")
->write("\n")
;
}
$compiler
Expand All @@ -170,8 +172,11 @@ protected function compileClassHeader(Compiler $compiler)
->raw(" extends Template\n")
->write("{\n")
->indent()
->write("private \$source;\n")
->write("private \$macros = [];\n\n")
->write("private Source \$source;\n")
->write("/**\n")
->write(" * @var array<string, Template>\n")
->write(" */\n")
->write("private array \$macros = [];\n\n")
;
}

Expand Down Expand Up @@ -377,7 +382,7 @@ protected function compileGetTemplateName(Compiler $compiler)
->write("/**\n")
->write(" * @codeCoverageIgnore\n")
->write(" */\n")
->write("public function getTemplateName()\n", "{\n")
->write("public function getTemplateName(): string\n", "{\n")
->indent()
->write('return ')
->repr($this->getSourceContext()->getName())
Expand Down Expand Up @@ -434,7 +439,7 @@ protected function compileIsTraitable(Compiler $compiler)
->write("/**\n")
->write(" * @codeCoverageIgnore\n")
->write(" */\n")
->write("public function isTraitable()\n", "{\n")
->write("public function isTraitable(): bool\n", "{\n")
->indent()
->write("return false;\n")
->outdent()
Expand All @@ -448,7 +453,7 @@ protected function compileDebugInfo(Compiler $compiler)
->write("/**\n")
->write(" * @codeCoverageIgnore\n")
->write(" */\n")
->write("public function getDebugInfo()\n", "{\n")
->write("public function getDebugInfo(): array\n", "{\n")
->indent()
->write(\sprintf("return %s;\n", str_replace("\n", '', var_export(array_reverse($compiler->getDebugInfo(), true), true))))
->outdent()
Expand All @@ -459,7 +464,7 @@ protected function compileDebugInfo(Compiler $compiler)
protected function compileGetSourceContext(Compiler $compiler)
{
$compiler
->write("public function getSourceContext()\n", "{\n")
->write("public function getSourceContext(): Source\n", "{\n")
->indent()
->write('return new Source(')
->string($compiler->getEnvironment()->isDebug() ? $this->getSourceContext()->getCode() : '')
Expand Down
16 changes: 6 additions & 10 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,20 @@ public function __construct(Environment $env)

/**
* Returns the template name.
*
* @return string The template name
*/
abstract public function getTemplateName();
abstract public function getTemplateName(): string;

/**
* Returns debug information about the template.
*
* @return array Debug information
* @return array<int, int> Debug information
*/
abstract public function getDebugInfo();
abstract public function getDebugInfo(): array;

/**
* Returns information about the original template source code.
*
* @return Source
*/
abstract public function getSourceContext();
abstract public function getSourceContext(): Source;

/**
* Returns the parent template.
Expand Down Expand Up @@ -107,12 +103,12 @@ public function getParent(array $context)
return $this->parents[$parent];
}

protected function doGetParent(array $context)
protected function doGetParent(array $context): bool|string|self|TemplateWrapper
{
return false;
}

public function isTraitable()
public function isTraitable(): bool
{
return true;
}
Expand Down
58 changes: 36 additions & 22 deletions tests/Node/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use Twig\Node\SetNode;
use Twig\Node\TextNode;
use Twig\Source;
use Twig\Template;
use Twig\TemplateWrapper;
use Twig\Test\NodeTestCase;

class ModuleTest extends NodeTestCase
Expand Down Expand Up @@ -73,12 +75,16 @@ public function getTests()
use Twig\Sandbox\SecurityNotAllowedFunctionError;
use Twig\Source;
use Twig\Template;
use Twig\TemplateWrapper;
/* foo.twig */
class __TwigTemplate_%x extends Template
{
private \$source;
private \$macros = [];
private Source \$source;
/**
* @var array<string, Template>
*/
private array \$macros = [];
public function __construct(Environment \$env)
{
Expand All @@ -103,20 +109,20 @@ protected function doDisplay(array \$context, array \$blocks = [])
/**
* @codeCoverageIgnore
*/
public function getTemplateName()
public function getTemplateName(): string
{
return "foo.twig";
}
/**
* @codeCoverageIgnore
*/
public function getDebugInfo()
public function getDebugInfo(): array
{
return array ( 38 => 1,);
return array ( 42 => 1,);
}
public function getSourceContext()
public function getSourceContext(): Source
{
return new Source("", "foo.twig", "");
}
Expand Down Expand Up @@ -145,12 +151,16 @@ public function getSourceContext()
use Twig\Sandbox\SecurityNotAllowedFunctionError;
use Twig\Source;
use Twig\Template;
use Twig\TemplateWrapper;
/* foo.twig */
class __TwigTemplate_%x extends Template
{
private \$source;
private \$macros = [];
private Source \$source;
/**
* @var array<string, Template>
*/
private array \$macros = [];
public function __construct(Environment \$env)
{
Expand All @@ -162,7 +172,7 @@ public function __construct(Environment \$env)
];
}
protected function doGetParent(array \$context)
protected function doGetParent(array \$context): bool|string|Template|TemplateWrapper
{
// line 1
return "layout.twig";
Expand All @@ -181,28 +191,28 @@ protected function doDisplay(array \$context, array \$blocks = [])
/**
* @codeCoverageIgnore
*/
public function getTemplateName()
public function getTemplateName(): string
{
return "foo.twig";
}
/**
* @codeCoverageIgnore
*/
public function isTraitable()
public function isTraitable(): bool
{
return false;
}
/**
* @codeCoverageIgnore
*/
public function getDebugInfo()
public function getDebugInfo(): array
{
return array ( 44 => 1, 42 => 2, 35 => 1,);
return array ( 48 => 1, 46 => 2, 39 => 1,);
}
public function getSourceContext()
public function getSourceContext(): Source
{
return new Source("", "foo.twig", "");
}
Expand Down Expand Up @@ -236,12 +246,16 @@ public function getSourceContext()
use Twig\Sandbox\SecurityNotAllowedFunctionError;
use Twig\Source;
use Twig\Template;
use Twig\TemplateWrapper;
/* foo.twig */
class __TwigTemplate_%x extends Template
{
private \$source;
private \$macros = [];
private Source \$source;
/**
* @var array<string, Template>
*/
private array \$macros = [];
public function __construct(Environment \$env)
{
Expand All @@ -253,7 +267,7 @@ public function __construct(Environment \$env)
];
}
protected function doGetParent(array \$context)
protected function doGetParent(array \$context): bool|string|Template|TemplateWrapper
{
// line 2
return \$this->loadTemplate(((true) ? ("foo") : ("foo")), "foo.twig", 2);
Expand All @@ -271,28 +285,28 @@ protected function doDisplay(array \$context, array \$blocks = [])
/**
* @codeCoverageIgnore
*/
public function getTemplateName()
public function getTemplateName(): string
{
return "foo.twig";
}
/**
* @codeCoverageIgnore
*/
public function isTraitable()
public function isTraitable(): bool
{
return false;
}
/**
* @codeCoverageIgnore
*/
public function getDebugInfo()
public function getDebugInfo(): array
{
return array ( 44 => 2, 42 => 4, 35 => 2,);
return array ( 48 => 2, 46 => 4, 39 => 2,);
}
public function getSourceContext()
public function getSourceContext(): Source
{
return new Source("{{ foo }}", "foo.twig", "");
}
Expand Down
9 changes: 5 additions & 4 deletions tests/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Twig\Sandbox\SecurityPolicy;
use Twig\Source;
use Twig\Template;
use Twig\TemplateWrapper;

class TemplateTest extends TestCase
{
Expand Down Expand Up @@ -443,22 +444,22 @@ public function getTrue()
return true;
}

public function getTemplateName()
public function getTemplateName(): string
{
return $this->name;
}

public function getDebugInfo()
public function getDebugInfo() : array
{
return [];
}

public function getSourceContext()
public function getSourceContext() : Source
{
return new Source('', $this->getTemplateName());
}

protected function doGetParent(array $context)
protected function doGetParent(array $context): bool|string|Template|TemplateWrapper
{
return false;
}
Expand Down

0 comments on commit 2d347b4

Please sign in to comment.