Skip to content

Commit

Permalink
phpstan fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tuutti committed Oct 27, 2023
1 parent a31660e commit bd7acba
Show file tree
Hide file tree
Showing 38 changed files with 219 additions and 151 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ jobs:
- name: Run PHPCS
working-directory: ${{ env.DRUPAL_ROOT }}
run: |
vendor/bin/phpcs $MODULE_FOLDER --standard=Drupal --extensions=php,module,inc,install,test,info
run: vendor/bin/phpcs $MODULE_FOLDER --standard=Drupal,DrupalPractice --extensions=php,module,install

- name: Run phpstan
working-directory: ${{ env.DRUPAL_ROOT }}
run: vendor/bin/phpstan analyze -c $MODULE_FOLDER/phpstan.neon $MODULE_FOLDER

- name: Start services
working-directory: ${{ env.DRUPAL_ROOT }}
Expand All @@ -90,6 +93,6 @@ jobs:
with:
name: results
path: |
${{ env.DRUPAL_ROOT }}/sites/simpletest/browser_output/
${{ env.DRUPAL_ROOT }}/public/sites/simpletest/browser_output/
${{ env.DRUPAL_ROOT }}/coverage.xml
retention-days: 1
20 changes: 20 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
parameters:
fileExtensions:
- php
- module
- install
paths:
- ./
excludePaths:
- vendor
level: 3
checkMissingIterableValueType: false
treatPhpDocTypesAsCertain: false
ignoreErrors:
-
message: '#^Method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\) invoked with 2 parameters, 1 required.#'
path: src/Commands/DeployCommands.php
count: 2
-
message: '#^Unsafe usage of new static#'
path: src/Plugin/migrate/source/HttpSourcePluginBase.php
12 changes: 6 additions & 6 deletions src/Azure/PubSub/PubSubManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,22 @@ public function setTimeout(int $timeout) : self {
*
* This is used to exit early if required settings are not populated.
*/
private function assertSettings() : self {
private function assertSettings() : void {
$vars = get_object_vars($this->settings);

foreach ($vars as $key => $value) {
if (empty($this->settings->{$key})) {
throw new ConnectionException("Azure PubSub '$key' is not configured.");
}
}
return $this;
}

/**
* {@inheritdoc}
*/
public function sendMessage(array $message) : self {
$this->assertSettings()
->joinGroup();
$this->assertSettings();
$this->joinGroup();

$this->client
->text(
Expand All @@ -155,8 +154,9 @@ public function sendMessage(array $message) : self {
* {@inheritdoc}
*/
public function receive() : string {
$this->assertSettings()
->joinGroup();
$this->assertSettings();
$this->joinGroup();

$message = (string) $this->client->receive();
$json = $this->decodeMessage($message);

Expand Down
5 changes: 3 additions & 2 deletions src/Azure/PubSub/SettingsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(
* The PubSub settings object.
*/
public function create() : Settings {
$data = (object) [
$data = [
'hub' => '',
'group' => '',
'endpoint' => '',
Expand All @@ -41,9 +41,10 @@ public function create() : Settings {
if (!isset($settings->data()->{$key})) {
continue;
}
$data->{$key} = $settings->data()->{$key};
$data[$key] = $settings->data()->{$key};
}
}
$data = (object) $data;

return new Settings(
$data->hub ?: '',
Expand Down
12 changes: 9 additions & 3 deletions src/Entity/Form/MenuLinkFormTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\helfi_api_base\Entity\Form;

use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuParentFormSelectorInterface;
use Drupal\Core\Site\Settings;
Expand All @@ -13,8 +14,8 @@
/**
* A trait to allow entity forms to provide a menu link form.
*
* To use this, call the ::attachMenuLinkForm() method in your
* ::buildForm() method, like:
* To use this, call the '::attachMenuLinkForm()' method in your
* '::buildForm()' method, like:
*
* @code
* $form = $this->attachMenuLinkForm($form, $form_state);
Expand Down Expand Up @@ -48,6 +49,7 @@ trait MenuLinkFormTrait {
*/
protected function getDefaultMenuLink() : MenuLinkContentInterface {
$entity = $this->getEntity();
assert($entity instanceof FieldableEntityInterface);

if (!$menuLink = $entity->get($this->menuLinkFieldName)->entity) {
$storage = $this->entityTypeManager
Expand All @@ -64,7 +66,10 @@ protected function getDefaultMenuLink() : MenuLinkContentInterface {
$menuLink = empty($results) ? MenuLinkContent::create([]) : MenuLinkContent::load(reset($results));
}

return $this->entityRepository->getTranslationFromContext($menuLink);
$entity = $this->entityRepository->getTranslationFromContext($menuLink);
assert($entity instanceof MenuLinkContentInterface);

return $entity;
}

/**
Expand Down Expand Up @@ -173,6 +178,7 @@ protected function attachMenuLinkForm(array $form, FormStateInterface $formState
*/
public function attachMenuLinkFormSubmit(array $form, FormStateInterface $formState) : void {
$entity = $this->getEntity();
assert($entity instanceof FieldableEntityInterface);
/** @var \Drupal\menu_link_content\MenuLinkContentInterface $menuLink */
$menuLink = $formState->get('menu_link');

Expand Down
19 changes: 12 additions & 7 deletions src/Entity/Form/RevisionRevertTranslationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
namespace Drupal\helfi_api_base\Entity\Form;

use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Entity\RevisionLogInterface;
use Drupal\Core\Entity\TranslatableRevisionableInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\entity\Form\RevisionRevertForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -33,7 +36,7 @@ final class RevisionRevertTranslationForm extends RevisionRevertForm {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
public static function create(ContainerInterface $container) : self {
$instance = parent::create($container);
$instance->languageManager = $container->get('language_manager');
return $instance;
Expand All @@ -42,14 +45,15 @@ public static function create(ContainerInterface $container) {
/**
* {@inheritdoc}
*/
public function getFormId() {
public function getFormId() : string {
return 'revision_revert_translation_confirm';
}

/**
* {@inheritdoc}
*/
public function getQuestion() {
public function getQuestion() : TranslatableMarkup {
assert($this->revision instanceof RevisionLogInterface);
return $this->t('Are you sure you want to revert @language translation to the revision from %revision-date?', [
'@language' => $this->languageManager->getLanguageName($this->langcode),
'%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime()),
Expand All @@ -59,14 +63,14 @@ public function getQuestion() {
/**
* {@inheritdoc}
*/
public function getDescription() {
return '';
public function getDescription() : TranslatableMarkup {
return $this->t('This action cannot be undone.');
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $_entity_revision = NULL, Request $request = NULL) {
public function buildForm(array $form, FormStateInterface $form_state, $_entity_revision = NULL, Request $request = NULL) : array {
$this->langcode = $request->attributes->get('langcode');

return parent::buildForm($form, $form_state, $_entity_revision);
Expand All @@ -75,7 +79,8 @@ public function buildForm(array $form, FormStateInterface $form_state, $_entity_
/**
* {@inheritdoc}
*/
protected function prepareRevision(RevisionableInterface $revision) {
protected function prepareRevision(RevisionableInterface $revision) : RevisionableInterface {
assert($revision instanceof TranslatableRevisionableInterface);
$translation = $revision->getTranslation($this->langcode);
return parent::prepareRevision($translation);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Entity/Revision/RevisionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\TranslatableInterface;

/**
* A class to manage revisions.
Expand Down Expand Up @@ -149,10 +150,11 @@ public function getRevisionsPerLanguage(
krsort($revision_ids);

foreach ($revision_ids as $vid) {
/** @var \Drupal\Core\Entity\RevisionableInterface $revision */
/** @var \Drupal\Core\Entity\TranslatableRevisionableInterface $revision */
$revision = $storage->loadRevision($vid);

foreach ($revision->getTranslationLanguages() as $langcode => $language) {
assert($revision instanceof TranslatableInterface);
if ($revision->hasTranslation($langcode) && $revision->getTranslation($langcode)->isRevisionTranslationAffected()) {
$revisions[$langcode][] = $revision->getLoadedRevisionId();
}
Expand Down
5 changes: 3 additions & 2 deletions src/Entity/Routing/RevisionRouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Symfony\Component\Routing\Route;

/**
* Providers extended revision routes for content entikties.
* Providers extended revision routes for content entities.
*/
final class RevisionRouteProvider extends EntityRevisionRouteProvider {

Expand All @@ -31,11 +31,12 @@ public function getRoutes(EntityTypeInterface $entity_type) {
/**
* {@inheritdoc}
*/
protected function getRevisionHistoryRoute(EntityTypeInterface $entity_type) {
protected function getRevisionHistoryRoute(EntityTypeInterface $entity_type): ?Route {
if ($route = parent::getRevisionHistoryRoute($entity_type)) {
$route->setDefault('_controller', RevisionController::class . '::revisionOverviewController');
return $route;
}
return NULL;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/EventSubscriber/MigrationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ public function onPostImport(MigrateImportEvent $event) : void {
->execute();

foreach ($results as $id) {
$storage->load($id)->delete(TRUE);
$entity = $storage->load($id);
assert($entity instanceof RemoteEntityBase);
$entity->delete(TRUE);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Logger/JsonLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function log($level, $message, array $context = []) : void {
}
// Populate the message placeholders and then replace them in the message.
$variables = $this->parser->parseMessagePlaceholders($message, $context);
$message = empty($variables) ? $message : strtr($message, $variables);
$message = empty($variables) ? $message : strtr((string) $message, $variables);

$this->output([
'base_url' => $base_url,
Expand Down
6 changes: 3 additions & 3 deletions src/Plugin/Action/MigrationUpdateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* deriver = "Drupal\helfi_api_base\Plugin\Derivative\MigrationUpdateActionDerivative",
* )
*/
class MigrationUpdateAction extends ActionBase implements ContainerFactoryPluginInterface {
final class MigrationUpdateAction extends ActionBase implements ContainerFactoryPluginInterface {

use MigrateTrait;

Expand All @@ -51,8 +51,8 @@ public static function create(
array $configuration,
$plugin_id,
$plugin_definition
) {
$instance = new static($configuration, $plugin_definition, $plugin_definition);
) : self {
$instance = new self($configuration, $plugin_definition, $plugin_definition);
$instance->migrationPluginManager = $container->get('plugin.manager.migration');
$instance->entityTypeManager = $container->get('entity_type.manager');

Expand Down
6 changes: 3 additions & 3 deletions src/Plugin/DebugDataItem/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* description = @Translation("Composer")
* )
*/
class Composer extends DebugDataItemPluginBase implements ContainerFactoryPluginInterface {
final class Composer extends DebugDataItemPluginBase implements ContainerFactoryPluginInterface {

/**
* The composer info.
Expand All @@ -38,8 +38,8 @@ class Composer extends DebugDataItemPluginBase implements ContainerFactoryPlugin
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = new static($configuration, $plugin_id, $plugin_definition);
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : self {
$instance = new self($configuration, $plugin_id, $plugin_definition);
$instance->composerInfo = $container->get('helfi_api_base.composer_info');
return $instance;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Plugin/DebugDataItem/MaintenanceMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* description = @Translation("Maintenance mode")
* )
*/
class MaintenanceMode extends DebugDataItemPluginBase implements ContainerFactoryPluginInterface {
final class MaintenanceMode extends DebugDataItemPluginBase implements ContainerFactoryPluginInterface {

/**
* The state service.
Expand All @@ -30,8 +30,8 @@ class MaintenanceMode extends DebugDataItemPluginBase implements ContainerFactor
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = new static($configuration, $plugin_id, $plugin_definition);
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : self {
$instance = new self($configuration, $plugin_id, $plugin_definition);
$instance->state = $container->get('state');
return $instance;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Plugin/DebugDataItem/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* description = @Translation("Migrate data")
* )
*/
class Migrate extends DebugDataItemPluginBase implements ContainerFactoryPluginInterface, CacheableDependencyInterface {
final class Migrate extends DebugDataItemPluginBase implements ContainerFactoryPluginInterface, CacheableDependencyInterface {

/**
* The migration plugin manager.
Expand All @@ -41,8 +41,8 @@ class Migrate extends DebugDataItemPluginBase implements ContainerFactoryPluginI
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = new static($configuration, $plugin_id, $plugin_definition);
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : self {
$instance = new self($configuration, $plugin_id, $plugin_definition);
$instance->migrationPluginManager = $container->get('plugin.manager.migration');
$instance->keyValue = $container->get('keyvalue')
->get('migrate_last_imported');
Expand Down
4 changes: 2 additions & 2 deletions src/Plugin/Derivative/MigrationUpdateActionDerivative.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Provides an action to update singular migration.
*/
class MigrationUpdateActionDerivative extends DeriverBase implements ContainerDeriverInterface {
final class MigrationUpdateActionDerivative extends DeriverBase implements ContainerDeriverInterface {

use StringTranslationTrait;

Expand All @@ -30,7 +30,7 @@ class MigrationUpdateActionDerivative extends DeriverBase implements ContainerDe
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
$instance = new static();
$instance = new self();
$instance->entityTypeManager = $container->get('entity_type.manager');

return $instance;
Expand Down
Loading

0 comments on commit bd7acba

Please sign in to comment.