-
-
Notifications
You must be signed in to change notification settings - Fork 80
Add service-id-string type #421
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
Draft
mglaman
wants to merge
1
commit into
main
Choose a base branch
from
service-id-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Drupal; | ||
|
||
use PHPStan\ShouldNotHappenException; | ||
|
||
final class ServiceMapStaticAccessor | ||
{ | ||
/** | ||
* @var \mglaman\PHPStanDrupal\Drupal\ServiceMap | ||
*/ | ||
private static $instance; | ||
|
||
private function __construct() | ||
{ | ||
} | ||
|
||
public static function registerInstance(ServiceMap $serviceMap): void | ||
{ | ||
self::$instance = $serviceMap; | ||
} | ||
|
||
public static function getInstance(): ServiceMap | ||
{ | ||
if (self::$instance === null) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
return self::$instance; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Type; | ||
|
||
use mglaman\PHPStanDrupal\Drupal\ServiceMapStaticAccessor; | ||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\CompoundType; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\VerbosityLevel; | ||
|
||
final class ServiceIdType extends \PHPStan\Type\StringType | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
public function describe(VerbosityLevel $level): string | ||
{ | ||
return 'service-id-string'; | ||
} | ||
|
||
public function accepts(Type $type, bool $strictTypes): TrinaryLogic | ||
{ | ||
if ($type instanceof CompoundType) { | ||
return $type->isAcceptedBy($this, $strictTypes); | ||
} | ||
|
||
if ($type instanceof ConstantStringType) { | ||
$serviceDefinition = ServiceMapStaticAccessor::getInstance()->getService($type->getValue()); | ||
if ($serviceDefinition !== null) { | ||
return TrinaryLogic::createYes(); | ||
} | ||
// Some services may be dynamically defined, so return maybe. | ||
return TrinaryLogic::createMaybe(); | ||
} | ||
|
||
if ($type instanceof self) { | ||
return TrinaryLogic::createYes(); | ||
} | ||
|
||
if ($type instanceof StringType) { | ||
return TrinaryLogic::createMaybe(); | ||
} | ||
|
||
return TrinaryLogic::createNo(); | ||
} | ||
|
||
public function isSuperTypeOf(Type $type): TrinaryLogic | ||
{ | ||
if ($type instanceof ConstantStringType) { | ||
$serviceDefinition = ServiceMapStaticAccessor::getInstance()->getService($type->getValue()); | ||
if ($serviceDefinition !== null) { | ||
return TrinaryLogic::createYes(); | ||
} | ||
// Some services may be dynamically defined, so return maybe. | ||
return TrinaryLogic::createMaybe(); | ||
} | ||
|
||
if ($type instanceof self) { | ||
return TrinaryLogic::createYes(); | ||
} | ||
|
||
if ($type instanceof parent) { | ||
return TrinaryLogic::createMaybe(); | ||
} | ||
|
||
if ($type instanceof CompoundType) { | ||
return $type->isSubTypeOf($this); | ||
} | ||
|
||
return TrinaryLogic::createNo(); | ||
} | ||
|
||
/** | ||
* @param mixed[] $properties | ||
*/ | ||
public static function __set_state(array $properties) : \PHPStan\Type\Type | ||
{ | ||
return new self(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Type; | ||
|
||
use PHPStan\Analyser\NameScope; | ||
use PHPStan\PhpDoc\TypeNodeResolverExtension; | ||
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; | ||
use PHPStan\PhpDocParser\Ast\Type\TypeNode; | ||
|
||
final class ServiceIdTypeNodeResolverExtension implements TypeNodeResolverExtension | ||
{ | ||
|
||
public function resolve(TypeNode $typeNode, NameScope $nameScope): ?\PHPStan\Type\Type | ||
{ | ||
if ($typeNode instanceof IdentifierTypeNode && $typeNode->__toString() === 'service-id-string') { | ||
return new \mglaman\PHPStanDrupal\Type\ServiceIdType(); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
class Drupal { | ||
|
||
/** | ||
* @var \Symfony\Component\DependencyInjection\ContainerInterface|null | ||
*/ | ||
protected static $container; | ||
|
||
/** | ||
* @phpstan-param service-id-string $id | ||
*/ | ||
public static function service($id) { | ||
return static::getContainer()->get($id); | ||
} | ||
|
||
/** | ||
* @phpstan-param service-id-string $id | ||
*/ | ||
public static function hasService($id): bool { | ||
// Check hasContainer() first in order to always return a Boolean. | ||
return static::hasContainer() && static::getContainer()->has($id); | ||
} | ||
|
||
/** | ||
* @return \Symfony\Component\DependencyInjection\ContainerInterface | ||
*/ | ||
public static function getContainer(): \Symfony\Component\DependencyInjection\ContainerInterface { | ||
return static::$container; | ||
} | ||
|
||
public static function hasContainer(): bool { | ||
return static::$container !== NULL; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Tests\Type; | ||
|
||
use Drupal\Core\Entity\EntityTypeManager; | ||
use mglaman\PHPStanDrupal\Drupal\ServiceMap; | ||
use mglaman\PHPStanDrupal\Drupal\ServiceMapStaticAccessor; | ||
use mglaman\PHPStanDrupal\Tests\AdditionalConfigFilesTrait; | ||
use PHPStan\Testing\PHPStanTestCase; | ||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\VerbosityLevel; | ||
|
||
final class ServiceIdTypeTest extends PHPStanTestCase { | ||
|
||
use AdditionalConfigFilesTrait; | ||
|
||
public function dataAccepts(): iterable | ||
{ | ||
yield 'self' => [ | ||
new \mglaman\PHPStanDrupal\Type\ServiceIdType(), | ||
new \mglaman\PHPStanDrupal\Type\ServiceIdType(), | ||
TrinaryLogic::createYes(), | ||
]; | ||
yield 'valid service' => [ | ||
new \mglaman\PHPStanDrupal\Type\ServiceIdType(), | ||
new ConstantStringType('entity_type.manager'), | ||
TrinaryLogic::createYes(), | ||
]; | ||
yield 'invalid service' => [ | ||
new \mglaman\PHPStanDrupal\Type\ServiceIdType(), | ||
new ConstantStringType('foo.manager'), | ||
TrinaryLogic::createMaybe(), | ||
]; | ||
yield 'generic string' => [ | ||
new \mglaman\PHPStanDrupal\Type\ServiceIdType(), | ||
new StringType(), | ||
TrinaryLogic::createMaybe(), | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataAccepts | ||
*/ | ||
public function testAccepts(\mglaman\PHPStanDrupal\Type\ServiceIdType $type, Type $otherType, TrinaryLogic $expectedResult): void | ||
{ | ||
$serviceMap = new ServiceMap(); | ||
$serviceMap->setDrupalServices([ | ||
'entity_type.manager' => [ | ||
'class' => EntityTypeManager::class | ||
], | ||
]); | ||
ServiceMapStaticAccessor::registerInstance($serviceMap); | ||
$actualResult = $type->accepts($otherType, true); | ||
self::assertSame( | ||
$expectedResult->describe(), | ||
$actualResult->describe(), | ||
sprintf('%s -> accepts(%s)', $type->describe(VerbosityLevel::precise()), $otherType->describe(VerbosityLevel::precise())), | ||
); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to see if there is a way to get this testable as the allowed parameters.