-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #5786 [Chore] Fix phpstan issues (asispts)
This PR was squashed before being merged into the 4.x branch. Discussion ---------- [Chore] Fix phpstan issues This PR will fix several `PHPStan` issues: - [x] Fix `Cannot unset offset string on array<int, EasyCorp\Bundle\EasyAdminBundle\Dto\ActionDto>.` - [x] Fix `Variable method call on Twig\Extension\RuntimeExtensionInterface.` - [x] Refactor `EasyAdminTwigExtension::applyFilterIfExists` - [x] Add integration tests for `applyFilterIfExists`. Issues #3762 and #4808 have been taken into consideration. - [x] Update parameter type declaration of `Action::new`. We can't use ``@var`` to annotate a parameter. See: https://phpstan.org/r/7695e922-db7f-42b3-ac4f-8ff65a8a37b9 - [x] Modify incorrect PHPDoc. See: https://phpstan.org/r/df4eb1c2-7286-4aa7-b276-f36ff2c0c82c These issues are a little bit tricky. Both `TranslatableMessageBuilder::withParameters` and `TranslatableChoiceMessage::__toString` are not compatible with `TranslatableInterface`. Those functions should depend on `TranslatableMessage`. ```txt ------ ----------------------------------------------------------------------- Line Field/Configurator/ChoiceConfigurator.php ------ ----------------------------------------------------------------------- 124 Parameter #1 $message of class EasyCorp\Bundle\EasyAdminBundle\Translation\TranslatableChoiceMessage constructor expects Symfony\Component\Translation\TranslatableMessage, Symfony\Contracts\Translation\TranslatableInterface given. ------ ----------------------------------------------------------------------- ------ ----------------------------------------------------------------------- Line Translation/TranslatableMessageBuilder.php ------ ----------------------------------------------------------------------- 28 Call to an undefined method Symfony\Contracts\Translation\TranslatableInterface::getMessage(). 29 Call to an undefined method Symfony\Contracts\Translation\TranslatableInterface::getParameters(). 30 Call to an undefined method Symfony\Contracts\Translation\TranslatableInterface::getDomain(). ------ ----------------------------------------------------------------------- ``` Commits ------- 053f62c [Chore] Fix phpstan issues
- Loading branch information
Showing
10 changed files
with
168 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Twig; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Twig\Environment; | ||
use Twig\Error\RuntimeError; | ||
use Twig\RuntimeLoader\FactoryRuntimeLoader; | ||
use Twig\TwigFilter; | ||
|
||
final class TwigFilterTest extends KernelTestCase | ||
{ | ||
private Environment $twig; | ||
|
||
protected function setUp(): void | ||
{ | ||
self::bootKernel(); | ||
|
||
$this->twig = $this->getContainer()->get(Environment::class); | ||
} | ||
|
||
public function myFilter($number, int $decimals, string $decPoint, string $thousandsSep) | ||
{ | ||
return number_format($number, $decimals, $decPoint, $thousandsSep); | ||
} | ||
|
||
public function testNonLazyLoadedFilter(): void | ||
{ | ||
$this->twig->addFilter(new TwigFilter('my_filter', [$this, 'myFilter'])); | ||
|
||
$view = "{{number | ea_apply_filter_if_exists('my_filter', 2, ',', '.')}}"; | ||
$context = ['number' => 123_456.789]; | ||
$result = $this->twig->createTemplate($view)->render($context); | ||
$this->assertSame('123.456,79', $result); | ||
} | ||
|
||
public function testLazyLoadedFilter(): void | ||
{ | ||
$loader = new FactoryRuntimeLoader([ | ||
'EATests\MyLazyFilterRuntime' => fn () => new class() { | ||
public function myFilter($number, int $decimals, string $decPoint, string $thousandsSep) | ||
{ | ||
return number_format($number, $decimals, $decPoint, $thousandsSep); | ||
} | ||
}, | ||
]); | ||
|
||
$this->twig->addRuntimeLoader($loader); | ||
$this->twig->addFilter(new TwigFilter('my_filter', ['EATests\MyLazyFilterRuntime', 'myFilter'])); | ||
|
||
$view = "{{number | ea_apply_filter_if_exists('my_filter', 2, ',', '.')}}"; | ||
$context = ['number' => 123_456.789]; | ||
$result = $this->twig->createTemplate($view)->render($context); | ||
$this->assertSame('123.456,79', $result); | ||
} | ||
|
||
public function testBuiltinFilter(): void | ||
{ | ||
$view = "{{number | ea_apply_filter_if_exists('abs')}}"; | ||
$context = ['number' => -10]; | ||
$result = $this->twig->createTemplate($view)->render($context); | ||
$this->assertSame('10', $result); | ||
} | ||
|
||
public function testNotFoundFilter(): void | ||
{ | ||
$view = "{{number | ea_apply_filter_if_exists('imagine_filter')}}"; | ||
$context = ['number' => 3.14]; | ||
$result = $this->twig->createTemplate($view)->render($context); | ||
$this->assertSame('3.14', $result); | ||
} | ||
|
||
public function testNotFoundClass(): void | ||
{ | ||
$this->expectException(RuntimeError::class); | ||
$this->expectExceptionMessage('a'); | ||
|
||
$this->twig->addFilter(new TwigFilter('my_filter', ['EATests\NotFoundClass', 'myFilter'])); | ||
|
||
$view = "{{number | ea_apply_filter_if_exists('my_filter')}}"; | ||
$context = ['number' => 123_456.789]; | ||
$this->twig->createTemplate($view)->render($context); | ||
} | ||
|
||
public function testInvalidCallableString(): void | ||
{ | ||
$this->twig->addFilter(new TwigFilter('my_filter', 'not-callable')); | ||
|
||
$view = "{{number | ea_apply_filter_if_exists('my_filter')}}"; | ||
$context = ['number' => 123_456.789]; | ||
$result = $this->twig->createTemplate($view)->render($context); | ||
$this->assertSame('', $result); | ||
} | ||
|
||
public function testInvalidCallableArray(): void | ||
{ | ||
$loader = new FactoryRuntimeLoader([ | ||
'EATests\MyLazyFilterRuntime' => fn () => new class() {}, | ||
]); | ||
|
||
$this->twig->addRuntimeLoader($loader); | ||
$this->twig->addFilter(new TwigFilter('my_filter', ['EATests\MyLazyFilterRuntime', 'unknownMethod'])); | ||
|
||
$view = "{{number | ea_apply_filter_if_exists('my_filter')}}"; | ||
$context = ['number' => 123_456.789]; | ||
$result = $this->twig->createTemplate($view)->render($context); | ||
$this->assertSame('', $result); | ||
} | ||
} |