Skip to content

Commit

Permalink
Rector changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lochmueller committed Oct 17, 2024
1 parent ed26803 commit 91f0a43
Show file tree
Hide file tree
Showing 17 changed files with 32 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Classes/Detect/BrowserLanguageDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __invoke(DetectUserLanguagesEvent $event): void
if (str_contains($languageAndQualityStr, ';')) {
$parts = GeneralUtility::trimExplode(';', $languageAndQualityStr, true);
$languageCode = $parts[0];
$quality = isset($parts[1]) ? $parts[1] : '';
$quality = $parts[1] ?? '';
} else {
$languageCode = $languageAndQualityStr;
$quality = 'q=1.0';
Expand Down
2 changes: 1 addition & 1 deletion Classes/Detect/MaxMindDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __invoke(DetectUserLanguagesEvent $event): void

try {
$result = $provider->country($event->getRequest()->getServerParams()['REMOTE_ADDR'] ?? '');
} catch (\Exception $exception) {
} catch (\Exception) {
return;
}
$mode = $configuration->getMaxMindMode();
Expand Down
4 changes: 2 additions & 2 deletions Classes/Domain/Model/Dto/LocaleValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Lochmueller\LanguageDetection\Domain\Model\Dto;

class LocaleValueObject
class LocaleValueObject implements \Stringable
{
public function __construct(protected string $locale) {}

Expand All @@ -18,7 +18,7 @@ public function setLocale(string $locale): void
$this->locale = $locale;
}

public function __toString()
public function __toString(): string
{
return $this->getLocale();
}
Expand Down
2 changes: 1 addition & 1 deletion Classes/Event/BuildResponseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public function setResponse(ResponseInterface $response): void

public function isPropagationStopped(): bool
{
return $this->getResponse() !== null;
return $this->getResponse() instanceof \Psr\Http\Message\ResponseInterface;
}
}
2 changes: 1 addition & 1 deletion Classes/Event/NegotiateSiteLanguageEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public function setSelectedLanguage(SiteLanguage $selectedLanguage): void

public function isPropagationStopped(): bool
{
return $this->selectedLanguage !== null;
return $this->selectedLanguage instanceof \TYPO3\CMS\Core\Site\Entity\SiteLanguage;
}
}
2 changes: 1 addition & 1 deletion Classes/Handler/JsonDetectionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$this->eventDispatcher->dispatch($negotiate);

