Skip to content

Commit fa497c5

Browse files
committed
DiagnoseExtension
1 parent 3f2cb3d commit fa497c5

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

Diff for: composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"require": {
99
"php": "^7.2 || ^8.0",
10-
"phpstan/phpstan": "^1.11"
10+
"phpstan/phpstan": "^1.11.7"
1111
},
1212
"conflict": {
1313
"doctrine/collections": "<1.0",

Diff for: extension.neon

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ services:
9191

9292
-
9393
class: PHPStan\Doctrine\Driver\DriverDetector
94+
-
95+
class: PHPStan\Doctrine\DoctrineDiagnoseExtension
96+
tags:
97+
- phpstan.diagnoseExtension
9498
-
9599
class: PHPStan\Type\Doctrine\HydrationModeReturnTypeResolver
96100
-

Diff for: src/Doctrine/DoctrineDiagnoseExtension.php

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Doctrine;
4+
5+
use Composer\InstalledVersions;
6+
use Doctrine\ORM\EntityManagerInterface;
7+
use OutOfBoundsException;
8+
use PHPStan\Command\Output;
9+
use PHPStan\Diagnose\DiagnoseExtension;
10+
use PHPStan\Doctrine\Driver\DriverDetector;
11+
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
12+
use function count;
13+
use function sprintf;
14+
15+
class DoctrineDiagnoseExtension implements DiagnoseExtension
16+
{
17+
18+
/** @var ObjectMetadataResolver */
19+
private $objectMetadataResolver;
20+
21+
/** @var DriverDetector */
22+
private $driverDetector;
23+
24+
public function __construct(
25+
ObjectMetadataResolver $objectMetadataResolver,
26+
DriverDetector $driverDetector
27+
)
28+
{
29+
$this->objectMetadataResolver = $objectMetadataResolver;
30+
$this->driverDetector = $driverDetector;
31+
}
32+
33+
public function print(Output $output): void
34+
{
35+
$output->writeLineFormatted(sprintf(
36+
'<info>Doctrine\'s objectManagerLoader:</info> %s',
37+
$this->objectMetadataResolver->hasObjectManagerLoader() ? 'In use' : 'No'
38+
));
39+
40+
$objectManager = $this->objectMetadataResolver->getObjectManager();
41+
if ($objectManager instanceof EntityManagerInterface) {
42+
$connection = $objectManager->getConnection();
43+
$driver = $this->driverDetector->detect($connection);
44+
45+
$output->writeLineFormatted(sprintf(
46+
'<info>Detected driver:</info> %s',
47+
$driver === null ? 'None' : $driver
48+
));
49+
}
50+
51+
$packages = [];
52+
$candidates = [
53+
'doctrine/dbal',
54+
'doctrine/orm',
55+
'doctrine/common',
56+
'doctrine/collections',
57+
'doctrine/persistence',
58+
];
59+
foreach ($candidates as $package) {
60+
try {
61+
$installedVersion = InstalledVersions::getPrettyVersion($package);
62+
} catch (OutOfBoundsException $e) {
63+
continue;
64+
}
65+
66+
if ($installedVersion === null) {
67+
continue;
68+
}
69+
70+
$packages[$package] = $installedVersion;
71+
}
72+
73+
if (count($packages) > 0) {
74+
$output->writeLineFormatted('<info>Installed Doctrine packages:</info>');
75+
foreach ($packages as $package => $version) {
76+
$output->writeLineFormatted(sprintf('%s: %s', $package, $version));
77+
}
78+
}
79+
80+
$output->writeLineFormatted('');
81+
}
82+
83+
}

0 commit comments

Comments
 (0)