Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 104
Add a dynamic stub loader to prefer upstream types#481
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
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| <?php declare(strict_types = 1); | ||
| namespace PHPStan\Stubs\Symfony; | ||
| use Composer\InstalledVersions; | ||
| use OutOfBoundsException; | ||
| use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound; | ||
| use PHPStan\BetterReflection\Reflector\Reflector; | ||
| use PHPStan\PhpDoc\StubFilesExtension; | ||
| use function class_exists; | ||
| use function dirname; | ||
| use function version_compare; | ||
| class StubFilesExtensionLoader implements StubFilesExtension | ||
| { | ||
| private Reflector $reflector; | ||
| public function __construct( | ||
| Reflector $reflector | ||
| ) | ||
| { | ||
| $this->reflector = $reflector; | ||
| } | ||
| public function getFiles(): array | ||
| { | ||
| $stubsDir = dirname(dirname(dirname(__DIR__))) . '/stubs'; | ||
| $files = []; | ||
| if ($this->isInstalledVersionBelow('symfony/security-bundle', '6.3.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/AuthenticatorFactoryInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/FirewallListenerFactoryInterface.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/console', '6.2.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/Console/Command.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Console/Exception/ExceptionInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Console/Exception/InvalidArgumentException.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Console/Exception/LogicException.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Console/Helper/HelperInterface.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/console', '8.1.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/Console/Output/OutputInterface.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/dependency-injection', '6.2.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/DependencyInjection/ContainerBuilder.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/DependencyInjection/Extension/ExtensionInterface.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/event-dispatcher-contracts', '3.1.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/EventDispatcher/EventDispatcherInterface.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/event-dispatcher', '5.4.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/EventDispatcher/EventSubscriberInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/EventDispatcher/GenericEvent.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/form', '6.2.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/Form/DataTransformerInterface.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/form', '8.1.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/Form/ChoiceList/Loader/ChoiceLoaderInterface.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/http-foundation', '5.4.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/HttpFoundation/HeaderBag.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/HttpFoundation/Session.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/http-foundation', '6.2.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/HttpFoundation/Cookie.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/http-foundation', '7.4.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/HttpFoundation/ParameterBag.stub'; | ||
| try { | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we check that the version is >= some version ? ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could. I copied the way it was done in the existing code that I moved here (to avoid having to handle coordinated conditions between 2 loaders). For reference, InputBag shipped in version 5.1.0 of the component. Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't see it was coming from another file | ||
| $this->reflector->reflectClass('Symfony\Component\HttpFoundation\InputBag'); | ||
| $files[] = $stubsDir . '/Symfony/Component/HttpFoundation/InputBag.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/HttpFoundation/Request.stub'; | ||
| } catch (IdentifierNotFound $e) { | ||
| // Don't load the InputBag and the associated Request stubs for older Symfony versions that did not have InputBag yet to avoid breaking the Request. | ||
| } | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/messenger', '6.1.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/Messenger/Envelope.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Messenger/StampInterface.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/process', '5.4.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/Process/Exception/LogicException.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Process/Process.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/property-access', '8.1.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/PropertyAccess/Exception/AccessException.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/PropertyAccess/Exception/ExceptionInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/PropertyAccess/Exception/InvalidArgumentException.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/PropertyAccess/Exception/RuntimeException.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/PropertyAccess/Exception/UnexpectedTypeException.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/PropertyAccess/PropertyAccessor.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/PropertyAccess/PropertyAccessorInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/PropertyAccess/PropertyPathInterface.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/security-acl', '3.2.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/Security/Acl/Model/AclInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Security/Acl/Model/EntryInterface.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/security-core', '8.1.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/Security/Core/Authentication/Token/TokenInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Security/Core/Authorization/Voter/VoterInterface.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/security-core', '6.3.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/Security/Core/Authorization/Voter/Voter.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/serializer', '7.0.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Encoder/ContextAwareDecoderInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Normalizer/ContextAwareDenormalizerInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Normalizer/ContextAwareNormalizerInterface.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/serializer', '7.4.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Normalizer/DenormalizerInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Normalizer/NormalizerInterface.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/serializer', '8.1.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Encoder/DecoderInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Encoder/EncoderInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Exception/BadMethodCallException.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Exception/CircularReferenceException.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Exception/ExceptionInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Exception/ExtraAttributesException.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Exception/InvalidArgumentException.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Exception/LogicException.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Exception/RuntimeException.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Exception/UnexpectedValueException.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Normalizer/DenormalizableInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Serializer/Normalizer/NormalizableInterface.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/validator', '5.4.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/Validator/ConstraintViolationInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Validator/ConstraintViolationListInterface.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/validator', '7.1.0.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Component/Validator/Constraint.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Validator/Constraints/Composite.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Component/Validator/Constraints/Compound.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('symfony/cache-contracts', '3.2.1.0')) { | ||
| $files[] = $stubsDir . '/Symfony/Contracts/Cache/CacheInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Contracts/Cache/CallbackInterface.stub'; | ||
| $files[] = $stubsDir . '/Symfony/Contracts/Cache/ItemInterface.stub'; | ||
| $files[] = $stubsDir . '/Psr/Cache/CacheException.stub'; | ||
| $files[] = $stubsDir . '/Psr/Cache/CacheItemInterface.stub'; | ||
| $files[] = $stubsDir . '/Psr/Cache/InvalidArgumentException.stub'; | ||
| } | ||
| if ($this->isInstalledVersionBelow('twig/twig', '3.15.0.0')) { | ||
| $files[] = $stubsDir . '/Twig/Node/Node.stub'; | ||
| } | ||
| return $files; | ||
| } | ||
| private function isInstalledVersionBelow(string $package, string $maxVersion): bool | ||
| { | ||
| if (!class_exists(InstalledVersions::class)) { | ||
| return false; | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In which case this will occur ? Should we return true, just to avoid a BC break ? ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this could happen when using a phpstan phar without loading the project composer autoloader (which likely causes issues). Otherwise, it should not happen when using composer 2+. I copied this condition from the stub loader of phpstan-doctrine. | ||
| } | ||
| try { | ||
| $installedVersion = InstalledVersions::getVersion($package); | ||
| } catch (OutOfBoundsException $e) { | ||
| return false; | ||
| } | ||
| return $installedVersion !== null && version_compare($installedVersion, $maxVersion, '<'); | ||
| } | ||
| } | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
what is the case for files which still are referenced in extension.neon?
are these required for every symfony version?
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.
See #481 (comment) where I explain that