Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
Issue #3265219 by chr.fritsch, volkerk: Fix coding style issues and a…
Browse files Browse the repository at this point in the history
…chieve phpstan lvl6

* feat: phpstan lvl6

* empty

* empty

* empty

* empty

* fix: stuff

* fix: foo

* fix: foo
  • Loading branch information
chrfritsch authored Feb 18, 2022
1 parent e9c637d commit f29878c
Show file tree
Hide file tree
Showing 34 changed files with 169 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ jobs:

- name: Test for deprecations
run: test-drupal-project deprecation
if: ${{ matrix.DRUPAL_TESTING_DRUPAL_VERSION != '~9.2.0' }}

- name: Install drupal
run: test-drupal-project install
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require-dev": {
"drupal/facets": "^1.5",
"drupal/facets": "^2.0",
"drupal/search_api": "^1.17",
"drupal/form_options_attributes": "^1.2",
"drupal/better_exposed_filters": "^5.0"
Expand Down
4 changes: 2 additions & 2 deletions modules/select2_facets/select2_facets.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Select2 Facets
type: module
description: Provides a Select2 Facet widget.
package: Search
core_version_requirement: ^8.8 || ^9
core_version_requirement: ^9
dependencies:
- facets:facets (>=8.x-1.5)
- facets:facets (>=8.x-2.0)
- select2:select2
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class FacetApiAutocompleteController extends ControllerBase {

/**
* {@inheritdoc}
*
* @phpstan-ignore-next-line
*/
public static function create(ContainerInterface $container) {
$controller = parent::create($container);
Expand All @@ -82,7 +84,7 @@ public static function create(ContainerInterface $container) {
* @param \Drupal\facets\FacetManager\DefaultFacetManager $facetManager
* The facet manager.
*/
protected function setFacetManager(DefaultFacetManager $facetManager) {
protected function setFacetManager(DefaultFacetManager $facetManager): void {
$this->facetManager = $facetManager;
}

Expand All @@ -92,7 +94,7 @@ protected function setFacetManager(DefaultFacetManager $facetManager) {
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* The request stack object.
*/
protected function setRequestStack(RequestStack $requestStack) {
protected function setRequestStack(RequestStack $requestStack): void {
$this->requestStack = $requestStack;
}

Expand All @@ -102,7 +104,7 @@ protected function setRequestStack(RequestStack $requestStack) {
* @param \Drupal\Core\Path\CurrentPathStack $currentPathStack
* Current path stack object.
*/
protected function setCurrentPathStack(CurrentPathStack $currentPathStack) {
protected function setCurrentPathStack(CurrentPathStack $currentPathStack): void {
$this->currentPathStack = $currentPathStack;
}

Expand All @@ -112,7 +114,7 @@ protected function setCurrentPathStack(CurrentPathStack $currentPathStack) {
* @param \Drupal\Core\Routing\AccessAwareRouterInterface $router
* The router object.
*/
protected function setRouter(AccessAwareRouterInterface $router) {
protected function setRouter(AccessAwareRouterInterface $router): void {
$this->router = $router;
}

Expand All @@ -122,7 +124,7 @@ protected function setRouter(AccessAwareRouterInterface $router) {
* @param \Drupal\Core\PathProcessor\InboundPathProcessorInterface $pathProcessor
* The path processor service object.
*/
protected function setPathProcessor(InboundPathProcessorInterface $pathProcessor) {
protected function setPathProcessor(InboundPathProcessorInterface $pathProcessor): void {
$this->pathProcessor = $pathProcessor;
}

Expand All @@ -145,11 +147,12 @@ protected function setPathProcessor(InboundPathProcessorInterface $pathProcessor
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
* Thrown if the selection settings key is not found in the key/value store
* or if it does not match the stored data.
*
* @throws \Drupal\facets\Exception\InvalidProcessorException
*/
public function handleAutocomplete(Request $request, $facetsource_id, $facet_id, $selection_settings_key) {
$matches['results'] = [];
public function handleAutocomplete(Request $request, string $facetsource_id, string $facet_id, string $selection_settings_key): JsonResponse {
$matches = [
'results' => [],
];
// Get the typed string from the URL, if it exists.
if ($input = $request->query->get('q')) {
$typed_string = mb_strtolower($input);
Expand Down Expand Up @@ -205,7 +208,7 @@ public function handleAutocomplete(Request $request, $facetsource_id, $facet_id,
* @return \Symfony\Component\HttpFoundation\Request
* A new request object.
*/
protected function createRequestFromPath($path) {
protected function createRequestFromPath(string $path): Request {
$new_request = Request::create($path);
$processed = $this->pathProcessor->processInbound($path, $new_request);
$this->currentPathStack->setPath($processed);
Expand All @@ -219,7 +222,7 @@ protected function createRequestFromPath($path) {
* @param \Symfony\Component\HttpFoundation\Request $request
* The one and only request.
*/
protected function overwriteRequestStack(Request $request) {
protected function overwriteRequestStack(Request $request): void {
while ($this->requestStack->getCurrentRequest()) {
$this->storedRequests[] = $this->requestStack->pop();
}
Expand All @@ -229,7 +232,7 @@ protected function overwriteRequestStack(Request $request) {
/**
* Restore all saved requests on the stack.
*/
protected function restoreRequestStack() {
protected function restoreRequestStack(): void {
$this->requestStack->pop();
foreach ($this->storedRequests as $request) {
$this->requestStack->push($request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Drupal\facets\Widget\WidgetPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Url;

/**
* The select2 widget.
Expand Down Expand Up @@ -49,7 +50,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new static(
$configuration,
$plugin_id,
Expand All @@ -62,7 +63,7 @@ public static function create(ContainerInterface $container, array $configuratio
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
public function defaultConfiguration(): array {
return [
'autocomplete' => FALSE,
'match_operator' => 'CONTAINS',
Expand All @@ -73,13 +74,13 @@ public function defaultConfiguration() {
/**
* {@inheritdoc}
*/
public function build(FacetInterface $facet) {
public function build(FacetInterface $facet): array {
$this->facet = $facet;

$items = [];
$active_items = [];
foreach ($facet->getResults() as $result) {
if (empty($result->getUrl())) {
if (!($result->getUrl() instanceof Url)) {
continue;
}

Expand Down Expand Up @@ -132,7 +133,7 @@ public function build(FacetInterface $facet) {
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) {
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet): array {
$form = parent::buildConfigurationForm($form, $form_state, $facet);
$form['width'] = [
'#type' => 'textfield',
Expand Down Expand Up @@ -171,7 +172,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
* @return array
* List of options.
*/
protected function getMatchOperatorOptions() {
protected function getMatchOperatorOptions(): array {
return [
'STARTS_WITH' => $this->t('Starts with'),
'CONTAINS' => $this->t('Contains'),
Expand All @@ -187,7 +188,7 @@ protected function getMatchOperatorOptions() {
* @return array
* The render element with autocomplete settings.
*/
public function processFacetAutocomplete(array &$element) {
public function processFacetAutocomplete(array &$element): array {
$selection_settings = [
'path' => $this->request->getUri(),
'match_operator' => $this->getConfiguration()['match_operator'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class FacetsTest extends WebDriverTestBase {
/**
* {@inheritdoc}
*/
protected function setUp() {
protected function setUp(): void {
parent::setUp();

$reference1 = EntityTestMulRevPub::create(['name' => 'Reference 1']);
Expand Down Expand Up @@ -59,7 +59,7 @@ protected function setUp() {
*
* @dataProvider providerTestBasicFunctionality
*/
public function testBasicFunctionality($config, $expected_settings) {
public function testBasicFunctionality(array $config, array $expected_settings): void {

$facet = Facet::load('referenced');
$facet->setWidget('select2', $config);
Expand Down Expand Up @@ -101,7 +101,7 @@ public function testBasicFunctionality($config, $expected_settings) {
* @return array
* The data.
*/
public function providerTestBasicFunctionality() {
public function providerTestBasicFunctionality(): array {
return [
[[], ['tags' => FALSE]],
[['autocomplete' => TRUE], ['ajax' => [], 'tags' => FALSE]],
Expand Down
4 changes: 2 additions & 2 deletions modules/select2_publish/select2_publish.module
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use Drupal\select2_publish\Element\StatusProperties;
/**
* Implements hook_element_info_alter().
*/
function select2_publish_element_info_alter(array &$info) {
function select2_publish_element_info_alter(array &$info): void {
if (!empty($info['select2'])) {
$info['select2']['#pre_render'][] = [StatusProperties::class, 'preRender'];
}
Expand All @@ -20,7 +20,7 @@ function select2_publish_element_info_alter(array &$info) {
/**
* Implements hook_select2_autocomplete_matches_alter().
*/
function select2_publish_select2_autocomplete_matches_alter(array &$matches, array $options) {
function select2_publish_select2_autocomplete_matches_alter(array &$matches, array $options): void {
$entity_manager = \Drupal::entityTypeManager();
$entity_definition = $entity_manager->getDefinition($options['target_type']);

Expand Down
6 changes: 3 additions & 3 deletions modules/select2_publish/src/Element/StatusProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class StatusProperties implements TrustedCallbackInterface {
/**
* {@inheritdoc}
*/
public static function trustedCallbacks() {
public static function trustedCallbacks(): array {
return ['preRender'];
}

Expand All @@ -23,13 +23,13 @@ public static function trustedCallbacks() {
* @param array $element
* The select2 render element.
*
* @return mixed
* @return array
* The select2 render element.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public static function preRender(array $element) {
public static function preRender(array $element): array {
if ($element['#target_type']) {
$entity_manager = \Drupal::entityTypeManager();
$entity_definition = $entity_manager->getDefinition($element['#target_type']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PublishTest extends Select2JavascriptTestBase {
/**
* Test autocomplete in a single value field.
*/
public function testMultipleSelection() {
public function testMultipleSelection(): void {
$this->createField('select2', 'node', 'test', 'entity_reference', [
'target_type' => 'entity_test_mulrevpub',
'cardinality' => -1,
Expand Down Expand Up @@ -59,7 +59,7 @@ public function testMultipleSelection() {
/**
* Tests that the autocomplete.
*/
public function testAutocomplete() {
public function testAutocomplete(): void {
$this->createField('select2', 'node', 'test', 'entity_reference', [
'target_type' => 'entity_test_mulrevpub',
], [
Expand Down
18 changes: 11 additions & 7 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
parameters:
customRulesetUsed: true
reportUnmatchedIgnoredErrors: false
# Ignore phpstan-drupal extension's rules.
checkGenericClassInNonGenericObjectType: false
checkMissingIterableValueType: false
reportUnmatchedIgnoredErrors: true
level: 6
ignoreErrors:
- '#\Drupal calls should be avoided in classes, use dependency injection instead#'
- '#Plugin definitions cannot be altered.#'
- '#Missing cache backend declaration for performance.#'
- '#Plugin manager has cache backend specified but does not declare cache tags.#'
#TODO: Remove when https://www.drupal.org/project/drupal/issues/2367933 is fixed.
- '#If the entity form display is available in configuration use#'
- '#\Call to an undefined method Drupal\\Tests\\WebAssert::#'
- "#^Unsafe usage of new static\\(\\)\\.$#"
- "#https:\/\/www.drupal.org\/node\/3083055#"
- "#Function drupal_phpunit_find_extension_directories not found.#"
# Drupal allows object property access to custom fields, so we cannot fix
# that.
- "#^Access to an undefined property Drupal\\\\#"
includes:
- ./vendor/mglaman/phpstan-drupal/extension.neon
- ./vendor/phpstan/phpstan-deprecation-rules/rules.neon
2 changes: 1 addition & 1 deletion select2.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ name: Select2
type: module
description: Makes entity reference fields more user-friendly using <a href="https://github.com/select2/select2">Select2</a>.
package: User interface
core_version_requirement: ^8.8 || ^9
core_version_requirement: ^9
11 changes: 3 additions & 8 deletions select2.install
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
/**
* Implements hook_requirements().
*/
function select2_requirements($phase) {
function select2_requirements(string $phase): array {
$requirements = [];
if ($phase !== 'runtime') {
return [];
}
Expand All @@ -19,13 +20,7 @@ function select2_requirements($phase) {
'severity' => REQUIREMENT_OK,
];

// library.libraries_directory_file_finder is available since D8.9.
if (\Drupal::hasService('library.libraries_directory_file_finder') && \Drupal::service('library.libraries_directory_file_finder')->find('select2')) {
return $requirements;
}

// @todo This code can be removed when we drop D8.8 support.
if (function_exists('libraries_get_path') && libraries_get_path('select2')) {
if (\Drupal::service('library.libraries_directory_file_finder')->find('select2')) {
return $requirements;
}

Expand Down
4 changes: 2 additions & 2 deletions select2.module
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Implements hook_library_info_alter().
*/
function select2_library_info_alter(&$libraries, $extension) {
function select2_library_info_alter(array &$libraries, string $extension): void {
if ($extension === 'select2') {
$libraries_path = 'libraries/select2';
if (\Drupal::hasService('library.libraries_directory_file_finder')) {
Expand Down Expand Up @@ -49,7 +49,7 @@ function select2_library_info_alter(&$libraries, $extension) {
/**
* Implements hook_library_info_build().
*/
function select2_library_info_build() {
function select2_library_info_build(): array {
$libraries = [];
foreach (\Drupal::languageManager()->getLanguages() as $language) {
if (file_exists('libraries/select2/dist/js/i18n/' . $language->getId() . '.js')) {
Expand Down
7 changes: 5 additions & 2 deletions src/Controller/EntityAutocompleteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class EntityAutocompleteController extends ControllerBase {

/**
* {@inheritdoc}
*
* @phpstan-ignore-next-line
*/
public static function create(ContainerInterface $container) {
$controller = parent::create($container);
Expand All @@ -38,7 +40,7 @@ public static function create(ContainerInterface $container) {
* @param \Drupal\select2\EntityAutocompleteMatcher $matcher
* The autocomplete matcher for entity references.
*/
protected function setMatcher(EntityAutocompleteMatcher $matcher) {
protected function setMatcher(EntityAutocompleteMatcher $matcher): void {
$this->matcher = $matcher;
}

Expand All @@ -62,7 +64,8 @@ protected function setMatcher(EntityAutocompleteMatcher $matcher) {
* Thrown if the selection settings key is not found in the key/value store
* or if it does not match the stored data.
*/
public function handleAutocomplete(Request $request, $target_type, $selection_handler, $selection_settings_key) {
public function handleAutocomplete(Request $request, string $target_type, string $selection_handler, string $selection_settings_key): JsonResponse {
$matches = [];
$matches['results'] = [];
// Get the typed string from the URL, if it exists.
$input = $request->query->get('q');
Expand Down
Loading

0 comments on commit f29878c

Please sign in to comment.