Skip to content

Commit 4bed131

Browse files
alongoszadamwojskonradoboza
authored
Extracted abstract for redundant Host & URI text matchers (#424)
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]>
1 parent 0b6ceef commit 4bed131

File tree

4 files changed

+63
-141
lines changed

4 files changed

+63
-141
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12425,36 +12425,6 @@ parameters:
1242512425
count: 1
1242612426
path: src/lib/MVC/Symfony/SiteAccess/Matcher/HostElement.php
1242712427

12428-
-
12429-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\HostText\\:\\:__construct\\(\\) has parameter \\$siteAccessesConfiguration with no value type specified in iterable type array\\.$#"
12430-
count: 1
12431-
path: src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php
12432-
12433-
-
12434-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\HostText\\:\\:setRequest\\(\\) has no return type specified\\.$#"
12435-
count: 1
12436-
path: src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php
12437-
12438-
-
12439-
message: "#^Property Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\HostText\\:\\:\\$prefix has no type specified\\.$#"
12440-
count: 1
12441-
path: src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php
12442-
12443-
-
12444-
message: "#^Property Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\HostText\\:\\:\\$siteAccessesConfiguration is never read, only written\\.$#"
12445-
count: 1
12446-
path: src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php
12447-
12448-
-
12449-
message: "#^Property Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\HostText\\:\\:\\$siteAccessesConfiguration type has no value type specified in iterable type array\\.$#"
12450-
count: 1
12451-
path: src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php
12452-
12453-
-
12454-
message: "#^Property Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\HostText\\:\\:\\$suffix has no type specified\\.$#"
12455-
count: 1
12456-
path: src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php
12457-
1245812428
-
1245912429
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\Map\\:\\:__construct\\(\\) has parameter \\$map with no value type specified in iterable type array\\.$#"
1246012430
count: 1
@@ -12620,31 +12590,6 @@ parameters:
1262012590
count: 1
1262112591
path: src/lib/MVC/Symfony/SiteAccess/Matcher/URIElement.php
1262212592

12623-
-
12624-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\URIText\\:\\:__construct\\(\\) has parameter \\$siteAccessesConfiguration with no value type specified in iterable type array\\.$#"
12625-
count: 1
12626-
path: src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php
12627-
12628-
-
12629-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\URIText\\:\\:analyseURI\\(\\) should return string but returns string\\|null\\.$#"
12630-
count: 1
12631-
path: src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php
12632-
12633-
-
12634-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\URIText\\:\\:setRequest\\(\\) has no return type specified\\.$#"
12635-
count: 1
12636-
path: src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php
12637-
12638-
-
12639-
message: "#^Property Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\URIText\\:\\:\\$siteAccessesConfiguration is never read, only written\\.$#"
12640-
count: 1
12641-
path: src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php
12642-
12643-
-
12644-
message: "#^Property Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\SiteAccess\\\\Matcher\\\\URIText\\:\\:\\$siteAccessesConfiguration type has no value type specified in iterable type array\\.$#"
12645-
count: 1
12646-
path: src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php
12647-
1264812593
-
1264912594
message: "#^Call to an undefined method object\\:\\:setRequest\\(\\)\\.$#"
1265012595
count: 1
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Core\MVC\Symfony\SiteAccess\Matcher;
10+
11+
use Ibexa\Core\MVC\Symfony\SiteAccess\VersatileMatcher;
12+
13+
/**
14+
* @internal
15+
*
16+
* @phpstan-type TSiteAccessConfigurationArray array{prefix?: string, suffix?: string}
17+
*/
18+
abstract class AffixBasedTextMatcher extends Regex implements VersatileMatcher
19+
{
20+
protected readonly string $prefix;
21+
22+
protected readonly string $suffix;
23+
24+
abstract protected function buildRegex(): string;
25+
26+
abstract protected function getMatchedItemNumber(): int;
27+
28+
/**
29+
* @phpstan-param TSiteAccessConfigurationArray $siteAccessesConfiguration
30+
*/
31+
public function __construct(private readonly array $siteAccessesConfiguration)
32+
{
33+
$this->prefix = $this->siteAccessesConfiguration['prefix'] ?? '';
34+
$this->suffix = $this->siteAccessesConfiguration['suffix'] ?? '';
35+
36+
parent::__construct($this->buildRegex(), $this->getMatchedItemNumber());
37+
}
38+
}

src/lib/MVC/Symfony/SiteAccess/Matcher/HostText.php

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,24 @@
1010
use Ibexa\Core\MVC\Symfony\Routing\SimplifiedRequest;
1111
use Ibexa\Core\MVC\Symfony\SiteAccess\VersatileMatcher;
1212

