Skip to content

Commit

Permalink
Fix Linters (phpstan, cspell, ...)
Browse files Browse the repository at this point in the history
  • Loading branch information
WengerK committed May 17, 2024
1 parent 7bda384 commit eb56639
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 30 deletions.
52 changes: 52 additions & 0 deletions .cspell-project-words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
againts
Antistatique
antistatique
Artikel
cleancode
codesize
csslint
Curent
currentuser
Cyclomatic
dans
derivated
drupaldatetime
Exceptend
Futur
heure
heures
interesed
Jahr
Jahren
Jeudi
jours
Juillet
Langauge
libbz
linkfield
Minuten
mois
Monat
Monaten
passowrd
PHPCPD
phpcpd
phpdd
phplint
raulfraile
rempalcement
reseting
rulesets
Sekunde
Sekunden
Stichworte
Stunde
Stunden
Tagen
Truncat
truncat
unmatch
unusedcode
wapmorgan
wengerk
whitout
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- add phpstan.neon file
- add cpsell project words for Gitlab-CI

### Changed
- remove usage of deprecated ContainerAware class
- add getRequestStack & getTitleResolver to TwigExtensionBase
- improve type-hinting using RevisionableStorageInterface for Loader::loadEntityRevision && Render::renderEntityRevision
- remove usage of deprecated twig_date_converter function when Twig 3.9 is installed

### Fixed
- fix tests running on 10.3-dev with rendered webp image

## [6.0.1] - 2024-03-01
### Changed
Expand Down
3 changes: 1 addition & 2 deletions bamboo_twig.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ services:
bamboo_twig.twig.base:
private: true
class: Drupal\bamboo_twig\TwigExtension\TwigExtensionBase
calls:
- [setContainer, ['@service_container']]
arguments: ['@service_container']
19 changes: 16 additions & 3 deletions bamboo_twig_extensions/src/TwigExtension/TwigDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Template\TwigEnvironment;
use Twig\Extension\AbstractExtension;
use Twig\Extension\CoreExtension;
use Twig\TwigFilter;

