Skip to content

Commit

Permalink
Merge pull request #19 from weirdan/support-ServiceEntityRepository
Browse files Browse the repository at this point in the history
Support ServiceEntityRepository (from doctrine-bundle)
  • Loading branch information
weirdan authored Jul 11, 2019
2 parents 1eb37dc + 738a47d commit 30ea862
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
40 changes: 40 additions & 0 deletions Plugin.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace Weirdan\DoctrinePsalmPlugin;

use OutOfBoundsException;
use PackageVersions\Versions;
use Muglug\PackageVersions\Versions as LegacyVersions;
use SimpleXMLElement;
use Psalm\Plugin\PluginEntryPointInterface;
use Psalm\Plugin\RegistrationInterface;
Expand All @@ -11,6 +14,7 @@ class Plugin implements PluginEntryPointInterface
public function __invoke(RegistrationInterface $psalm, ?SimpleXMLElement $config = null)
{
$stubs = $this->getStubFiles();
$stubs = array_merge($stubs, $this->getBundleStubs());
foreach ($stubs as $file) {
$psalm->addStubFile($file);
}
Expand All @@ -21,4 +25,40 @@ private function getStubFiles(): array
{
return glob(__DIR__ . '/' . 'stubs/*.php');
}

/** @return string[] */
private function getBundleStubs(): array
{
if (!$this->hasPackage('doctrine/doctrine-bundle')) {
return [];
}

return glob(__DIR__ . '/' . 'bundle-stubs/*.php');
}

private function hasPackage(string $packageName): bool
{
try {
$this->getPackageVersion($packageName);
} catch (OutOfBoundsException $e) {
return false;
}
return true;
}

/**
* @throws OutOfBoundsException
*/
private function getPackageVersion(string $packageName): string
{
if (class_exists(Versions::class)) {
return (string) Versions::getVersion($packageName);
}

if (class_exists(LegacyVersions::class)) {
return (string) LegacyVersions::getVersion($packageName);
}

throw new OutOfBoundsException;
}
}
18 changes: 18 additions & 0 deletions bundle-stubs/ServiceEntityRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Repository;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityRepository;

/**
* @template T
* @template-extends EntityRepository<T>
*/
class ServiceEntityRepository extends EntityRepository implements ServiceEntityRepositoryInterface
{
/**
* @param string $entityClass The class name of the entity this repository manages
*/
public function __construct(ManagerRegistry $registry, $entityClass) {}
}

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"squizlabs/php_codesniffer": "^3.3",
"codeception/base": "^2.5 || ^3.0",
"composer/semver": "^1.4",
"weirdan/codeception-psalm-module": "^0.2.2"
"weirdan/codeception-psalm-module": "^0.2.2",
"doctrine/doctrine-bundle": "^1.11"
}
}
47 changes: 47 additions & 0 deletions tests/acceptance/ServiceEntityRepository.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Feature: ServiceEntityRepository
In order to use simplified repository safely
As a Psalm user
I need Psalm to typecheck ServiceEntityRepository

Background:
Given I have the following config
"""
<?xml version="1.0"?>
<psalm totallyTyped="true">
<projectFiles>
<directory name="."/>
</projectFiles>
<plugins>
<pluginClass class="Weirdan\DoctrinePsalmPlugin\Plugin" />
</plugins>
</psalm>
"""
And I have the following code preamble
"""
<?php
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;
interface I {}
"""

@ServiceEntityRepository::inheritance
Scenario: Extending a ServiceEntityRepository
Given I have the following code
"""
/**
* @method I|null find($id, $lockMode = null, $lockVersion = null)
* @method I|null findOneBy(array $criteria, array $orderBy = null)
* @method I[] findAll()
* @method I[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
* @template-extends ServiceEntityRepository<I>
* @psalm-suppress PropertyNotSetInConstructor
*/
class IRepository extends ServiceEntityRepository {
public function __construct(RegistryInterface $registry) {
parent::__construct($registry, I::class);
}
}
"""
When I run Psalm
Then I see no errors

0 comments on commit 30ea862

Please sign in to comment.