From ce8faf8b8c15922e1c469842e1aff41768e3db97 Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Tue, 2 Jul 2024 10:09:21 +0200 Subject: [PATCH 1/3] Extracted abstract for redundant Host & URI text matchers --- .../Symfony/SiteAccess/Matcher/HostText.php | 44 +++--------- .../Matcher/PrefixSuffixBasedTextMatcher.php | 43 ++++++++++++ .../Symfony/SiteAccess/Matcher/URIText.php | 67 ++++--------------- 3 files changed, 68 insertions(+), 86 deletions(-) create mode 100644 src/lib/MVC/Symfony/SiteAccess/Matcher/PrefixSuffixBasedTextMatcher.php diff --git a/src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php b/src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php index 7bba712087..f1353562c8 100644 --- a/src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php +++ b/src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php @@ -10,46 +10,24 @@ use Ibexa\Core\MVC\Symfony\Routing\SimplifiedRequest; use Ibexa\Core\MVC\Symfony\SiteAccess\VersatileMatcher; -class HostText extends Regex implements VersatileMatcher +class HostText extends PrefixSuffixBasedTextMatcher { - private $prefix; - - private $suffix; - - /** - * The property needed to allow correct deserialization with Symfony serializer. - * - * @var array - */ - private $siteAccessesConfiguration; + protected function buildRegex(): string + { + return '^' . preg_quote($this->prefix, '@') . "(\w+)" . preg_quote($this->suffix, '@') . '$'; + } - /** - * Constructor. - * - * @param array $siteAccessesConfiguration SiteAccesses configuration. - */ - public function __construct(array $siteAccessesConfiguration) + protected function getMatchedItemNumber(): int { - $this->prefix = isset($siteAccessesConfiguration['prefix']) ? $siteAccessesConfiguration['prefix'] : ''; - $this->suffix = isset($siteAccessesConfiguration['suffix']) ? $siteAccessesConfiguration['suffix'] : ''; - parent::__construct( - '^' . preg_quote($this->prefix, '@') . "(\w+)" . preg_quote($this->suffix, '@') . '$', - 1 - ); - $this->siteAccessesConfiguration = $siteAccessesConfiguration; + return 1; } - public function getName() + public function getName(): string { return 'host:text'; } - /** - * Injects the request object to match against. - * - * @param \Ibexa\Core\MVC\Symfony\Routing\SimplifiedRequest $request - */ - public function setRequest(SimplifiedRequest $request) + public function setRequest(SimplifiedRequest $request): void { if (!$this->element) { $this->setMatchElement($request->host); @@ -58,14 +36,14 @@ public function setRequest(SimplifiedRequest $request) parent::setRequest($request); } - public function reverseMatch($siteAccessName) + public function reverseMatch($siteAccessName): ?VersatileMatcher { $this->request->setHost($this->prefix . $siteAccessName . $this->suffix); return $this; } - public function getRequest() + public function getRequest(): SimplifiedRequest { return $this->request; } diff --git a/src/lib/MVC/Symfony/SiteAccess/Matcher/PrefixSuffixBasedTextMatcher.php b/src/lib/MVC/Symfony/SiteAccess/Matcher/PrefixSuffixBasedTextMatcher.php new file mode 100644 index 0000000000..30ef3a0ade --- /dev/null +++ b/src/lib/MVC/Symfony/SiteAccess/Matcher/PrefixSuffixBasedTextMatcher.php @@ -0,0 +1,43 @@ +siteAccessesConfiguration = $siteAccessesConfiguration; + + $this->prefix = $this->siteAccessesConfiguration['prefix'] ?? ''; + $this->suffix = $this->siteAccessesConfiguration['suffix'] ?? ''; + + parent::__construct($this->buildRegex(), $this->getMatchedItemNumber()); + } +} diff --git a/src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php b/src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php index 963407a921..284192fdce 100644 --- a/src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php +++ b/src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php @@ -11,49 +11,24 @@ use Ibexa\Core\MVC\Symfony\SiteAccess\URILexer; use Ibexa\Core\MVC\Symfony\SiteAccess\VersatileMatcher; -class URIText extends Regex implements VersatileMatcher, URILexer +class URIText extends PrefixSuffixBasedTextMatcher implements URILexer { - /** @var string */ - private $prefix; - - /** @var string */ - private $suffix; - - /** - * The property needed to allow correct deserialization with Symfony serializer. - * - * @var array - */ - private $siteAccessesConfiguration; - - /** - * Constructor. - * - * @param array $siteAccessesConfiguration SiteAccesses configuration. - */ - public function __construct(array $siteAccessesConfiguration) + protected function buildRegex(): string { - $this->prefix = isset($siteAccessesConfiguration['prefix']) ? $siteAccessesConfiguration['prefix'] : ''; - $this->suffix = isset($siteAccessesConfiguration['suffix']) ? $siteAccessesConfiguration['suffix'] : ''; + return '^(/' . preg_quote($this->prefix, '@') . '(\w+)' . preg_quote($this->suffix, '@') . ')'; + } - parent::__construct( - '^(/' . preg_quote($this->prefix, '@') . '(\w+)' . preg_quote($this->suffix, '@') . ')', - 2 - ); - $this->siteAccessesConfiguration = $siteAccessesConfiguration; + protected function getMatchedItemNumber(): int + { + return 2; } - public function getName() + public function getName(): string { return 'uri:text'; } - /** - * Injects the request object to match against. - * - * @param \Ibexa\Core\MVC\Symfony\Routing\SimplifiedRequest $request - */ - public function setRequest(SimplifiedRequest $request) + public function setRequest(SimplifiedRequest $request): void { if (!$this->element) { $this->setMatchElement($request->pathinfo); @@ -62,28 +37,14 @@ public function setRequest(SimplifiedRequest $request) parent::setRequest($request); } - /** - * Analyses $uri and removes the siteaccess part, if needed. - * - * @param string $uri The original URI - * - * @return string The modified URI - */ - public function analyseURI($uri) + public function analyseURI($uri): string { $uri = '/' . ltrim($uri, '/'); - return preg_replace("@$this->regex@", '', $uri); + return preg_replace("@$this->regex@", '', $uri) ?? $uri; } - /** - * Analyses $linkUri when generating a link to a route, in order to have the siteaccess part back in the URI. - * - * @param string $linkUri - * - * @return string The modified link URI - */ - public function analyseLink($linkUri) + public function analyseLink($linkUri): string { $linkUri = '/' . ltrim($linkUri, '/'); $siteAccessUri = "/$this->prefix" . $this->match() . $this->suffix; @@ -91,14 +52,14 @@ public function analyseLink($linkUri) return $siteAccessUri . $linkUri; } - public function reverseMatch($siteAccessName) + public function reverseMatch($siteAccessName): ?VersatileMatcher { $this->request->setPathinfo("/{$this->prefix}{$siteAccessName}{$this->suffix}{$this->request->pathinfo}"); return $this; } - public function getRequest() + public function getRequest(): SimplifiedRequest { return $this->request; } From 134cc06c634f128cd0270fdf67a8afb3131af4fe Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Tue, 27 Aug 2024 11:47:52 +0200 Subject: [PATCH 2/3] [PHPStan] Aligned baseline with the changes --- phpstan-baseline.neon | 55 ------------------------------------------- 1 file changed, 55 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index fa13de6a13..94e053d031 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -12425,36 +12425,6 @@ parameters: count: 1 path: src/lib/MVC/Symfony/SiteAccess/Matcher/HostElement.php - - - message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\HostText\\:\\:__construct\\(\\) has parameter \\$siteAccessesConfiguration with no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php - - - - message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\HostText\\:\\:setRequest\\(\\) has no return type specified\\.$#" - count: 1 - path: src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php - - - - message: "#^Property Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\HostText\\:\\:\\$prefix has no type specified\\.$#" - count: 1 - path: src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php - - - - message: "#^Property Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\HostText\\:\\:\\$siteAccessesConfiguration is never read, only written\\.$#" - count: 1 - path: src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php - - - - message: "#^Property Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\HostText\\:\\:\\$siteAccessesConfiguration type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php - - - - message: "#^Property Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\HostText\\:\\:\\$suffix has no type specified\\.$#" - count: 1 - path: src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php - - message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\Map\\:\\:__construct\\(\\) has parameter \\$map with no value type specified in iterable type array\\.$#" count: 1 @@ -12620,31 +12590,6 @@ parameters: count: 1 path: src/lib/MVC/Symfony/SiteAccess/Matcher/URIElement.php - - - message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\URIText\\:\\:__construct\\(\\) has parameter \\$siteAccessesConfiguration with no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php - - - - message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\URIText\\:\\:analyseURI\\(\\) should return string but returns string\\|null\\.$#" - count: 1 - path: src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php - - - - message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\URIText\\:\\:setRequest\\(\\) has no return type specified\\.$#" - count: 1 - path: src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php - - - - message: "#^Property Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\URIText\\:\\:\\$siteAccessesConfiguration is never read, only written\\.$#" - count: 1 - path: src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php - - - - message: "#^Property Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\URIText\\:\\:\\$siteAccessesConfiguration type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php - - message: "#^Call to an undefined method object\\:\\:setRequest\\(\\)\\.$#" count: 1 From d647336adf2a0acb1ce6360e473759ed6bce7db1 Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Wed, 28 Aug 2024 12:12:41 +0200 Subject: [PATCH 3/3] Renamed base class to AffixBasedTextMatcher and improved code quality MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Adam Wójs Co-Authored-By: Konrad Oboza --- ...sedTextMatcher.php => AffixBasedTextMatcher.php} | 13 ++++--------- src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php | 2 +- src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php | 2 +- 3 files changed, 6 insertions(+), 11 deletions(-) rename src/lib/MVC/Symfony/SiteAccess/Matcher/{PrefixSuffixBasedTextMatcher.php => AffixBasedTextMatcher.php} (69%) diff --git a/src/lib/MVC/Symfony/SiteAccess/Matcher/PrefixSuffixBasedTextMatcher.php b/src/lib/MVC/Symfony/SiteAccess/Matcher/AffixBasedTextMatcher.php similarity index 69% rename from src/lib/MVC/Symfony/SiteAccess/Matcher/PrefixSuffixBasedTextMatcher.php rename to src/lib/MVC/Symfony/SiteAccess/Matcher/AffixBasedTextMatcher.php index 30ef3a0ade..76dc56296e 100644 --- a/src/lib/MVC/Symfony/SiteAccess/Matcher/PrefixSuffixBasedTextMatcher.php +++ b/src/lib/MVC/Symfony/SiteAccess/Matcher/AffixBasedTextMatcher.php @@ -15,14 +15,11 @@ * * @phpstan-type TSiteAccessConfigurationArray array{prefix?: string, suffix?: string} */ -abstract class PrefixSuffixBasedTextMatcher extends Regex implements VersatileMatcher +abstract class AffixBasedTextMatcher extends Regex implements VersatileMatcher { - protected string $prefix; + protected readonly string $prefix; - protected string $suffix; - - /** @phpstan-var TSiteAccessConfigurationArray */ - protected array $siteAccessesConfiguration; + protected readonly string $suffix; abstract protected function buildRegex(): string; @@ -31,10 +28,8 @@ abstract protected function getMatchedItemNumber(): int; /** * @phpstan-param TSiteAccessConfigurationArray $siteAccessesConfiguration */ - public function __construct(array $siteAccessesConfiguration) + public function __construct(private readonly array $siteAccessesConfiguration) { - $this->siteAccessesConfiguration = $siteAccessesConfiguration; - $this->prefix = $this->siteAccessesConfiguration['prefix'] ?? ''; $this->suffix = $this->siteAccessesConfiguration['suffix'] ?? ''; diff --git a/src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php b/src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php index f1353562c8..890063c3aa 100644 --- a/src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php +++ b/src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php @@ -10,7 +10,7 @@ use Ibexa\Core\MVC\Symfony\Routing\SimplifiedRequest; use Ibexa\Core\MVC\Symfony\SiteAccess\VersatileMatcher; -class HostText extends PrefixSuffixBasedTextMatcher +class HostText extends AffixBasedTextMatcher { protected function buildRegex(): string { diff --git a/src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php b/src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php index 284192fdce..ff2016298e 100644 --- a/src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php +++ b/src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php @@ -11,7 +11,7 @@ use Ibexa\Core\MVC\Symfony\SiteAccess\URILexer; use Ibexa\Core\MVC\Symfony\SiteAccess\VersatileMatcher; -class URIText extends PrefixSuffixBasedTextMatcher implements URILexer +class URIText extends AffixBasedTextMatcher implements URILexer { protected function buildRegex(): string {