Skip to content

Commit

Permalink
Add strict typing everywhere, use php attributes and property promotion
Browse files Browse the repository at this point in the history
* Add strict typing everywhere, use php attributes and property promotion

* Fix

* Moar in tests

* Rename fieldContext

---------

Co-authored-by: Christian Fritsch <[email protected]>
  • Loading branch information
chrfritsch and Christian Fritsch authored Jul 12, 2024
1 parent f49a604 commit b58b038
Show file tree
Hide file tree
Showing 50 changed files with 220 additions and 554 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,20 @@
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\taxonomy\TermStorageInterface;

/**
* Class to define the menu_link breadcrumb builder.
*/
class ThunderArticleBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use StringTranslationTrait;

/**
* The router request context.
*
* @var \Drupal\Core\Routing\RequestContext
*/
protected $context;

/**
* The menu link access service.
*
* @var \Drupal\Core\Access\AccessManagerInterface
*/
protected $accessManager;

/**
* The dynamic router service.
*
* @var \Symfony\Component\Routing\Matcher\RequestMatcherInterface
*/
protected $router;

/**
* The dynamic router service.
*
* @var \Drupal\Core\PathProcessor\InboundPathProcessorInterface
*/
protected $pathProcessor;

/**
* Site configFactory object.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* The title resolver.
*
* @var \Drupal\Core\Controller\TitleResolverInterface
*/
protected $titleResolver;

/**
* The current user object.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;

/**
* The entity repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;

/**
* The taxonomy storage.
*
* @var \Drupal\taxonomy\TermStorageInterface
*/
protected $termStorage;
protected TermStorageInterface $termStorage;

/**
* Constructs the ThunderArticleBreadcrumbBuilder.
Expand All @@ -93,10 +38,8 @@ class ThunderArticleBreadcrumbBuilder implements BreadcrumbBuilderInterface {
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityRepositoryInterface $entityRepository, ConfigFactoryInterface $configFactory) {
$this->entityRepository = $entityRepository;
public function __construct(EntityTypeManagerInterface $entityTypeManager, protected readonly EntityRepositoryInterface $entityRepository, protected readonly ConfigFactoryInterface $configFactory) {
$this->termStorage = $entityTypeManager->getStorage('taxonomy_term');
$this->configFactory = $configFactory;
}

/**
Expand All @@ -106,7 +49,7 @@ public function applies(RouteMatchInterface $route_match): bool {
// This breadcrumb apply only for all articles.
$parameters = $route_match->getParameters()->all();
if (($route_match->getRouteName() === 'entity.node.canonical') && is_object($parameters['node'])) {
return $parameters['node']->getType() == 'article';
return $parameters['node']->getType() === 'article';
}
return FALSE;
}
Expand All @@ -125,7 +68,7 @@ public function build(RouteMatchInterface $route_match): Breadcrumb {

// Add all parent forums to breadcrumbs.
/** @var \Drupal\taxonomy\TermInterface|NULL $term */
$term = !empty($node->field_channel) ? $node->field_channel->entity : NULL;
$term = $node->field_channel->entity ?? NULL;

