Skip to content

Commit cd357e9

Browse files
committed
tidy: phpcs tweaks
1 parent ec7dfe4 commit cd357e9

File tree

5 files changed

+60
-9
lines changed

5 files changed

+60
-9
lines changed

phpcs.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
<rule ref="Generic.Files.EndFileNewline" />
2020
<rule ref="Generic.Files.InlineHTML" />
2121
<rule ref="Generic.Files.LineEndings" />
22-
<rule ref="Generic.Files.LineLength" />
22+
<rule ref="Generic.Files.LineLength">
23+
<properties>
24+
<property name="lineLimit" value="100"/>
25+
</properties>
26+
</rule>
2327
<rule ref="Generic.Files.OneClassPerFile" />
2428
<rule ref="Generic.Files.OneInterfacePerFile" />
2529
<rule ref="Generic.Files.OneObjectStructurePerFile" />

src/NotYetRoutedException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
use Throwable;
55

66
class NotYetRoutedException extends RoutingException {
7-
const DEFAULT_MESSAGE = "A Router method is being called before route(). For help, please visit: https://www.php.gt/routing/not-yet-routed-exception";
7+
const DEFAULT_MESSAGE = "A Router method is being called before route(). "
8+
. "For help, please visit: https://www.php.gt/routing/not-yet-routed-exception";
89
}

src/Path/DynamicPath.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ public function get(?string $key = null, ?bool $extra = false):?string {
3939
* @param bool|null $extra Whether to include extra path parts
4040
* @return string|null The matched parameter or null if no match
4141
*/
42-
private function processFilePathParts(array $filePathParts, array $requestPathParts, ?string $key, ?bool $extra): ?string {
42+
private function processFilePathParts(
43+
array $filePathParts,
44+
array $requestPathParts,
45+
?string $key,
46+
?bool $extra,
47+
): ?string {
4348
foreach($filePathParts as $i => $filePart) {
4449
$filePart = strtok($filePart, ".");
4550
if($filePart[0] !== "@") {
@@ -67,7 +72,11 @@ private function processFilePathParts(array $filePathParts, array $requestPathPa
6772
* @param bool|null $extra Whether to include extra path parts
6873
* @return string|null The matched parameter
6974
*/
70-
private function handleNullKey(array $filePathParts, array $requestPathParts, ?bool $extra): ?string {
75+
private function handleNullKey(
76+
array $filePathParts,
77+
array $requestPathParts,
78+
?bool $extra,
79+
): ?string {
7180
if($extra) {
7281
return $this->getExtraPathParts($filePathParts, $requestPathParts);
7382
}
@@ -101,7 +110,12 @@ private function getExtraPathParts(array $filePathParts, array $requestPathParts
101110
* @param string $key The key to look for
102111
* @return string|null The matched parameter or null if no match
103112
*/
104-
private function handleNamedKey(string $filePart, array $requestPathParts, int $i, string $key): ?string {
113+
private function handleNamedKey(
114+
string $filePart,
115+
array $requestPathParts,
116+
int $i,
117+
string $key,
118+
): ?string {
105119
if(ltrim($filePart, "@") !== $key) {
106120
return null;
107121
}

src/RouterCallback.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
<?php
22
namespace Gt\Routing;
33

4+
use Gt\Routing\Method\Any;
5+
use Gt\Routing\Method\Connect;
6+
use Gt\Routing\Method\Delete;
7+
use Gt\Routing\Method\Get;
8+
use Gt\Routing\Method\Head;
49
use Gt\Routing\Method\HttpMethodHandler;
10+
use Gt\Routing\Method\Options;
11+
use Gt\Routing\Method\Patch;
12+
use Gt\Routing\Method\Post;
13+
use Gt\Routing\Method\Put;
14+
use Gt\Routing\Method\Trace;
515
use Gt\ServiceContainer\Container;
616
use Gt\ServiceContainer\Injector;
717
use Negotiation\Accept;
@@ -10,10 +20,16 @@
1020
use ReflectionMethod;
1121
use Gt\Routing\Method\HttpRouteMethod;
1222

23+
24+
/**
25+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
26+
* @phpcs:disable Generic.Metrics.CyclomaticComplexity
27+
*/
1328
class RouterCallback {
1429
private Container $container;
1530
private Injector $injector;
1631
private ContentNegotiator $contentNegotiator;
32+
/** @phpstan-ignore-next-line */
1733
private HttpMethodHandler $httpMethodHandler;
1834

1935
/** @param ReflectionAttribute<HttpRouteMethod> $attribute */
@@ -38,11 +54,26 @@ public function call(BaseRouter $router):void {
3854

3955
public function isAllowedMethod(string $requestMethod):bool {
4056
$methodsArgument = $this->attribute->getArguments()["methods"] ?? [];
41-
return $this->httpMethodHandler->isAllowedMethod(
42-
$requestMethod,
43-
$this->attribute->getName(),
44-
$methodsArgument
57+
58+
$allowedMethods = match($this->attribute->getName()) {
59+
Any::class => HttpRoute::METHODS_ALL,
60+
Connect::class => [HttpRoute::METHOD_CONNECT],
61+
Delete::class => [HttpRoute::METHOD_DELETE],
62+
Get::class => [HttpRoute::METHOD_GET],
63+
Head::class => [HttpRoute::METHOD_HEAD],
64+
Options::class => [HttpRoute::METHOD_OPTIONS],
65+
Patch::class => [HttpRoute::METHOD_PATCH],
66+
Post::class => [HttpRoute::METHOD_POST],
67+
Put::class => [HttpRoute::METHOD_PUT],
68+
Trace::class => [HttpRoute::METHOD_TRACE],
69+
default => $methodsArgument,
70+
};
71+
$allowedMethods = array_map(
72+
"strtoupper",
73+
$allowedMethods
4574
);
75+
76+
return in_array($requestMethod, $allowedMethods);
4677
}
4778

4879
public function matchesPath(string $requestPath):bool {

src/RoutingException.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class RoutingException extends RuntimeException {
88
const DEFAULT_MESSAGE = "";
99

10+
/** @phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod */
1011
public function __construct(
1112
string $message = self::DEFAULT_MESSAGE,
1213
int $code = 0,

0 commit comments

Comments
 (0)