Skip to content
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

Open
wants to merge 29 commits into
base: 3.x
Choose a base branch
from

Conversation

smnandre
Copy link
Contributor

@smnandre smnandre commented Jan 5, 2025

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 probably certainely have another look.

Remains:

  • src/ExpressionParser.php, where i'm not confident enough about the best return types (AbstractExpression|null works almost everytime, but lack some precision)
  • extra directory
  • tests directory

I'm happy to let them to someone else.... this was less fun than I expected 😅

@smnandre
Copy link
Contributor Author

smnandre commented Jan 5, 2025

(fabbot unrelated)

@smnandre
Copy link
Contributor Author

smnandre commented Jan 7, 2025

(fixed my mistakes, all seems ok now)

@@ -79,6 +79,9 @@ public function getDynamicName(): string
return $this->dynamicName;
}

/**
* @return callable
Copy link
Member

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)

@@ -117,6 +120,13 @@ public function checkPropertyAllowed($obj, $property, int $lineno = -1, ?Source
}
}

/**
* @param array|object $obj
Copy link
Member

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)

@@ -72,6 +72,9 @@ private function isSourceSandboxed(?Source $source): bool
return $this->sourcePolicy->enableSandbox($source);
}

/**
* @return void
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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}

@smnandre
Copy link
Contributor Author

smnandre commented Jan 8, 2025

Thank you very much @stof! (and sorry i make that many mistakes 😮‍💨 )

I've fixed all your comments

@smnandre smnandre requested a review from stof January 10, 2025 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants