Skip to content

Commit

Permalink
Merge branch '3.x' into 4.x
Browse files Browse the repository at this point in the history
* 3.x:
  Fix CS
  Use [] instead of array() to represent a constant value in compiled code
  Add more coding standard rules
  • Loading branch information
fabpot committed Nov 30, 2024
2 parents f9f9653 + 4451613 commit aaf4a50
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
23 changes: 18 additions & 5 deletions doc/coding_standards.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,34 @@ standards:
[1, 2, 3]
{'name': 'Fabien'}
* Use snake case for all variable names (provided by the application and
created in templates):
* Do not put any spaces before and after ``=`` in macro argument declarations:

.. code-block:: twig
{% set name = 'Fabien' %}
{% set first_name = 'Fabien' %}
{% macro html_input(class="input") %}
* Use snake case for all function/filter/test names:
* Put exactly one space after the ``:`` sign in macro argument declarations:

.. code-block:: twig
{% macro html_input(class: "input") %}
* Use snake case for all variable names (provided by the application and
created in templates), function/filter/test names, argument names and named
arguments:

.. code-block:: twig
{% set name = 'Fabien' %}
{% set first_name = 'Fabien' %}
{{ 'Fabien Potencier'|to_lower_case }}
{{ generate_random_number() }}
{% macro html_input(class_name) %}
{{ html_input(class_name: 'pwd') }}
* Indent your code inside tags (use the same indentation as the one used for
the target language of the rendered template):

Expand Down
4 changes: 2 additions & 2 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function repr($value)
} elseif (\is_bool($value)) {
$this->raw($value ? 'true' : 'false');
} elseif (\is_array($value)) {
$this->raw('array(');
$this->raw('[');
$first = true;
foreach ($value as $key => $v) {
if (!$first) {
Expand All @@ -182,7 +182,7 @@ public function repr($value)
$this->raw(' => ');
$this->repr($v);
}
$this->raw(')');
$this->raw(']');
} else {
$this->string($value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private function triggerPrecedenceDeprecations(AbstractExpression $expr): void
/** @var AbstractExpression $node */
$node = $expr->getNode('node');
foreach ($this->precedenceChanges as $operatorName => $changes) {
if (!in_array($unaryOp, $changes)) {
if (!\in_array($unaryOp, $changes)) {
continue;
}
if ($node->hasAttribute('operator') && $operatorName === $node->getAttribute('operator')) {
Expand Down
4 changes: 2 additions & 2 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -1476,11 +1476,11 @@ public static function enumCases(string $enum): array
public static function enum(string $enum): \UnitEnum
{
if (!enum_exists($enum)) {
throw new RuntimeError(sprintf('"%s" is not an enum.', $enum));
throw new RuntimeError(\sprintf('"%s" is not an enum.', $enum));
}

if (!$cases = $enum::cases()) {
throw new RuntimeError(sprintf('"%s" is an empty enum.', $enum));
throw new RuntimeError(\sprintf('"%s" is an empty enum.', $enum));
}

return $cases[0];
Expand Down
2 changes: 1 addition & 1 deletion src/TokenParser/GuardTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function parse(Token $token): Node
{
$stream = $this->parser->getStream();
$typeToken = $stream->expect(Token::NAME_TYPE);
if (!in_array($typeToken->getValue(), ['function', 'filter', 'test'])) {
if (!\in_array($typeToken->getValue(), ['function', 'filter', 'test'])) {
throw new SyntaxError(\sprintf('Supported guard types are function, filter and test, "%s" given.', $typeToken->getValue()), $typeToken->getLine(), $stream->getSourceContext());
}
$method = 'get'.$typeToken->getValue();
Expand Down
2 changes: 1 addition & 1 deletion src/TokenParser/MacroTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private function parseDefinition(): ArrayExpression
$stream = $this->parser->getStream();
$stream->expect(Token::PUNCTUATION_TYPE, '(', 'A list of arguments must begin with an opening parenthesis');
while (!$stream->test(Token::PUNCTUATION_TYPE, ')')) {
if (count($arguments)) {
if (\count($arguments)) {
$stream->expect(Token::PUNCTUATION_TYPE, ',', 'Arguments must be separated by a comma');

// if the comma above was a trailing comma, early exit the argument parse loop
Expand Down

0 comments on commit aaf4a50

Please sign in to comment.