13-
class HostText extends Regex implements VersatileMatcher
13+
class HostText extends AffixBasedTextMatcher
1414
{
15-
private $prefix;
16-
17-
private $suffix;
18-
19-
/**
20-
* The property needed to allow correct deserialization with Symfony serializer.
21-
*
22-
* @var array
23-
*/
24-
private $siteAccessesConfiguration;
15+
protected function buildRegex(): string
16+
{
17+
return '^' . preg_quote($this->prefix, '@') . "(\w+)" . preg_quote($this->suffix, '@') . '$';
18+
}
2519

26-
/**
27-
* Constructor.
28-
*
29-
* @param array $siteAccessesConfiguration SiteAccesses configuration.
30-
*/
31-
public function __construct(array $siteAccessesConfiguration)
20+
protected function getMatchedItemNumber(): int
3221
{
33-
$this->prefix = isset($siteAccessesConfiguration['prefix']) ? $siteAccessesConfiguration['prefix'] : '';
34-
$this->suffix = isset($siteAccessesConfiguration['suffix']) ? $siteAccessesConfiguration['suffix'] : '';
35-
parent::__construct(
36-
'^' . preg_quote($this->prefix, '@') . "(\w+)" . preg_quote($this->suffix, '@') . '$',
37-
1
38-
);
39-
$this->siteAccessesConfiguration = $siteAccessesConfiguration;
22+
return 1;
4023
}
4124

42-
public function getName()
25+
public function getName(): string
4326
{
4427
return 'host:text';
4528
}
4629

47-
/**
48-
* Injects the request object to match against.
49-
*
50-
* @param \Ibexa\Core\MVC\Symfony\Routing\SimplifiedRequest $request
51-
*/
52-
public function setRequest(SimplifiedRequest $request)
30+
public function setRequest(SimplifiedRequest $request): void
5331
{
5432
if (!$this->element) {
5533
$this->setMatchElement($request->host);
@@ -58,14 +36,14 @@ public function setRequest(SimplifiedRequest $request)
5836
parent::setRequest($request);
5937
}
6038

61-
public function reverseMatch($siteAccessName)
39+
public function reverseMatch($siteAccessName): ?VersatileMatcher
6240
{
6341
$this->request->setHost($this->prefix . $siteAccessName . $this->suffix);
6442

6543
return $this;
6644
}
6745

68-
public function getRequest()
46+
public function getRequest(): SimplifiedRequest
6947
{
7048
return $this->request;
7149
}

src/lib/MVC/Symfony/SiteAccess/Matcher/URIText.php

Lines changed: 14 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,24 @@
1111
use Ibexa\Core\MVC\Symfony\SiteAccess\URILexer;
1212
use Ibexa\Core\MVC\Symfony\SiteAccess\VersatileMatcher;
1313

14-
class URIText extends Regex implements VersatileMatcher, URILexer
14+
class URIText extends AffixBasedTextMatcher implements URILexer
1515
{
16-
/** @var string */
17-
private $prefix;
18-
19-
/** @var string */
20-
private $suffix;
21-
22-
/**
23-
* The property needed to allow correct deserialization with Symfony serializer.
24-
*
25-
* @var array
26-
*/
27-
private $siteAccessesConfiguration;
28-
29-
/**
30-
* Constructor.
31-
*
32-
* @param array $siteAccessesConfiguration SiteAccesses configuration.
33-
*/
34-
public function __construct(array $siteAccessesConfiguration)
16+
protected function buildRegex(): string
3517
{
36-
$this->prefix = isset($siteAccessesConfiguration['prefix']) ? $siteAccessesConfiguration['prefix'] : '';
37-
$this->suffix = isset($siteAccessesConfiguration['suffix']) ? $siteAccessesConfiguration['suffix'] : '';
18+
return '^(/' . preg_quote($this->prefix, '@') . '(\w+)' . preg_quote($this->suffix, '@') . ')';
19+
}
3820

39-
parent::__construct(
40-
'^(/' . preg_quote($this->prefix, '@') . '(\w+)' . preg_quote($this->suffix, '@') . ')',
41-
2
42-
);
43-
$this->siteAccessesConfiguration = $siteAccessesConfiguration;
21+
protected function getMatchedItemNumber(): int
22+
{
23+
return 2;
4424
}
4525

46-
public function getName()
26+
public function getName(): string
4727
{
4828
return 'uri:text';
4929
}
5030

51-
/**
52-
* Injects the request object to match against.
53-
*
54-
* @param \Ibexa\Core\MVC\Symfony\Routing\SimplifiedRequest $request
55-
*/
56-
public function setRequest(SimplifiedRequest $request)
31+
public function setRequest(SimplifiedRequest $request): void
5732
{
5833
if (!$this->element) {
5934
$this->setMatchElement($request->pathinfo);
@@ -62,43 +37,29 @@ public function setRequest(SimplifiedRequest $request)
6237
parent::setRequest($request);
6338
}
6439

65-
/**
66-
* Analyses $uri and removes the siteaccess part, if needed.
67-
*
68-
* @param string $uri The original URI
69-
*
70-
* @return string The modified URI
71-
*/
72-
public function analyseURI($uri)
40+
public function analyseURI($uri): string
7341
{
7442
$uri = '/' . ltrim($uri, '/');
7543

76-
return preg_replace("@$this->regex@", '', $uri);
44+
return preg_replace("@$this->regex@", '', $uri) ?? $uri;
7745
}
7846

79-
/**
80-
* Analyses $linkUri when generating a link to a route, in order to have the siteaccess part back in the URI.
81-
*
82-
* @param string $linkUri
83-
*
84-
* @return string The modified link URI
85-
*/
86-
public function analyseLink($linkUri)
47+
public function analyseLink($linkUri): string
8748
{
8849
$linkUri = '/' . ltrim($linkUri, '/');
8950
$siteAccessUri = "/$this->prefix" . $this->match() . $this->suffix;
9051

9152
return $siteAccessUri . $linkUri;
9253
}
9354

94-
public function reverseMatch($siteAccessName)
55+
public function reverseMatch($siteAccessName): ?VersatileMatcher
9556
{
9657
$this->request->setPathinfo("/{$this->prefix}{$siteAccessName}{$this->suffix}{$this->request->pathinfo}");
9758

9859
return $this;
9960
}
10061

101-
public function getRequest()
62+
public function getRequest(): SimplifiedRequest
10263
{
10364
return $this->request;
10465
}

0 commit comments

Comments
 (0)