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

Raise phpstan to max level #1386

Merged
merged 3 commits into from
Apr 11, 2021
Merged

Conversation

@@ -50,6 +50,12 @@ public function buildField(?string $type, FieldDescriptionInterface $fieldDescri
{
if (null === $type) {
$guessType = $this->guesser->guess($fieldDescription);
if (null === $guessType) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If every time we are returning null we throw an exception, wouldnt it be better to only return non null values or throw exception inside the guesser?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made sonata-project/SonataAdminBundle#6987 because the TypeGuesserChain could return null...

If you have a TypeGuesserChain with no guesser how can you return a type ?
And the Guesser interface from Symfony was allowing null as return type...

The Guesser from this bundle never return null, but just in case, I added the exception...

@@ -39,16 +39,21 @@ public function __construct(Environment $twig, AuditReader $auditReader)

public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response
{
$template = $blockContext->getTemplate();
\assert(null !== $template);
$limit = $blockContext->getSetting('limit');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe cast is better here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is

public function configureSettings(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'limit' => 10,
            'template' => '@SonataDoctrineORMAdmin/Block/block_audit.html.twig',
        ]);
    }

What do you think about using allowedTypes ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that a feature from the options resolver? I think it would be great but not sure if phpstan will understand it tho.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phpstan won't understand, so I'll keep the assert, but it still a code improvement

@@ -39,16 +39,21 @@ public function __construct(Environment $twig, AuditReader $auditReader)

public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response
{
$template = $blockContext->getTemplate();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can a block return null here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that no because of

public function configureSettings(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'limit' => 10,
            'template' => '@SonataDoctrineORMAdmin/Block/block_audit.html.twig',
        ]);
    }

$this->getQuery()->setFirstResult(null);
$this->getQuery()->setMaxResults(null);
$query = $this->getQuery();
if (null === $query) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably out of scope of this Pr, but wouldnt be nice if we ensure somehow that the query is never null, maybe as a parameter of this method or as a parameter of construct?

Copy link
Member Author

@VincentLanglet VincentLanglet Apr 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently the pager and the query are constructor param of the Datagrid.
In this one we're running

$this->pager->setQuery($this->query);
$this->pager->init();

Maybe we could remove setQuery and use the query as a param of init indeed.

It's more a SonataAdmin change, so I'll recommend an issue.

@@ -38,6 +38,7 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('doctrine_orm_filter_types.xml');

$bundles = $container->getParameter('kernel.bundles');
\assert(\is_array($bundles));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems strange having to add this around each getParameter to ensure type

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getParameter is considered as returning mixed for phpstan because of the symfony phpdoc

@@ -79,7 +80,9 @@ public function testEdit(): void

public function testDelete(): void
{
$entityManager = static::bootKernel()->getContainer()->get('doctrine')->getManager();
$doctrine = static::bootKernel()->getContainer()->get('doctrine');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the container resolution can be solved warming up cache before running phpstan. (can be done executing test previously). Then you add the xml to the phpstan config and it can solve those issues

@VincentLanglet VincentLanglet marked this pull request as ready for review April 9, 2021 13:43
@VincentLanglet VincentLanglet requested a review from a team April 9, 2021 13:43
@VincentLanglet
Copy link
Member Author

This is ready @sonata-project/contributors :)

jordisala1991
jordisala1991 previously approved these changes Apr 10, 2021
src/Filter/ChoiceFilter.php Outdated Show resolved Hide resolved
src/Filter/ChoiceFilter.php Outdated Show resolved Hide resolved
@VincentLanglet VincentLanglet requested review from franmomu and a team April 10, 2021 10:07
@OskarStark OskarStark merged commit 8388bfa into sonata-project:master Apr 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants