Skip to content

Raise phpstan to max level#1386

Merged
OskarStark merged 3 commits intosonata-project:masterfrom
VincentLanglet:phpstanMax
Apr 11, 2021
Merged

Raise phpstan to max level#1386
OskarStark merged 3 commits intosonata-project:masterfrom
VincentLanglet:phpstanMax

Conversation

@VincentLanglet
Copy link
Copy Markdown
Member

@VincentLanglet VincentLanglet commented Apr 1, 2021

{
if (null === $type) {
$guessType = $this->guesser->guess($fieldDescription);
if (null === $guessType) {
Copy link
Copy Markdown
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
Copy Markdown
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...

{
$template = $blockContext->getTemplate();
\assert(null !== $template);
$limit = $blockContext->getSetting('limit');
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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


public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response
{
$template = $blockContext->getTemplate();
Copy link
Copy Markdown
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
Copy Markdown
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',
        ]);
    }

Comment thread src/Builder/ListBuilder.php Outdated
Comment thread src/Builder/ListBuilder.php Outdated
Comment thread src/Datagrid/Pager.php
$this->getQuery()->setFirstResult(null);
$this->getQuery()->setMaxResults(null);
$query = $this->getQuery();
if (null === $query) {
Copy link
Copy Markdown
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
Copy Markdown
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.

$loader->load('doctrine_orm_filter_types.xml');

$bundles = $container->getParameter('kernel.bundles');
\assert(\is_array($bundles));
Copy link
Copy Markdown
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
Copy Markdown
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

public function testDelete(): void
{
$entityManager = static::bootKernel()->getContainer()->get('doctrine')->getManager();
$doctrine = static::bootKernel()->getContainer()->get('doctrine');
Copy link
Copy Markdown
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
Copy Markdown
Member Author

This is ready @sonata-project/contributors :)

jordisala1991
jordisala1991 previously approved these changes Apr 10, 2021
Comment thread src/Filter/ChoiceFilter.php Outdated
Comment thread src/Filter/ChoiceFilter.php Outdated
@VincentLanglet VincentLanglet requested review from a team and franmomu 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