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
  Remove redundant "$thing instanceof \Countable" check
  Update PHPUnit schema
  fix TwigExtraBundle tests with Symfony 6.0/6.1
  • Loading branch information
fabpot committed Feb 10, 2024
2 parents f987153 + 7f5958c commit ae7e6cc
Show file tree
Hide file tree
Showing 42 changed files with 79 additions and 73 deletions.
2 changes: 1 addition & 1 deletion extra/cache-extra/Tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testTagsTooManyArgs()
$twig->render('index');
}

private function createEnvironment(array $templates, ArrayAdapter $cache = null): Environment
private function createEnvironment(array $templates, ?ArrayAdapter $cache = null): Environment
{
$twig = new Environment(new ArrayLoader($templates));
$cache = $cache ?? new ArrayAdapter();
Expand Down
4 changes: 2 additions & 2 deletions extra/html-extra/HtmlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class HtmlExtension extends AbstractExtension
{
private $mimeTypes;

public function __construct(MimeTypes $mimeTypes = null)
public function __construct(?MimeTypes $mimeTypes = null)
{
$this->mimeTypes = $mimeTypes;
}
Expand Down Expand Up @@ -50,7 +50,7 @@ public function getFunctions(): array
*
* @internal
*/
public function dataUri(string $data, string $mime = null, array $parameters = []): string
public function dataUri(string $data, ?string $mime = null, array $parameters = []): string
{
$repr = 'data:';

Expand Down
38 changes: 19 additions & 19 deletions extra/intl-extra/IntlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private static function availableDateFormats(): array
private $dateFormatterPrototype;
private $numberFormatterPrototype;

public function __construct(\IntlDateFormatter $dateFormatterPrototype = null, \NumberFormatter $numberFormatterPrototype = null)
public function __construct(?\IntlDateFormatter $dateFormatterPrototype = null, ?\NumberFormatter $numberFormatterPrototype = null)
{
$this->dateFormatterPrototype = $dateFormatterPrototype;
$this->numberFormatterPrototype = $numberFormatterPrototype;
Expand Down Expand Up @@ -191,7 +191,7 @@ public function getFunctions()
];
}

public function getCountryName(?string $country, string $locale = null): string
public function getCountryName(?string $country, ?string $locale = null): string
{
if (null === $country) {
return '';
Expand All @@ -204,7 +204,7 @@ public function getCountryName(?string $country, string $locale = null): string
}
}

public function getCurrencyName(?string $currency, string $locale = null): string
public function getCurrencyName(?string $currency, ?string $locale = null): string
{
if (null === $currency) {
return '';
Expand All @@ -217,7 +217,7 @@ public function getCurrencyName(?string $currency, string $locale = null): strin
}
}

public function getCurrencySymbol(?string $currency, string $locale = null): string
public function getCurrencySymbol(?string $currency, ?string $locale = null): string
{
if (null === $currency) {
return '';
Expand All @@ -230,7 +230,7 @@ public function getCurrencySymbol(?string $currency, string $locale = null): str
}
}

public function getLanguageName(?string $language, string $locale = null): string
public function getLanguageName(?string $language, ?string $locale = null): string
{
if (null === $language) {
return '';
Expand All @@ -243,7 +243,7 @@ public function getLanguageName(?string $language, string $locale = null): strin
}
}

public function getLocaleName(?string $data, string $locale = null): string
public function getLocaleName(?string $data, ?string $locale = null): string
{
if (null === $data) {
return '';
Expand All @@ -256,7 +256,7 @@ public function getLocaleName(?string $data, string $locale = null): string
}
}

public function getTimezoneName(?string $timezone, string $locale = null): string
public function getTimezoneName(?string $timezone, ?string $locale = null): string
{
if (null === $timezone) {
return '';
Expand All @@ -278,7 +278,7 @@ public function getCountryTimezones(string $country): array
}
}

public function getLanguageNames(string $locale = null): array
public function getLanguageNames(?string $locale = null): array
{
try {
return Languages::getNames($locale);
Expand All @@ -287,7 +287,7 @@ public function getLanguageNames(string $locale = null): array
}
}

public function getScriptNames(string $locale = null): array
public function getScriptNames(?string $locale = null): array
{
try {
return Scripts::getNames($locale);
Expand All @@ -296,7 +296,7 @@ public function getScriptNames(string $locale = null): array
}
}

public function getCountryNames(string $locale = null): array
public function getCountryNames(?string $locale = null): array
{
try {
return Countries::getNames($locale);
Expand All @@ -305,7 +305,7 @@ public function getCountryNames(string $locale = null): array
}
}

public function getLocaleNames(string $locale = null): array
public function getLocaleNames(?string $locale = null): array
{
try {
return Locales::getNames($locale);
Expand All @@ -314,7 +314,7 @@ public function getLocaleNames(string $locale = null): array
}
}

public function getCurrencyNames(string $locale = null): array
public function getCurrencyNames(?string $locale = null): array
{
try {
return Currencies::getNames($locale);
Expand All @@ -323,7 +323,7 @@ public function getCurrencyNames(string $locale = null): array
}
}

public function getTimezoneNames(string $locale = null): array
public function getTimezoneNames(?string $locale = null): array
{
try {
return Timezones::getNames($locale);
Expand All @@ -332,7 +332,7 @@ public function getTimezoneNames(string $locale = null): array
}
}

public function formatCurrency($amount, string $currency, array $attrs = [], string $locale = null): string
public function formatCurrency($amount, string $currency, array $attrs = [], ?string $locale = null): string
{
$formatter = $this->createNumberFormatter($locale, 'currency', $attrs);

Expand All @@ -343,7 +343,7 @@ public function formatCurrency($amount, string $currency, array $attrs = [], str
return $ret;
}

public function formatNumber($number, array $attrs = [], string $style = 'decimal', string $type = 'default', string $locale = null): string
public function formatNumber($number, array $attrs = [], string $style = 'decimal', string $type = 'default', ?string $locale = null): string
{
if (!isset(self::NUMBER_TYPES[$type])) {
throw new RuntimeError(sprintf('The type "%s" does not exist, known types are: "%s".', $type, implode('", "', array_keys(self::NUMBER_TYPES))));
Expand All @@ -358,7 +358,7 @@ public function formatNumber($number, array $attrs = [], string $style = 'decima
return $ret;
}

public function formatNumberStyle(string $style, $number, array $attrs = [], string $type = 'default', string $locale = null): string
public function formatNumberStyle(string $style, $number, array $attrs = [], string $type = 'default', ?string $locale = null): string
{
return $this->formatNumber($number, $attrs, $style, $type, $locale);
}
Expand All @@ -367,7 +367,7 @@ public function formatNumberStyle(string $style, $number, array $attrs = [], str
* @param \DateTimeInterface|string|null $date A date or null to use the current time
* @param \DateTimeZone|string|false|null $timezone The target timezone, null to use the default, false to leave unchanged
*/
public function formatDateTime(Environment $env, $date, ?string $dateFormat = 'medium', ?string $timeFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', string $locale = null): string
public function formatDateTime(Environment $env, $date, ?string $dateFormat = 'medium', ?string $timeFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', ?string $locale = null): string
{
$date = CoreExtension::dateConverter($env, $date, $timezone);

Expand All @@ -390,7 +390,7 @@ public function formatDateTime(Environment $env, $date, ?string $dateFormat = 'm
* @param \DateTimeInterface|string|null $date A date or null to use the current time
* @param \DateTimeZone|string|false|null $timezone The target timezone, null to use the default, false to leave unchanged
*/
public function formatDate(Environment $env, $date, ?string $dateFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', string $locale = null): string
public function formatDate(Environment $env, $date, ?string $dateFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', ?string $locale = null): string
{
return $this->formatDateTime($env, $date, $dateFormat, 'none', $pattern, $timezone, $calendar, $locale);
}
Expand All @@ -399,7 +399,7 @@ public function formatDate(Environment $env, $date, ?string $dateFormat = 'mediu
* @param \DateTimeInterface|string|null $date A date or null to use the current time
* @param \DateTimeZone|string|false|null $timezone The target timezone, null to use the default, false to leave unchanged
*/
public function formatTime(Environment $env, $date, ?string $timeFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', string $locale = null): string
public function formatTime(Environment $env, $date, ?string $timeFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', ?string $locale = null): string
{
return $this->formatDateTime($env, $date, 'none', $timeFormat, $pattern, $timezone, $calendar, $locale);
}
Expand Down
2 changes: 1 addition & 1 deletion extra/markdown-extra/ErusevMarkdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ErusevMarkdown implements MarkdownInterface
{
private $converter;

public function __construct(Parsedown $converter = null)
public function __construct(?Parsedown $converter = null)
{
$this->converter = $converter ?: new Parsedown();
}
Expand Down
2 changes: 1 addition & 1 deletion extra/markdown-extra/LeagueMarkdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LeagueMarkdown implements MarkdownInterface
private $converter;
private $legacySupport;

public function __construct(CommonMarkConverter $converter = null)
public function __construct(?CommonMarkConverter $converter = null)
{
$this->converter = $converter ?: new CommonMarkConverter();
$this->legacySupport = !method_exists($this->converter, 'convert');
Expand Down
2 changes: 1 addition & 1 deletion extra/markdown-extra/MichelfMarkdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MichelfMarkdown implements MarkdownInterface
{
private $converter;

public function __construct(MarkdownExtra $converter = null)
public function __construct(?MarkdownExtra $converter = null)
{
if (null === $converter) {
$converter = new MarkdownExtra();
Expand Down
2 changes: 1 addition & 1 deletion extra/string-extra/StringExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class StringExtension extends AbstractExtension
{
private $slugger;

public function __construct(SluggerInterface $slugger = null)
public function __construct(?SluggerInterface $slugger = null)
{
$this->slugger = $slugger ?: new AsciiSlugger();
}
Expand Down
10 changes: 8 additions & 2 deletions extra/twig-extra-bundle/Tests/Fixture/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use League\CommonMark\Extension\Strikethrough\StrikethroughExtension;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Bundle\FrameworkBundle\Test\NotificationAssertionsTrait;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -29,11 +30,16 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
'test' => true,
'router' => ['utf8' => true],
'http_method_override' => false,
'php_errors' => [
'log' => true,
],
];
if (6 === Kernel::MAJOR_VERSION) {

// the "handle_all_throwables" option was introduced in FrameworkBundle 6.2 (and so was the NotificationAssertionsTrait)
if (trait_exists(NotificationAssertionsTrait::class)) {
$config['handle_all_throwables'] = true;
$config['php_errors']['log'] = true;
}

$c->loadFromExtension('framework', $config);
$c->loadFromExtension('twig', [
'default_path' => __DIR__.'/views',
Expand Down
3 changes: 2 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd">
<coverage/>
<testsuites>
<testsuite name="Twig Test Suite">
<directory>./tests/</directory>
Expand Down
6 changes: 3 additions & 3 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public function setCache($cache)
*
* @internal
*/
public function getTemplateClass(string $name, int $index = null): string
public function getTemplateClass(string $name, ?int $index = null): string
{
$key = $this->getLoader()->getCacheKey($name).$this->optionsHash;

Expand Down Expand Up @@ -338,7 +338,7 @@ public function load($name): TemplateWrapper
*
* @internal
*/
public function loadTemplate(string $cls, string $name, int $index = null): Template
public function loadTemplate(string $cls, string $name, ?int $index = null): Template
{
$mainCls = $cls;
if (null !== $index) {
Expand Down Expand Up @@ -393,7 +393,7 @@ public function loadTemplate(string $cls, string $name, int $index = null): Temp
* @throws LoaderError When the template cannot be found
* @throws SyntaxError When an error occurred during compilation
*/
public function createTemplate(string $template, string $name = null): TemplateWrapper
public function createTemplate(string $template, ?string $name = null): TemplateWrapper
{
$hash = hash('xxh128', $template, false);
if (null !== $name) {
Expand Down
4 changes: 2 additions & 2 deletions src/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Error extends \Exception
* @param int $lineno The template line where the error occurred
* @param Source|null $source The source context where the error occurred
*/
public function __construct(string $message, int $lineno = -1, Source $source = null, \Throwable $previous = null)
public function __construct(string $message, int $lineno = -1, ?Source $source = null, ?\Throwable $previous = null)
{
parent::__construct('', 0, $previous);

Expand Down Expand Up @@ -93,7 +93,7 @@ public function getSourceContext(): ?Source
return $this->name ? new Source($this->sourceCode, $this->name, $this->sourcePath) : null;
}

public function setSourceContext(Source $source = null): void
public function setSourceContext(?Source $source = null): void
{
if (null === $source) {
$this->sourceCode = $this->name = $this->sourcePath = null;
Expand Down
3 changes: 1 addition & 2 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\ExpressionParser;
use Twig\Extension\SandboxExtension;
use Twig\Markup;
use Twig\Node\Expression\Binary\AddBinary;
use Twig\Node\Expression\Binary\AndBinary;
Expand Down Expand Up @@ -1170,7 +1169,7 @@ public static function lengthFilter(Environment $env, $thing)
return iterator_count($thing);
}

if (method_exists($thing, '__toString') && !$thing instanceof \Countable) {
if (method_exists($thing, '__toString')) {
return mb_strlen((string) $thing, $env->getCharset());
}

Expand Down
12 changes: 6 additions & 6 deletions src/Extension/SandboxExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class SandboxExtension extends AbstractExtension
private SecurityPolicyInterface $policy;
private ?SourcePolicyInterface $sourcePolicy;

public function __construct(SecurityPolicyInterface $policy, $sandboxed = false, SourcePolicyInterface $sourcePolicy = null)
public function __construct(SecurityPolicyInterface $policy, $sandboxed = false, ?SourcePolicyInterface $sourcePolicy = null)
{
$this->policy = $policy;
$this->sandboxedGlobally = $sandboxed;
Expand All @@ -53,7 +53,7 @@ public function disableSandbox(): void
$this->sandboxed = false;
}

public function isSandboxed(Source $source = null): bool
public function isSandboxed(?Source $source = null): bool
{
return $this->sandboxedGlobally || $this->sandboxed || $this->isSourceSandboxed($source);
}
Expand Down Expand Up @@ -82,14 +82,14 @@ public function getSecurityPolicy(): SecurityPolicyInterface
return $this->policy;
}

public function checkSecurity($tags, $filters, $functions, Source $source = null): void
public function checkSecurity($tags, $filters, $functions, ?Source $source = null): void
{
if ($this->isSandboxed($source)) {
$this->policy->checkSecurity($tags, $filters, $functions);
}
}

public function checkMethodAllowed($obj, $method, int $lineno = -1, Source $source = null): void
public function checkMethodAllowed($obj, $method, int $lineno = -1, ?Source $source = null): void
{
if ($this->isSandboxed($source)) {
try {
Expand All @@ -103,7 +103,7 @@ public function checkMethodAllowed($obj, $method, int $lineno = -1, Source $sour
}
}

public function checkPropertyAllowed($obj, $property, int $lineno = -1, Source $source = null): void
public function checkPropertyAllowed($obj, $property, int $lineno = -1, ?Source $source = null): void
{
if ($this->isSandboxed($source)) {
try {
Expand All @@ -117,7 +117,7 @@ public function checkPropertyAllowed($obj, $property, int $lineno = -1, Source $
}
}

public function ensureToStringAllowed($obj, int $lineno = -1, Source $source = null)
public function ensureToStringAllowed($obj, int $lineno = -1, ?Source $source = null)
{
if ($this->isSandboxed($source) && \is_object($obj) && method_exists($obj, '__toString')) {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/Extension/StringLoaderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function getFunctions(): array
*
* @internal
*/
public static function templateFromString(Environment $env, $template, string $name = null): TemplateWrapper
public static function templateFromString(Environment $env, $template, ?string $name = null): TemplateWrapper
{
return $env->createTemplate((string) $template, $name);
}
Expand Down
Loading

0 comments on commit ae7e6cc

Please sign in to comment.