/**
Expand Down Expand Up @@ -64,16 +65,28 @@ public function getName() {
* The results as string or integer depend of the $humanize param.
*/
public function diff(TwigEnvironment $env, $date, $now = NULL, $unit = NULL, $humanize = TRUE) {

// Convert both dates to DateTime instances.
$date = twig_date_converter($env, $date);
$now = twig_date_converter($env, $now);
if (method_exists(CoreExtension::class, 'convertDate')) {
$date = $env->getExtension(CoreExtension::class)->convertDate($date);
$now = $env->getExtension(CoreExtension::class)->convertDate($now);
}
elseif (method_exists(CoreExtension::class, 'dateConverter')) {
$date = CoreExtension::dateConverter($env, $date);
$now = CoreExtension::dateConverter($env, $date);
}
else {
$date = twig_date_converter($env, $date);
$now = twig_date_converter($env, $now);
}

// Get the difference between the two DateTime objects.
$diff = $date->diff($now);

$count = 0;

// Check existing units.
$duration = NULL;
if ($unit != NULL && array_key_exists($unit, self::$units)) {
$count = $this->getIntervalUnits($diff, $unit);
$duration = self::$units[$unit];
Expand All @@ -88,7 +101,7 @@ public function diff(TwigEnvironment $env, $date, $now = NULL, $unit = NULL, $hu
}
}

if ($humanize) {
if ($humanize && $duration) {
return $this->humanize($count, $diff->invert, $duration);
}

Expand Down
13 changes: 12 additions & 1 deletion bamboo_twig_i18n/src/TwigExtension/I18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\bamboo_twig\TwigExtension\TwigExtensionBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Template\TwigEnvironment;
use Twig\Extension\CoreExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;

Expand Down Expand Up @@ -82,7 +83,17 @@ public function getCurrentLanguage() {
* contain user input, this value should be escaped when output.
*/
public function formatDate(TwigEnvironment $env, $date, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
$date = twig_date_converter($env, $date);
// Convert both dates to DateTime instances.
if (method_exists(CoreExtension::class, 'convertDate')) {
$date = $env->getExtension(CoreExtension::class)->convertDate($date);
}
elseif (method_exists(CoreExtension::class, 'dateConverter')) {
$date = CoreExtension::dateConverter($env, $date);
}
else {
$date = twig_date_converter($env, $date);
}

if ($date instanceof \DateTime) {
return $this->getDateFormatter()->format($date->getTimestamp(), $type, $format, $timezone, $langcode);
}
Expand Down
8 changes: 7 additions & 1 deletion bamboo_twig_loader/src/TwigExtension/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\bamboo_twig\TwigExtension\TwigExtensionBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\RevisionableStorageInterface;
use Twig\TwigFunction;

/**
Expand Down Expand Up @@ -89,8 +90,13 @@ public function loadEntity($entity_type, $id = NULL, $langcode = NULL) {
public function loadEntityRevision($entity_type, $revision_id = NULL, $langcode = NULL) {
$entityRepository = $this->getEntityRepository();

$storage = $this->getEntityTypeManager()->getStorage($entity_type);
if (!$storage instanceof RevisionableStorageInterface) {
return NULL;
}

$revision = $revision_id ?
$this->getEntityTypeManager()->getStorage($entity_type)->loadRevision($revision_id) :
$storage->loadRevision($revision_id) :
$this->getCurrentRouteMatch()->getParameter($entity_type . '_revision');

if (!$revision instanceof EntityInterface) {
Expand Down
18 changes: 10 additions & 8 deletions bamboo_twig_loader/src/TwigExtension/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\bamboo_twig\TwigExtension\TwigExtensionBase;
use Drupal\Core\Block\TitleBlockPluginInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\RevisionableStorageInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Routing\RouteObjectInterface;
use Twig\TwigFunction;
Expand Down Expand Up @@ -152,8 +153,14 @@ public function renderEntity($entity_type, $id = NULL, $view_mode = '', $langcod
* exists.
*/
public function renderEntityRevision($entity_type, $revision_id = NULL, $view_mode = '', $langcode = NULL) {
$storage = $this->getEntityTypeManager()->getStorage($entity_type);

if (!$storage instanceof RevisionableStorageInterface) {
return NULL;
}

$revision = $revision_id ?
$this->getEntityTypeManager()->getStorage($entity_type)->loadRevision($revision_id) :
$storage->loadRevision($revision_id) :
$this->getCurrentRouteMatch()->getParameter($entity_type . '_revision');

if (!$revision instanceof EntityInterface) {
Expand Down Expand Up @@ -225,11 +232,6 @@ public function renderImageStyle($path, $style, $preprocess = FALSE) {
// From an uri or path retrieve an image object.
$image = $this->getImageFactory()->get($path);

// Assert the image exist, otherwise return null.
if (empty($image)) {
return NULL;
}

// Lazy load the fso.
$fso = $this->getFileSystemObject();

Expand Down Expand Up @@ -296,10 +298,10 @@ public function renderRegion($region, $theme = NULL) {
foreach ($blocks as $id => $block) {
$block_plugin = $block->getPlugin();
if ($block_plugin instanceof TitleBlockPluginInterface) {
$request = $this->requestStack->getCurrentRequest();
$request = $this->getRequestStack()->getCurrentRequest();
$route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT);
if ($route) {
$block_plugin->setTitle($this->titleResolver->getTitle($request, $route));
$block_plugin->setTitle($this->getTitleResolver()->getTitle($request, $route));
}
}
$build[$id] = $view_builder->view($block);
Expand Down
18 changes: 18 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
includes:
- phar://phpstan.phar/conf/bleedingEdge.neon

parameters:
level: 1
reportUnmatchedIgnoredErrors: false
ignoreErrors:
# new static() is a best practice in Drupal, so we cannot fix that.
- "#^Unsafe usage of new static#"
# The module support Drupal 10 and Drupal 11, so we cannot fix that deprecation without dropping support of Twig 3.9.3 and below.
- "#^Call to deprecated function twig_date_converter#"
# The module support Drupal 10 and Drupal 11, so we cannot fix that deprecation without dropping support of 10.1.x and below.
-
message: """
#^Usage of deprecated trait Drupal\\\\Tests\\\\field\\\\Traits\\\\EntityReferenceTestTrait#
"""
count: 1
path: tests/src/Functional/BambooTwigTestBase.php
34 changes: 32 additions & 2 deletions src/TwigExtension/TwigExtensionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Drupal\bamboo_twig\TwigExtension;

use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Mime\MimeTypes;
use Twig\Extension\AbstractExtension;

Expand All @@ -12,7 +12,17 @@
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class TwigExtensionBase extends AbstractExtension {
use ContainerAwareTrait;

/**
* The service container.
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;

public function __construct(ContainerInterface $container) {
$this->container = $container;
}

/**
* Unique identifier for this Twig extension.
Expand Down Expand Up @@ -281,4 +291,24 @@ protected function getExtensionPathResolver() {
return $this->container->get('extension.path.resolver');
}

/**
* Provides the request stack.
*
* @return \Symfony\Component\HttpFoundation\RequestStack
* The request stack.
*/
protected function getRequestStack() {
return $this->container->get('request_stack');
}

/**
* Provides the title resolver.
*
* @return \Drupal\Core\Controller\TitleResolverInterface
* The title resolver.
*/
protected function getTitleResolver() {
return $this->container->get('title_resolver');
}

}
24 changes: 12 additions & 12 deletions tests/src/Kernel/Render/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ public function testRenderImageFile() {
$markup = $this->renderer->renderRoot($renderer);
$this->assertInstanceOf(Markup::class, $markup);

// Since Drupal 11.x the image styles are rendered as webp.
if (version_compare(\Drupal::VERSION, '11', '>=')) {
// Since Drupal 10.3 the image styles are rendered as webp.
if (version_compare(\Drupal::VERSION, '10.3', '>=')) {
$this->assertMatchesRegularExpression('/^<img src=".*public\/antistatique\.jpg.webp\?itok=.*" \/>/', $markup->__toString());
}
else {
Expand All @@ -161,8 +161,8 @@ public function testRenderImageFile() {
$markup = $this->renderer->renderRoot($renderer);
$this->assertInstanceOf(Markup::class, $markup);

// Since Drupal 11.x the image styles are rendered as webp.
if (version_compare(\Drupal::VERSION, '11', '>=')) {
// Since Drupal 10.3 the image styles are rendered as webp.
if (version_compare(\Drupal::VERSION, '10.3', '>=')) {
$this->assertMatchesRegularExpression('/^<img src=".*public\/antistatique\.jpg.webp\?itok=.*" alt="" \/>/', $markup->__toString());
}
else {
Expand All @@ -181,8 +181,8 @@ public function testRenderImageFile() {
$markup = $this->renderer->renderRoot($renderer);
$this->assertInstanceOf(Markup::class, $markup);

// Since Drupal 11.x the image styles are rendered as webp.
if (version_compare(\Drupal::VERSION, '11', '>=')) {
// Since Drupal 10.3 the image styles are rendered as webp.
if (version_compare(\Drupal::VERSION, '10.3', '>=')) {
$this->assertMatchesRegularExpression('/^<img src=".*public\/antistatique\.jpg.webp\?itok=.*" alt="Dignissim dui dolor ipsum sapien habitant primis" \/>/', $markup->__toString());
}
else {
Expand Down Expand Up @@ -213,8 +213,8 @@ public function testRenderImageMedia() {
$markup = $this->renderer->renderRoot($renderer);
$this->assertInstanceOf(Markup::class, $markup);

// Since Drupal 11.x the image styles are rendered as webp.
if (version_compare(\Drupal::VERSION, '11', '>=')) {
// Since Drupal 10.3 the image styles are rendered as webp.
if (version_compare(\Drupal::VERSION, '10.3', '>=')) {
$this->assertMatchesRegularExpression('/^<img src=".*public\/antistatique\.jpg.webp\?itok=.*" \/>/', $markup->__toString());
}
else {
Expand All @@ -233,8 +233,8 @@ public function testRenderImageMedia() {
$markup = $this->renderer->renderRoot($renderer);
$this->assertInstanceOf(Markup::class, $markup);

// Since Drupal 11.x the image styles are rendered as webp.
if (version_compare(\Drupal::VERSION, '11', '>=')) {
// Since Drupal 10.3 the image styles are rendered as webp.
if (version_compare(\Drupal::VERSION, '10.3', '>=')) {
$this->assertMatchesRegularExpression('/^<img src=".*public\/antistatique\.jpg.webp\?itok=.*" \/>/', $markup->__toString());
}
else {
Expand All @@ -253,8 +253,8 @@ public function testRenderImageMedia() {
$markup = $this->renderer->renderRoot($renderer);
$this->assertInstanceOf(Markup::class, $markup);

// Since Drupal 11.x the image styles are rendered as webp.
if (version_compare(\Drupal::VERSION, '11', '>=')) {
// Since Drupal 10.3 the image styles are rendered as webp.
if (version_compare(\Drupal::VERSION, '10.3', '>=')) {
$this->assertMatchesRegularExpression('/^<img src=".*public\/antistatique\.jpg.webp\?itok=.*" alt="Dignissim dui dolor ipsum sapien habitant primis" \/>/', $markup->__toString());
}
else {
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Kernel/RenderFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function testFormWithParams(string $form_class, string $expected_form_id,
/**
* Tests the renderForm() method with parameter(s).
*/
public function providerFormParameters() {
public static function providerFormParameters() {
yield [
'ArrayParamForm', 'bamboo_twig_test_array_form', [
'text' => 'foobar',
Expand Down

0 comments on commit eb56639

Please sign in to comment.