Skip to content

Commit

Permalink
Extracted abstract for redundant Host & URI text matchers (#424)
Browse files Browse the repository at this point in the history
For more details see #424

Key changes:

* Extracted abstract `AffixBasedTextMatcher` for redundant Host & URI text matchers

* [PHPStan] Aligned baseline with the changes

---------

Co-authored-by: Adam Wójs <[email protected]>
Co-authored-by: Konrad Oboza <[email protected]>
  • Loading branch information
3 people authored Aug 28, 2024
1 parent 0b6ceef commit 4bed131
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 141 deletions.
55 changes: 0 additions & 55 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions src/lib/MVC/Symfony/SiteAccess/Matcher/AffixBasedTextMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Core\MVC\Symfony\SiteAccess\Matcher;

use Ibexa\Core\MVC\Symfony\SiteAccess\VersatileMatcher;

/**
* @internal
*
* @phpstan-type TSiteAccessConfigurationArray array{prefix?: string, suffix?: string}
*/
abstract class AffixBasedTextMatcher extends Regex implements VersatileMatcher
{
protected readonly string $prefix;

protected readonly string $suffix;

abstract protected function buildRegex(): string;

abstract protected function getMatchedItemNumber(): int;

/**
* @phpstan-param TSiteAccessConfigurationArray $siteAccessesConfiguration
*/
public function __construct(private readonly array $siteAccessesConfiguration)
{
$this->prefix = $this->siteAccessesConfiguration['prefix'] ?? '';
$this->suffix = $this->siteAccessesConfiguration['suffix'] ?? '';

parent::__construct($this->buildRegex(), $this->getMatchedItemNumber());
}
}
44 changes: 11 additions & 33 deletions src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
67 changes: 14 additions & 53 deletions src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -62,43 +37,29 @@ 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;

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;
}
Expand Down

0 comments on commit 4bed131

Please sign in to comment.