diff --git a/stubs/Drupal/Component/Plugin/ConfigurableInterface.stub b/stubs/Drupal/Component/Plugin/ConfigurableInterface.stub new file mode 100644 index 00000000..60dcda19 --- /dev/null +++ b/stubs/Drupal/Component/Plugin/ConfigurableInterface.stub @@ -0,0 +1,24 @@ + + */ + public function getConfiguration(); + + /** + * @param array $configuration + * + * @return void|static + */ + public function setConfiguration(array $configuration); + + /** + * @return array + */ + public function defaultConfiguration(); + +} diff --git a/stubs/Drupal/Component/Plugin/Definition/PluginDefinitionInterface.stub b/stubs/Drupal/Component/Plugin/Definition/PluginDefinitionInterface.stub new file mode 100644 index 00000000..3dec7047 --- /dev/null +++ b/stubs/Drupal/Component/Plugin/Definition/PluginDefinitionInterface.stub @@ -0,0 +1,7 @@ +|\Drupal\Component\Plugin\Definition\PluginDefinitionInterface $base_plugin_definition + * + * @return array|null + */ + public function getDerivativeDefinition($derivative_id, $base_plugin_definition); + + /** + * @param array|\Drupal\Component\Plugin\Definition\PluginDefinitionInterface $base_plugin_definition + * + * @return array + */ + public function getDerivativeDefinitions($base_plugin_definition); + +} diff --git a/stubs/Drupal/Component/Plugin/PluginBase.stub b/stubs/Drupal/Component/Plugin/PluginBase.stub index fab12b46..f1c4987e 100644 --- a/stubs/Drupal/Component/Plugin/PluginBase.stub +++ b/stubs/Drupal/Component/Plugin/PluginBase.stub @@ -4,4 +4,11 @@ namespace Drupal\Component\Plugin; abstract class PluginBase implements PluginInspectionInterface, DerivativeInspectionInterface { + /** + * @param array $configuration + * @param string $plugin_id + * @param mixed $plugin_definition + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition) {} + } diff --git a/stubs/Drupal/Core/Entity/EntityAccessControlHandler.stub b/stubs/Drupal/Core/Entity/EntityAccessControlHandler.stub new file mode 100644 index 00000000..646fec86 --- /dev/null +++ b/stubs/Drupal/Core/Entity/EntityAccessControlHandler.stub @@ -0,0 +1,27 @@ + + */ + protected $accessCache = []; + + /** + * @param array $context + * @param string|null $entity_bundle + * + * @return \Drupal\Core\Access\AccessResultInterface + */ + protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {} + + /** + * @param array $context + */ + protected function buildCreateAccessCid(array $context, ?string $entity_bundle): ?string {} + +} diff --git a/stubs/Drupal/Core/Entity/EntityAccessControlHandlerInterface.stub b/stubs/Drupal/Core/Entity/EntityAccessControlHandlerInterface.stub new file mode 100644 index 00000000..ad14e568 --- /dev/null +++ b/stubs/Drupal/Core/Entity/EntityAccessControlHandlerInterface.stub @@ -0,0 +1,7 @@ + $form + * + * @return array + */ + public function form(array $form, FormStateInterface $form_state) {} + +} diff --git a/stubs/Drupal/Core/Entity/EntityFormInterface.stub b/stubs/Drupal/Core/Entity/EntityFormInterface.stub new file mode 100644 index 00000000..d3471acd --- /dev/null +++ b/stubs/Drupal/Core/Entity/EntityFormInterface.stub @@ -0,0 +1,24 @@ + $form + * + * @return \Drupal\Core\Entity\EntityInterface + */ + public function buildEntity(array $form, FormStateInterface $form_state); + + /** + * @param array $form + * + * @return int + */ + public function save(array $form, FormStateInterface $form_state); + +} diff --git a/stubs/Drupal/Core/Entity/EntityListBuilder.stub b/stubs/Drupal/Core/Entity/EntityListBuilder.stub new file mode 100644 index 00000000..acf8d0d2 --- /dev/null +++ b/stubs/Drupal/Core/Entity/EntityListBuilder.stub @@ -0,0 +1,27 @@ + + */ + protected function getDefaultOperations(EntityInterface $entity) {} + + /** + * @return array + */ + public function buildHeader() {} + + /** + * @return array + */ + public function buildRow(EntityInterface $entity) {} + + /** + * @return array + */ + public function buildOperations(EntityInterface $entity) {} + +} diff --git a/stubs/Drupal/Core/Entity/EntityListBuilderInterface.stub b/stubs/Drupal/Core/Entity/EntityListBuilderInterface.stub new file mode 100644 index 00000000..06ee4f9f --- /dev/null +++ b/stubs/Drupal/Core/Entity/EntityListBuilderInterface.stub @@ -0,0 +1,17 @@ + + */ + public function getOperations(EntityInterface $entity); + + /** + * @return array + */ + public function render(); + +} diff --git a/stubs/Drupal/Core/Entity/FieldableEntityInterface.stub b/stubs/Drupal/Core/Entity/FieldableEntityInterface.stub index 7450286a..8a82a09d 100644 --- a/stubs/Drupal/Core/Entity/FieldableEntityInterface.stub +++ b/stubs/Drupal/Core/Entity/FieldableEntityInterface.stub @@ -15,6 +15,8 @@ interface FieldableEntityInterface extends EntityInterface { public static function baseFieldDefinitions(EntityTypeInterface $entity_type): array; /** + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * @param string $bundle * @param array $base_field_definitions * @return array */ diff --git a/stubs/Drupal/Core/Field/FieldItemInterface.stub b/stubs/Drupal/Core/Field/FieldItemInterface.stub index a46bae86..a7bcd5cf 100644 --- a/stubs/Drupal/Core/Field/FieldItemInterface.stub +++ b/stubs/Drupal/Core/Field/FieldItemInterface.stub @@ -2,7 +2,39 @@ namespace Drupal\Core\Field; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\TypedData\ComplexDataInterface; interface FieldItemInterface extends ComplexDataInterface { + + /** + * @return array + */ + public static function schema(FieldStorageDefinitionInterface $field_definition); + + /** + * @param array $form + * @param bool $has_data + * + * @return array + */ + public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data); + + /** + * @return array + */ + public static function defaultFieldSettings(); + + /** + * @param array $form + * + * @return array + */ + public function fieldSettingsForm(array $form, FormStateInterface $form_state); + + /** + * @return array + */ + public static function generateSampleValue(FieldDefinitionInterface $field_definition); + } diff --git a/stubs/Drupal/Core/Field/FieldStorageDefinitionInterface.stub b/stubs/Drupal/Core/Field/FieldStorageDefinitionInterface.stub new file mode 100644 index 00000000..880562ff --- /dev/null +++ b/stubs/Drupal/Core/Field/FieldStorageDefinitionInterface.stub @@ -0,0 +1,7 @@ + $settings + * @param string $label + * @param string $view_mode + * @param array $third_party_settings + */ + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings) {} + } diff --git a/stubs/Drupal/Core/Field/PluginSettingsInterface.stub b/stubs/Drupal/Core/Field/PluginSettingsInterface.stub index bda09167..95195d3f 100644 --- a/stubs/Drupal/Core/Field/PluginSettingsInterface.stub +++ b/stubs/Drupal/Core/Field/PluginSettingsInterface.stub @@ -7,4 +7,9 @@ use Drupal\Core\Config\Entity\ThirdPartySettingsInterface; interface PluginSettingsInterface extends PluginInspectionInterface, ThirdPartySettingsInterface { + /** + * @return array + */ + public static function defaultSettings(); + } diff --git a/stubs/Drupal/Core/Field/WidgetBase.stub b/stubs/Drupal/Core/Field/WidgetBase.stub new file mode 100644 index 00000000..e95c93e0 --- /dev/null +++ b/stubs/Drupal/Core/Field/WidgetBase.stub @@ -0,0 +1,28 @@ + $settings + * @param array $third_party_settings + */ + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings) {} + + /** + * @param \Drupal\Core\Field\FieldItemListInterface<\Drupal\Core\Field\FieldItemInterface> $items + * @param int $delta + * @param array $element + * @param array $form + * + * @return array + */ + protected function formSingleElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {} + +} diff --git a/stubs/Drupal/Core/Field/WidgetBaseInterface.stub b/stubs/Drupal/Core/Field/WidgetBaseInterface.stub new file mode 100644 index 00000000..c04e2d42 --- /dev/null +++ b/stubs/Drupal/Core/Field/WidgetBaseInterface.stub @@ -0,0 +1,7 @@ + $form + * + * @return array + */ + public function settingsForm(array $form, FormStateInterface $form_state); + + /** + * @return array + */ + public function settingsSummary(); + + /** + * @param \Drupal\Core\Field\FieldItemListInterface<\Drupal\Core\Field\FieldItemInterface> $items + * @param int $delta + * @param array $element + * @param array $form + * + * @return array + */ + public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state); + + /** + * @param array $element + * @param array $form + * + * @return array|false + */ + public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state); + + /** + * @param array $values + * @param array $form + * + * @return array + */ + public function massageFormValues(array $values, array $form, FormStateInterface $form_state); + +} diff --git a/stubs/Drupal/Core/Form/BaseFormIdInterface.stub b/stubs/Drupal/Core/Form/BaseFormIdInterface.stub new file mode 100644 index 00000000..1d00e58d --- /dev/null +++ b/stubs/Drupal/Core/Form/BaseFormIdInterface.stub @@ -0,0 +1,7 @@ + + */ + abstract protected function getEditableConfigNames(); + +} diff --git a/stubs/Drupal/Core/Form/FormBase.stub b/stubs/Drupal/Core/Form/FormBase.stub new file mode 100644 index 00000000..c4f3ce03 --- /dev/null +++ b/stubs/Drupal/Core/Form/FormBase.stub @@ -0,0 +1,7 @@ + $form + * + * @return array + */ + public function buildForm(array $form, FormStateInterface $form_state); + +} diff --git a/stubs/Drupal/Core/Form/FormStateInterface.stub b/stubs/Drupal/Core/Form/FormStateInterface.stub new file mode 100644 index 00000000..b651310c --- /dev/null +++ b/stubs/Drupal/Core/Form/FormStateInterface.stub @@ -0,0 +1,7 @@ + $configuration + * @param string $plugin_id + * @param mixed $plugin_definition + * + * @return static + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition); + } diff --git a/stubs/Drupal/Core/Plugin/PluginBase.stub b/stubs/Drupal/Core/Plugin/PluginBase.stub index 151d13f7..899fde40 100644 --- a/stubs/Drupal/Core/Plugin/PluginBase.stub +++ b/stubs/Drupal/Core/Plugin/PluginBase.stub @@ -3,7 +3,17 @@ namespace Drupal\Core\Plugin; use Drupal\Component\Plugin\PluginBase as ComponentPluginBase; +use Symfony\Component\DependencyInjection\ContainerInterface; abstract class PluginBase extends ComponentPluginBase { + /** + * @param array $configuration + * @param string $plugin_id + * @param mixed $plugin_definition + * + * @return static + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {} + } diff --git a/stubs/Drupal/Core/Plugin/PluginFormInterface.stub b/stubs/Drupal/Core/Plugin/PluginFormInterface.stub new file mode 100644 index 00000000..778888bb --- /dev/null +++ b/stubs/Drupal/Core/Plugin/PluginFormInterface.stub @@ -0,0 +1,23 @@ + $form + * + * @return array + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state); + + /** + * @param array $form + * + * @return void + */ + public function validateConfigurationForm(array &$form, FormStateInterface $form_state); + +} diff --git a/stubs/Drupal/Core/Render/MainContent/MainContentRendererInterface.stub b/stubs/Drupal/Core/Render/MainContent/MainContentRendererInterface.stub new file mode 100644 index 00000000..9577e8b7 --- /dev/null +++ b/stubs/Drupal/Core/Render/MainContent/MainContentRendererInterface.stub @@ -0,0 +1,17 @@ + $main_content + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function renderResponse(array $main_content, Request $request, RouteMatchInterface $route_match); + +} diff --git a/stubs/Drupal/Core/Routing/RouteMatchInterface.stub b/stubs/Drupal/Core/Routing/RouteMatchInterface.stub new file mode 100644 index 00000000..5156f0b5 --- /dev/null +++ b/stubs/Drupal/Core/Routing/RouteMatchInterface.stub @@ -0,0 +1,7 @@ +|null $parent + * + * @return static + */ + public static function createInstance($definition, $name = NULL, ?TraversableTypedDataInterface $parent = NULL); + + /** + * @return array + */ + public function getConstraints(); + } diff --git a/stubs/Drupal/views/Plugin/views/HandlerBase.stub b/stubs/Drupal/views/Plugin/views/HandlerBase.stub new file mode 100644 index 00000000..c8eeb335 --- /dev/null +++ b/stubs/Drupal/views/Plugin/views/HandlerBase.stub @@ -0,0 +1,14 @@ + $configuration + * @param string $plugin_id + * @param mixed $plugin_definition + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition) {} + +} diff --git a/stubs/Drupal/views/Plugin/views/PluginBase.stub b/stubs/Drupal/views/Plugin/views/PluginBase.stub new file mode 100644 index 00000000..8616e466 --- /dev/null +++ b/stubs/Drupal/views/Plugin/views/PluginBase.stub @@ -0,0 +1,27 @@ + + */ + protected function defineOptions() {} + + /** + * @param array $configuration + * @param string $plugin_id + * @param mixed $plugin_definition + * + * @return static + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {} + +} diff --git a/stubs/Drupal/views/Plugin/views/ViewsHandlerInterface.stub b/stubs/Drupal/views/Plugin/views/ViewsHandlerInterface.stub new file mode 100644 index 00000000..122fa13b --- /dev/null +++ b/stubs/Drupal/views/Plugin/views/ViewsHandlerInterface.stub @@ -0,0 +1,7 @@ + $form + * + * @return void + */ + public function buildOptionsForm(&$form, FormStateInterface $form_state); + + /** + * @param array|null $options + * + * @return void + */ + public function init(ViewExecutable $view, DisplayPluginBase $display, ?array &$options = NULL); + +} diff --git a/stubs/Drupal/views/Plugin/views/display/DisplayPluginBase.stub b/stubs/Drupal/views/Plugin/views/display/DisplayPluginBase.stub new file mode 100644 index 00000000..7fc162f7 --- /dev/null +++ b/stubs/Drupal/views/Plugin/views/display/DisplayPluginBase.stub @@ -0,0 +1,10 @@ + + */ +final class InheritedArrayParameterTypesTest extends DrupalRuleTestCase +{ + protected function getRule(): Rule + { + /** @phpstan-ignore phpstanApi.constructor, phpstanApi.classConstant */ + return self::getContainer()->getByType(MissingMethodParameterTypehintRule::class); + } + + public function testRule(): void + { + $this->analyse( + [__DIR__ . '/data/inherited-array-value-types.php'], + [] + ); + } +} diff --git a/tests/src/Rules/InheritedArrayReturnTypesTest.php b/tests/src/Rules/InheritedArrayReturnTypesTest.php new file mode 100644 index 00000000..76601849 --- /dev/null +++ b/tests/src/Rules/InheritedArrayReturnTypesTest.php @@ -0,0 +1,30 @@ + + */ +final class InheritedArrayReturnTypesTest extends DrupalRuleTestCase +{ + protected function getRule(): Rule + { + /** @phpstan-ignore phpstanApi.constructor, phpstanApi.classConstant */ + return self::getContainer()->getByType(MissingMethodReturnTypehintRule::class); + } + + public function testRule(): void + { + $this->analyse( + [__DIR__ . '/data/inherited-array-value-types.php'], + [] + ); + } +} diff --git a/tests/src/Rules/data/inherited-array-value-types.php b/tests/src/Rules/data/inherited-array-value-types.php new file mode 100644 index 00000000..1be891f1 --- /dev/null +++ b/tests/src/Rules/data/inherited-array-value-types.php @@ -0,0 +1,110 @@ + $form + */ + public function submitForm(array &$form, FormStateInterface $form_state): void { + } + +} + +final class ExampleWidget extends WidgetBase { + + /** + * {@inheritdoc} + */ + public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { + return $element; + } + + /** + * {@inheritdoc} + */ + public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { + return $values; + } + +} + +final class ExamplePlugin extends PluginBase implements ContainerFactoryPluginInterface, PluginFormInterface { + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static($configuration, $plugin_id, $plugin_definition); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { + } + + /** + * Core returns values from some submit handlers, so the stub leaves this + * method's types to the implementation. + * + * @param array $form + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void { + } + +} + +final class ExampleListBuilder extends EntityListBuilder { + + /** + * {@inheritdoc} + */ + public function buildHeader() { + return parent::buildHeader(); + } + + /** + * {@inheritdoc} + */ + public function buildRow(EntityInterface $entity) { + return parent::buildRow($entity); + } + +}