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

phpstan: lvl 1 -> 2 #1854

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
parameters:
level: 1
level: 2
paths:
- src
- tests

excludePaths:
- src/Command/Proxy/ConvertMappingDoctrineCommand.php
- src/Command/Proxy/EnsureProductionSettingsDoctrineCommand.php
ignoreErrors:
# Available in ORM < 3 only
- '#Doctrine\\ORM\\Tools\\EntityGenerator.#'
- '#Doctrine\\ORM\\Tools\\DisconnectedClassMetadataFactory.#'
- '#Doctrine\\ORM\\Tools\\Export\\ClassMetadataExporter.#'
# phpstan has no array shape intersection support https://github.com/phpstan/phpstan/issues/12414
- message: '#unresolvable type.#'
path: src/DataCollector/DoctrineDataCollector.php
# Probably needs Symfony plugin
- message: '#Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\Node#'
path: src/DependencyInjection/Configuration.php
Copy link
Member

Choose a reason for hiding this comment

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

How do you make the choice to user ignoreErrors vs phpstan-ignore? Now that phpstan-ignore comes with error identifier, I think it's good.

Copy link
Member Author

Choose a reason for hiding this comment

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

This rule suppresses 10 violations. It's hard to make a hard rule out of it, but 10 violations of same kind I would say are sufficient. Once we solve underlying issue (like here, maybe it will be solved by symfony plugin), we can remove it from one place instead of hunting it all over. If it was like 3 violations, I would be more inclined to do it inline. But anything in between is subjective.

Copy link
Member

Choose a reason for hiding this comment

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

Now that phpstan-ignore comes with error identifier

We can use those identifiers in the ignore rules of our neon file as well and we should probably do that.

Copy link
Member

Choose a reason for hiding this comment

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

To elaborate, in the past I thought the baseline approach was superior, because using @phpstan-ignore-next-line could hide more than the intended errors. Now that it's possible to do that directly in the code, I think it might be nicer to have the ignore rules close to the code, because sometimes I'm wondering why PHPStan "isn't working". Also, if you see an existing ignore rule in the code, you are more likely to challenge it, if you know how to properly fix it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes normally I prefer inline suppressions (and ask people to do it). There are bunch of issues that prevent us to do it efficiently unfortunately, for example it's not enough for phpstan to suppress missing class when instantiating it, phpstan is not smart enough to understand that if you ignore missing class it's also ok to ignore method calls on such class. So now you have to sprinkle code with suppressions each time such class is used as well. I would expect I can just suppress instantiating class that can be missing, instead of having to suppress dozens of other issues cascading from that as well.

3 changes: 0 additions & 3 deletions src/Command/DoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ public function __construct(ManagerRegistry $doctrine)
* get a doctrine entity generator
*
* @return EntityGenerator
*
* @psalm-suppress UndefinedDocblockClass ORM < 3 specific
*/
protected function getEntityGenerator()
{
/** @phpstan-ignore class.notFound */
$entityGenerator = new EntityGenerator();
$entityGenerator->setGenerateAnnotations(false);
$entityGenerator->setGenerateStubMethods(true);
Expand Down
6 changes: 5 additions & 1 deletion src/Command/ImportMappingDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\Bundle\DoctrineBundle\Command;

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Driver\DatabaseDriver;
use Doctrine\ORM\Tools\Console\MetadataFilter;
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
Expand All @@ -13,6 +14,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use function assert;
use function chmod;
use function dirname;
use function file_put_contents;
Expand Down Expand Up @@ -91,6 +93,7 @@

$namespaceOrBundle = $input->getArgument('name');
if (isset($this->bundles[$namespaceOrBundle])) {
/** @phpstan-ignore method.notFound */
$bundle = $this->getApplication()->getKernel()->getBundle($namespaceOrBundle);
$namespace = $bundle->getNamespace() . '\Entity';

Expand Down Expand Up @@ -121,20 +124,21 @@

$em = $this->getEntityManager($input->getOption('em'));

/* @phpstan-ignore method.notFound (Available in DBAL < 4) */
$databaseDriver = new DatabaseDriver($em->getConnection()->getSchemaManager());
$em->getConfiguration()->setMetadataDriverImpl($databaseDriver);

$emName = $input->getOption('em');
$emName = $emName ? $emName : 'default';

/* @phpstan-ignore class.notFound */
$cmf = new DisconnectedClassMetadataFactory();
$cmf->setEntityManager($em);
$metadata = $cmf->getAllMetadata();
$metadata = MetadataFilter::filter($metadata, $input->getOption('filter'));
if ($metadata) {
$output->writeln(sprintf('Importing mapping information from "<info>%s</info>" entity manager', $emName));
foreach ($metadata as $class) {
assert($class instanceof ClassMetadata);

Check warning on line 141 in src/Command/ImportMappingDoctrineCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/ImportMappingDoctrineCommand.php#L141

Added line #L141 was not covered by tests
$className = $class->name;
$class->name = $namespace . '\\' . $className;
if ($type === 'annotation') {
Expand Down
2 changes: 1 addition & 1 deletion src/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function createConnection(array $params, ?Configuration $config = null, ?
$connection = DriverManager::getConnection(...array_merge([$params, $config], $eventManager ? [$eventManager] : []));
$params = $this->addDatabaseSuffix(array_merge($connection->getParams(), $overriddenOptions));
$driver = $connection->getDriver();
/** @psalm-suppress InvalidScalarArgument Bogus error, StaticServerVersionProvider implements Doctrine\DBAL\ServerVersionProvider */
/** @phpstan-ignore arguments.count (DBAL < 4.x doesn't accept an argument) */
$platform = $driver->getDatabasePlatform(
...(class_exists(StaticServerVersionProvider::class)
? [new StaticServerVersionProvider($params['serverVersion'] ?? $params['primary']['serverVersion'] ?? '')]
Expand Down
2 changes: 1 addition & 1 deletion src/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function getAliasNamespace($alias)
}

try {
/** @psalm-suppress UndefinedMethod ORM < 3 specific */
/** @phpstan-ignore method.notFound (ORM < 3 specific) */
return $objectManager->getConfiguration()->getEntityNamespace($alias);
/* @phpstan-ignore class.notFound */
} catch (ORMException $e) {
Expand Down
Loading