Skip to content

Commit

Permalink
DiagnoseExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 16, 2024
1 parent 3f2cb3d commit fa497c5
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.2 || ^8.0",
"phpstan/phpstan": "^1.11"
"phpstan/phpstan": "^1.11.7"
},
"conflict": {
"doctrine/collections": "<1.0",
Expand Down
4 changes: 4 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ services:

-
class: PHPStan\Doctrine\Driver\DriverDetector
-
class: PHPStan\Doctrine\DoctrineDiagnoseExtension
tags:
- phpstan.diagnoseExtension
-
class: PHPStan\Type\Doctrine\HydrationModeReturnTypeResolver
-
Expand Down
83 changes: 83 additions & 0 deletions src/Doctrine/DoctrineDiagnoseExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php declare(strict_types = 1);

namespace PHPStan\Doctrine;

use Composer\InstalledVersions;
use Doctrine\ORM\EntityManagerInterface;
use OutOfBoundsException;
use PHPStan\Command\Output;
use PHPStan\Diagnose\DiagnoseExtension;
use PHPStan\Doctrine\Driver\DriverDetector;
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
use function count;
use function sprintf;

class DoctrineDiagnoseExtension implements DiagnoseExtension
{

/** @var ObjectMetadataResolver */
private $objectMetadataResolver;

/** @var DriverDetector */
private $driverDetector;

public function __construct(
ObjectMetadataResolver $objectMetadataResolver,
DriverDetector $driverDetector
)
{
$this->objectMetadataResolver = $objectMetadataResolver;
$this->driverDetector = $driverDetector;
}

public function print(Output $output): void
{
$output->writeLineFormatted(sprintf(
'<info>Doctrine\'s objectManagerLoader:</info> %s',
$this->objectMetadataResolver->hasObjectManagerLoader() ? 'In use' : 'No'
));

$objectManager = $this->objectMetadataResolver->getObjectManager();
if ($objectManager instanceof EntityManagerInterface) {
$connection = $objectManager->getConnection();
$driver = $this->driverDetector->detect($connection);

$output->writeLineFormatted(sprintf(
'<info>Detected driver:</info> %s',
$driver === null ? 'None' : $driver
));
}

$packages = [];
$candidates = [
'doctrine/dbal',
'doctrine/orm',
'doctrine/common',
'doctrine/collections',
'doctrine/persistence',
];
foreach ($candidates as $package) {
try {
$installedVersion = InstalledVersions::getPrettyVersion($package);
} catch (OutOfBoundsException $e) {
continue;
}

if ($installedVersion === null) {
continue;
}

$packages[$package] = $installedVersion;
}

if (count($packages) > 0) {
$output->writeLineFormatted('<info>Installed Doctrine packages:</info>');
foreach ($packages as $package => $version) {
$output->writeLineFormatted(sprintf('%s: %s', $package, $version));
}
}

$output->writeLineFormatted('');
}

}

0 comments on commit fa497c5

Please sign in to comment.