$links = [];
if ($term) {
Expand All @@ -139,7 +82,7 @@ public function build(RouteMatchInterface $route_match): Breadcrumb {
$links[] = Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', ['taxonomy_term' => $term->id()]);
}
}
if (!$links || '/' . $links[0]->getUrl()->getInternalPath() != $this->configFactory->get('system.site')->get('page.front')) {
if (!$links || '/' . $links[0]->getUrl()->getInternalPath() !== $this->configFactory->get('system.site')->get('page.front')) {
array_unshift($links, Link::createFromRoute($this->t('Home'), '<front>'));
}

Expand Down
22 changes: 3 additions & 19 deletions modules/thunder_article/src/Form/NodeRevisionRevertDefaultForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,17 @@ class NodeRevisionRevertDefaultForm extends ConfirmFormBase {
*
* @var \Drupal\node\NodeInterface
*/
protected $revision;

/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;

/**
* The time service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
protected NodeInterface $revision;

/**
* Constructs a new NodeRevisionRevertForm.
*
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
* The date formatter service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
*/
public function __construct(DateFormatterInterface $date_formatter, TimeInterface $time) {
$this->dateFormatter = $date_formatter;
$this->time = $time;
public function __construct(protected readonly DateFormatterInterface $dateFormatter, protected readonly TimeInterface $time) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,20 @@ class DynamicLocalTasks extends DeriverBase implements ContainerDeriverInterface

use StringTranslationTrait;

/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;

/**
* The route provider service.
*
* @var \Drupal\Core\Routing\RouteProviderInterface
*/
protected $routeProvider;

/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* Creates an DynamicLocalTasks object.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The translation manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler service.
* @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
* @param \Drupal\Core\Routing\RouteProviderInterface $routeProvider
* The route provider service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory service.
*/
public function __construct(TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, RouteProviderInterface $route_provider, ConfigFactoryInterface $config_factory) {
public function __construct(TranslationInterface $string_translation, protected readonly ModuleHandlerInterface $moduleHandler, protected readonly RouteProviderInterface $routeProvider, protected readonly ConfigFactoryInterface $configFactory) {
$this->stringTranslation = $string_translation;
$this->moduleHandler = $module_handler;
$this->routeProvider = $route_provider;
$this->configFactory = $config_factory;
}

/**
Expand Down
14 changes: 3 additions & 11 deletions modules/thunder_article/src/ThunderNodeFormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,15 @@
/**
* Base for handler for node add/edit forms.
*/
class ThunderNodeFormHelper implements ContainerInjectionInterface {

/**
* The theme manager.
*
* @var \Drupal\Core\Theme\ThemeManagerInterface
*/
protected ThemeManagerInterface $themeManager;
readonly class ThunderNodeFormHelper implements ContainerInjectionInterface {

/**
* Constructs a ThunderNodeFormHelper object.
*
* @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
* @param \Drupal\Core\Theme\ThemeManagerInterface $themeManager
* The theme manager.
*/
final public function __construct(ThemeManagerInterface $theme_manager) {
$this->themeManager = $theme_manager;
final public function __construct(protected ThemeManagerInterface $themeManager) {
}

/**
Expand Down
4 changes: 2 additions & 2 deletions modules/thunder_gqls/src/GraphQL/DecoratableTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(?self $resolver) {
* @return string|null
* The GraphQL type name or NULL if this resolver could not determine it.
*/
abstract protected function resolve($object) : ?string;
abstract protected function resolve(mixed $object) : ?string;

/**
* Allows this type resolver to be called by the GraphQL library.
Expand All @@ -75,7 +75,7 @@ abstract protected function resolve($object) : ?string;
* @throws \RuntimeException
* When a type was passed for which no type resolver exists in the chain.
*/
public function __invoke($object) : string {
public function __invoke(mixed $object) : string {
$type = $this->resolve($object);
if ($type !== NULL) {
return $type;
Expand Down
2 changes: 1 addition & 1 deletion modules/thunder_gqls/src/GraphQL/MediaTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MediaTypeResolver extends DecoratableTypeResolver {
/**
* {@inheritdoc}
*/
protected function resolve($object) : ?string {
protected function resolve(mixed $object) : ?string {
if ($object instanceof MediaInterface) {
return 'Media' . $this->mapBundleToSchemaName($object->bundle());
}
Expand Down
2 changes: 1 addition & 1 deletion modules/thunder_gqls/src/GraphQL/PagesTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PagesTypeResolver extends DecoratableTypeResolver {
/**
* {@inheritdoc}
*/
protected function resolve($object) : ?string {
protected function resolve(mixed $object) : ?string {
if ($object instanceof NodeInterface || $object instanceof TermInterface || $object instanceof UserInterface) {
if ($object->bundle() === 'page') {
return 'BasicPage';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ParagraphsTypeResolver extends DecoratableTypeResolver {
/**
* {@inheritdoc}
*/
protected function resolve($object) : ?string {
protected function resolve(mixed $object) : ?string {
if ($object instanceof ParagraphInterface) {
return 'Paragraph' . $this->mapBundleToSchemaName($object->bundle());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,16 @@
*/
class EntityLinks extends DataProducerPluginBase implements ContainerFactoryPluginInterface {

/**
* The rendering service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;

/**
* {@inheritdoc}
*
* @codeCoverageIgnore
*/
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition): self {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new static(
$configuration,
$pluginId,
$pluginDefinition,
$plugin_id,
$plugin_definition,
$container->get('renderer')
);
}
Expand All @@ -54,21 +47,20 @@ public static function create(ContainerInterface $container, array $configuratio
*
* @param array $configuration
* The plugin configuration array.
* @param string $pluginId
* @param string $plugin_id
* The plugin id.
* @param mixed $pluginDefinition
* @param mixed $plugin_definition
* The plugin definition.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
*/
public function __construct(
array $configuration,
string $pluginId,
$pluginDefinition,
RendererInterface $renderer,
string $plugin_id,
$plugin_definition,
protected readonly RendererInterface $renderer,
) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->renderer = $renderer;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}

/**
Expand Down
Loading

0 comments on commit b58b038

Please sign in to comment.