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

Invalid finding: Auto-Generated ID is never assigned int #610

Open
kevinpapst opened this issue Sep 17, 2024 · 4 comments
Open

Invalid finding: Auto-Generated ID is never assigned int #610

kevinpapst opened this issue Sep 17, 2024 · 4 comments

Comments

@kevinpapst
Copy link
Contributor

Expected:
PHPStand understands that such a definition is valid and that $id is set to int by Doctrine.

Actual:

Property HelloWorld::$id (int|null) is never assigned int so it can be removed from the property type.

    #[ORM\Column(name: 'id', type: 'integer')]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'IDENTITY')]
    private ?int $id = null;

    public function __clone()
    {
        if ($this->id !== null) {
            $this->id = null;
        }
        // ...
     }
}

Only happens when I have that clone method in place, which says that id is not null 😁

Playground: https://phpstan.org/r/ed1ce389-72ab-4deb-a1bc-6d570db17f87
But in that case it makes sense, as there is no Doctrine annotation/bridge available.

@kevinpapst kevinpapst changed the title Auto-Generated ID treated as: is never assigned int Invalid finding: Auto-Generated ID is never assigned int Sep 17, 2024
@scannerGT
Copy link

I have the same issue. Entity generatet via maker, id is nullable and autogenerated, PHPStan 2.0.2 (lvl 6). Nop setter, no __clone - just pure Symfony entity and fresh PHPStan installation.

@kevinpapst
Copy link
Contributor Author

To share some more infos: this does not generally happen. Only in one Symfony bundle of at least 6 of my bundles that use Doctrine entities. I invested a few hours to find a reason and/or fix, but nothing worked.
This is my PHPStan and work-around:

includes:
  - %rootDir%/../phpstan-symfony/extension.neon
  - %rootDir%/../phpstan-symfony/rules.neon
  - %rootDir%/../phpstan-doctrine/extension.neon
  - %rootDir%/../phpstan-doctrine/rules.neon
  - %rootDir%/../phpstan-deprecation-rules/rules.neon
  - %rootDir%/../phpstan-strict-rules/rules.neon
  - %rootDir%/../phpstan/conf/bleedingEdge.neon

parameters:
  level: 9
  excludePaths:
    - vendor/(?)
  scanDirectories:
    - Migrations
  treatPhpDocTypesAsCertain: false
  inferPrivatePropertyTypeFromConstructor: true
  doctrine:
    allowNullablePropertyForRequiredField: true
  ignoreErrors:
    -
      message: "#^Property App\\\\Entity\\\\HelloWorld\\:\\:\\$id \\(int\\|null\\) is never assigned int so it can be removed from the property type\\.$#"
      reportUnmatched: false
      path: Entity/Task.php

@DennisdeBest
Copy link

Hello, I had the same issue. To ignore it for all the entities I added :

       - '#Property .+::\$id \(int\|null\) is never assigned int so it can be removed from the property type\.$#'

To the ignoreErrors.

@GuillaumeVrill
Copy link

GuillaumeVrill commented Dec 27, 2024

I confirm the following workaround works (file phpstan.dist.neon) :

parameters:
    ...
    ignoreErrors:
        - message: "#^Property App\\\\Entity\\\\.*\\:\\:\\$id \\(int\\|null\\) is never assigned int so it can be removed from the property type\\.$#"

I reproduce on a recent project, with standard configuration, on basic entities with or without the clone method.

Code example on a basic doctrine entity :

#[ORM\Entity(repositoryClass: MyEntity::class)]
class MyEntity
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;
    
    public function getId(): ?int
    {
        return $this->id;
    }
    
    // No setter, as it's autogenerated / managed by Doctrine. 
}

If you do not wish (or can't, for any reason) edit your phpstan.dist.neon file, adding a setter like the following resolves the problem, but i don't know if that could conflict with Doctrine native behavior (though i don't think so).

public function setId(int $id): self
{
    $this->id = $id;

    return $this;
}

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

No branches or pull requests

4 participants