-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add return type annotations on Twig 3 to prepare Twig 4 #4534
base: 3.x
Are you sure you want to change the base?
Conversation
# Conflicts: # src/Node/ModuleNode.php
SandboxExtension Add return type on public methods
(fabbot unrelated) |
(fixed my mistakes, all seems ok now) |
src/AbstractTwigCallable.php
Outdated
@@ -79,6 +79,9 @@ public function getDynamicName(): string | |||
return $this->dynamicName; | |||
} | |||
|
|||
/** | |||
* @return callable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong. The callable of a AbstractTwigCallable might not be a PHP callable, due to the case of Twig runtimes.
See the constructor parameter type (on child classes, as it is missing on the constructor of that class currently)
src/Extension/SandboxExtension.php
Outdated
@@ -117,6 +120,13 @@ public function checkPropertyAllowed($obj, $property, int $lineno = -1, ?Source | |||
} | |||
} | |||
|
|||
/** | |||
* @param array|object $obj |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function actually supports mixed
as input (and as output)
src/Extension/SandboxExtension.php
Outdated
@@ -72,6 +72,9 @@ private function isSourceSandboxed(?Source $source): bool | |||
return $this->sourcePolicy->enableSandbox($source); | |||
} | |||
|
|||
/** | |||
* @return void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this class is final, the return type can be added as a native one directly.
src/Markup.php
Outdated
@@ -27,6 +27,9 @@ public function __construct($content, $charset) | |||
$this->charset = $charset; | |||
} | |||
|
|||
/** | |||
* @return string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this type can be added directly (__toString
is special in PHP and has an implicit string
return type)
@@ -157,7 +157,7 @@ private function isSafeFor(string $type, AbstractExpression $expression, Environ | |||
return \in_array($type, $safe) || \in_array('all', $safe); | |||
} | |||
|
|||
private function needEscaping() | |||
private function needEscaping(): string|bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string|false
actually. true
is not a valid strategy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"true" will wait Twig4 / PHP 8.2 no ?
@@ -33,6 +33,9 @@ public function getClassName(): string | |||
return $this->className; | |||
} | |||
|
|||
/** | |||
* @return string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be a native one as the class is final
src/Token.php
Outdated
@@ -80,6 +80,9 @@ public function getType(): int | |||
return $this->type; | |||
} | |||
|
|||
/** | |||
* @return string|int|float|bool|null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong if the constructor does not have a parameter type restricting the type of the value.
src/Parser.php
Outdated
@@ -307,6 +313,9 @@ public function addImportedSymbol(string $type, string $alias, ?string $name = n | |||
$this->importedSymbols[0][$type][$alias] = ['name' => $name, 'node' => $internalRef]; | |||
} | |||
|
|||
/** | |||
* @return string|null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong. It returns array{name: string, node: AssignTemplateVariable|null}
Thank you very much @stof! (and sorry i make that many mistakes 😮💨 ) I've fixed all your comments |
Started to add missing type annotation following @stof comment here.
Update: I checked here every class (but one) in the
src
directory.But you should
probablycertainely have another look.Remains:
I'm happy to let them to someone else.... this was less fun than I expected 😅