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 diff --git a/src/lib/MVC/Symfony/SiteAccess/Matcher/AffixBasedTextMatcher.php b/src/lib/MVC/Symfony/SiteAccess/Matcher/AffixBasedTextMatcher.php new file mode 100644 index 0000000000..76dc56296e --- /dev/null +++ b/src/lib/MVC/Symfony/SiteAccess/Matcher/AffixBasedTextMatcher.php @@ -0,0 +1,38 @@ +prefix = $this->siteAccessesConfiguration['prefix'] ?? ''; + $this->suffix = $this->siteAccessesConfiguration['suffix'] ?? ''; + + parent::__construct($this->buildRegex(), $this->getMatchedItemNumber()); + } +} diff --git a/src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php b/src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php index 7bba712087..890063c3aa 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 AffixBasedTextMatcher { - 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/URIText.php b/src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php index 963407a921..ff2016298e 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 AffixBasedTextMatcher 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; }