$selectedLanguage = $negotiate->getSelectedLanguage();
if ($selectedLanguage === null) {
if (!$selectedLanguage instanceof \TYPO3\CMS\Core\Site\Entity\SiteLanguage) {
return new JsonResponse($site->getDefaultLanguage()->toArray());
}

Expand Down
4 changes: 2 additions & 2 deletions Classes/Handler/LanguageDetectionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$negotiate = new NegotiateSiteLanguageEvent($site, $request, $detect->getUserLanguages());
$this->eventDispatcher->dispatch($negotiate);

if ($negotiate->getSelectedLanguage() === null) {
if (!$negotiate->getSelectedLanguage() instanceof \TYPO3\CMS\Core\Site\Entity\SiteLanguage) {
throw new NoSelectedLanguageException();
}

$response = new BuildResponseEvent($site, $request, $negotiate->getSelectedLanguage());
$this->eventDispatcher->dispatch($response);

if ($response->getResponse() === null) {
if (!$response->getResponse() instanceof \Psr\Http\Message\ResponseInterface) {
throw new NoResponseException();
}

Expand Down
2 changes: 1 addition & 1 deletion Classes/Handler/LinkLanguageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$negotiate = new NegotiateSiteLanguageEvent($site, $request, $detect->getUserLanguages());
$this->eventDispatcher->dispatch($negotiate);

if ($negotiate->getSelectedLanguage() === null) {
if (!$negotiate->getSelectedLanguage() instanceof \TYPO3\CMS\Core\Site\Entity\SiteLanguage) {
throw new NoSelectedLanguageException();
}

Expand Down
2 changes: 1 addition & 1 deletion Classes/Middleware/JsonDetectionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
{
try {
return $this->jsonDetectionHandler->handle($request);
} catch (AbstractHandlerException $exception) {
} catch (AbstractHandlerException) {
return $handler->handle($request);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Classes/Middleware/LanguageDetectionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
{
try {
return $this->languageDetectionHandler->handle($request);
} catch (AbstractHandlerException $exception) {
} catch (AbstractHandlerException) {
return $handler->handle($request);
}
}
Expand Down
6 changes: 3 additions & 3 deletions Classes/Service/IpLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public function getCountryCode(string $ip): ?string
}
$result = (array)unserialize((string)$response->getBody(), ['allowed_classes' => false]);

if (empty($result) || (int)$result['geoplugin_status'] === 404) {
if ($result === [] || (int)$result['geoplugin_status'] === 404) {
throw new IpLocationException('No valid data', 162378);
}

return $result['geoplugin_countryCode'] ?? null;
} catch (IpLocationException $exc) {
} catch (IpLocationException) {
return null;
}
}
Expand All @@ -43,6 +43,6 @@ protected function getClient(): \Psr\Http\Client\ClientInterface
if (GeneralUtility::makeInstance(Typo3Version::class)->getMajorVersion() >= 12) {
return GeneralUtility::makeInstance(GuzzleClientFactory::class)->getClient();
}
return GuzzleClientFactory::getClient();
return (new GuzzleClientFactory())->getClient();
}
}
4 changes: 2 additions & 2 deletions Classes/Service/RespectLanguageLinkDetailsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function addLanguageParameterByDetection(array $linkDetails, ?ServerReque
return $linkDetails;
}

if ($request === null) {
if (!$request instanceof \Psr\Http\Message\ServerRequestInterface) {
$request = ServerRequestFactory::fromGlobals();
$request = $request->withMethod('GET')->withUri(new Uri('/'));
}
Expand All @@ -43,7 +43,7 @@ public function addLanguageParameterByDetection(array $linkDetails, ?ServerReque
$linkDetails['parameters'] = 'L=' . $response->getHeaderLine(LinkLanguageHandler::HEADER_NAME);

return $linkDetails;
} catch (AbstractHandlerException $exception) {
} catch (AbstractHandlerException) {
return $linkDetails;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Classes/Service/TcaLanguageSelection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TcaLanguageSelection
{
public function __construct(protected ?SiteFinder $siteFinder = null)
{
if ($siteFinder === null) {
if (!$siteFinder instanceof \TYPO3\CMS\Core\Site\SiteFinder) {
/** @var SiteFinder $siteFinder */
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
$this->siteFinder = $siteFinder;
Expand All @@ -30,7 +30,7 @@ public function get(array &$configuration): void

try {
$site = $this->siteFinder->getSiteByIdentifier($configuration['row']['identifier']);
} catch (SiteNotFoundException $exception) {
} catch (SiteNotFoundException) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/Unit/Detect/MaxMindDetectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ public function testMemoryConsumptionAndExecutionTimeOfMaxMindDatabaseFileUpload
'languageDetectionMaxMindDatabasePath' => $dbFile,
]);

self::assertExecutionTimeLessThenOrEqual(0.3, function () use ($site, $serverRequest) {
self::assertExecutionTimeLessThenOrEqual(0.3, function () use ($site, $serverRequest): void {
$event = new DetectUserLanguagesEvent($site, $serverRequest);
$event->setUserLanguages(LocaleCollection::fromArray([]));
$maxMindDetect = new MaxMindDetect(new LanguageService(), new SiteConfigurationService(), new LocaleCollectionSortService());
$maxMindDetect($event);
});

self::assertExecutionMemoryLessThenOrEqual(400, function () use ($site, $serverRequest) {
self::assertExecutionMemoryLessThenOrEqual(400, function () use ($site, $serverRequest): void {
$event = new DetectUserLanguagesEvent($site, $serverRequest);
$event->setUserLanguages(LocaleCollection::fromArray([]));
$maxMindDetect = new MaxMindDetect(new LanguageService(), new SiteConfigurationService(), new LocaleCollectionSortService());
Expand Down
4 changes: 2 additions & 2 deletions Tests/Unit/Response/DefaultResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function testConfigurationWithWrongErrorCode(): void
$defaultResponse($event);

self::assertNotNull($event->getResponse());
if ($event->getResponse() !== null) {
if ($event->getResponse() instanceof \Psr\Http\Message\ResponseInterface) {
self::assertEquals(307, $event->getResponse()->getStatusCode());
}
}
Expand Down Expand Up @@ -117,7 +117,7 @@ public function testConfigurationWithRedirectParams(): void
$defaultResponse($event);

self::assertNotNull($event->getResponse());
if ($event->getResponse() !== null) {
if ($event->getResponse() instanceof \Psr\Http\Message\ResponseInterface) {
self::assertEquals(307, $event->getResponse()->getStatusCode());
self::assertEquals('/en/?test=1', $event->getResponse()->getHeaderLine('location'));
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/Unit/Service/RespectLanguageLinkDetailsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ protected function createTraitMock(?LinkLanguageHandler $linkLanguageHandler = n
$traitObject = $this->getObjectForTrait(RespectLanguageLinkDetailsTrait::class);
$reflectionClass = new \ReflectionClass($traitObject);

if ($linkLanguageHandler !== null) {
if ($linkLanguageHandler instanceof \Lochmueller\LanguageDetection\Handler\LinkLanguageHandler) {
$propertyLanguageEventDispatcher = $reflectionClass->getProperty('linkLanguageHandler');
$propertyLanguageEventDispatcher->setAccessible(true);
$propertyLanguageEventDispatcher->setValue($traitObject, $linkLanguageHandler);
}

if ($languageSiteFinder !== null) {
if ($languageSiteFinder instanceof \TYPO3\CMS\Core\Site\SiteFinder) {
$propertyLanguageSiteFinder = $reflectionClass->getProperty('languageSiteFinder');
$propertyLanguageSiteFinder->setAccessible(true);
$propertyLanguageSiteFinder->setValue($traitObject, $languageSiteFinder);
Expand Down
25 changes: 7 additions & 18 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,26 @@
use Ssch\TYPO3Rector\Set\Typo3SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
return static function (\Rector\Config\RectorConfig $config): void {

$containerConfigurator->import(Typo3LevelSetList::UP_TO_TYPO3_12);
$containerConfigurator->import(SetList::CODE_QUALITY);
$containerConfigurator->import(SetList::TYPE_DECLARATION);
$containerConfigurator->import(SetList::TYPE_DECLARATION_STRICT);
$containerConfigurator->import(LevelSetList::UP_TO_PHP_80);
$config->import(Typo3LevelSetList::UP_TO_TYPO3_13);
$config->import(SetList::CODE_QUALITY);
$config->import(SetList::TYPE_DECLARATION);
$config->import(LevelSetList::UP_TO_PHP_81);

$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, Typo3Option::PHPSTAN_FOR_RECTOR_PATH);
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
$parameters->set(Option::IMPORT_SHORT_CLASSES, false);
$parameters->set(Option::IMPORT_DOC_BLOCKS, false);

$parameters->set(Option::PATHS, [
$config->paths([
'ext_emconf.php',
'composer.json',
__DIR__ . '/Classes/',
__DIR__ . '/Configuration/',
__DIR__ . '/Tests/',
]);

$parameters->set(Option::SKIP, [
$config->skip([
NameImportingPostRector::class => [
'ext_emconf.php',
'ext_localconf.php',
'ext_tables.php',
],
]);

$services = $containerConfigurator->services();
$services->set(ExtEmConfRector::class);
$services->set(ExtensionComposerRector::class);
};

0 comments on commit 91f0a43

Please sign in to comment.