Skip to content

Commit 6039bf0

Browse files
committed
maintenance: phpmd/phpcs
1 parent 3aaad53 commit 6039bf0

17 files changed

Lines changed: 145 additions & 84 deletions

phpcs.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?xml version="1.0"?>
22
<ruleset name="PHP.Gt Standard" namespace="Gt\CS\Standard">
3-
<description>Created from PHP.GT/Styleguide</description>
3+
<description>Created from PHP.Gt/Styleguide</description>
44
<arg name="extensions" value="php" />
55

66
<rule ref="Generic.Classes.DuplicateClassName" />
77
<rule ref="Generic.Classes.OpeningBraceSameLine" />
88
<rule ref="Generic.CodeAnalysis.EmptyPHPStatement" />
9-
<rule ref="Generic.CodeAnalysis.EmptyStatement" />
9+
<rule ref="Generic.CodeAnalysis.EmptyStatement">
10+
<exclude name="Generic.CodeAnalysis.EmptyStatement.DetectedCatch" />
11+
</rule>
1012
<rule ref="Generic.CodeAnalysis.ForLoopShouldBeWhileLoop" />
1113
<rule ref="Generic.CodeAnalysis.ForLoopWithTestFunctionCall" />
1214
<rule ref="Generic.CodeAnalysis.JumbledIncrementer" />
@@ -18,6 +20,13 @@
1820
<rule ref="Generic.Files.EndFileNewline" />
1921
<rule ref="Generic.Files.InlineHTML" />
2022
<rule ref="Generic.Files.LineEndings" />
23+
<rule ref="Generic.Files.LineLength">
24+
<properties>
25+
<property name="lineLimit" value="120" />
26+
<property name="absoluteLineLimit" value="0" />
27+
<property name="ignoreComments" value="true" />
28+
</properties>
29+
</rule>
2130
<rule ref="Generic.Files.OneClassPerFile" />
2231
<rule ref="Generic.Files.OneInterfacePerFile" />
2332
<rule ref="Generic.Files.OneObjectStructurePerFile" />

phpmd.xml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
77
<description>Custom ruleset</description>
88

9+
<rule ref="rulesets/codesize.xml">
10+
<exclude name="TooManyPublicMethods"/>
11+
<exclude name="CyclomaticComplexity"/>
12+
<exclude name="NPathComplexity"/>
13+
</rule>
14+
915
<rule ref="rulesets/cleancode.xml">
16+
<exclude name="StaticAccess" />
1017
<exclude name="ElseExpression" />
1118
<exclude name="IfStatementAssignment" />
1219
</rule>
@@ -16,22 +23,30 @@
1623
</rule>
1724

1825
<rule ref="rulesets/design.xml">
26+
<exclude name="CouplingBetweenObjects" />
1927
<exclude name="NumberOfChildren" />
28+
<exclude name="EmptyCatchBlock" />
2029
</rule>
2130

2231
<rule ref="rulesets/naming.xml">
2332
<exclude name="ShortVariable"/>
24-
<exclude name="LongVariable"/>
2533
<exclude name="LongClassName"/>
2634
</rule>
2735

2836
<rule ref="rulesets/unusedcode.xml">
2937
<exclude name="UnusedFormalParameter" />
3038
</rule>
3139

40+
<rule ref="rulesets/codesize.xml/TooManyPublicMethods">
41+
<priority>1</priority>
42+
<properties>
43+
<property name="maxmethods" value="20" />
44+
</properties>
45+
</rule>
46+
3247
<rule ref="rulesets/naming.xml/ShortVariable">
3348
<properties>
34-
<property name="exceptions" value="ch,mh,sh,id,tr,td,i,fh" />
49+
<property name="exceptions" value="ch,mh,sh,id,tr,td,i" />
3550
</properties>
3651
</rule>
3752
</ruleset>

src/Application.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
* The heavy lifting of converting Request to Response is performed in the
3434
* Dispatcher's generateResponse() method.
3535
*
36-
* @SuppressWarnings("PHPMD.CouplingBetweenObjects")
36+
* @SuppressWarnings("PHPMD.CouplingBetweenObjects")
37+
* @SuppressWarnings("PHPMD.ExcessiveClassComplexity")
3738
*/
3839
class Application {
3940
private Redirect $redirect;
@@ -48,7 +49,7 @@ class Application {
4849
private Config $config;
4950
private DispatcherFactory $dispatcherFactory;
5051
private Dispatcher $dispatcher;
51-
private static bool $loggerStreamsConfigured = false;
52+
private static bool $loggerConfigured = false;
5253
private bool $finished = false;
5354

5455
/**
@@ -259,6 +260,7 @@ private function protectGlobals():void {
259260
);
260261
}
261262

263+
/** @SuppressWarnings("PHPMD.Superglobals") */
262264
public function restoreGlobals(): void {
263265
foreach ($this->globals as $key => $value) {
264266
$GLOBALS[$key] = $value;
@@ -285,7 +287,7 @@ private function loadConfig():Config {
285287
}
286288

287289
private function configureLoggerStreams():void {
288-
if(self::$loggerStreamsConfigured) {
290+
if(self::$loggerConfigured) {
289291
return;
290292
}
291293

@@ -318,7 +320,7 @@ private function configureLoggerStreams():void {
318320
$stderrMinLevel,
319321
LogLevel::EMERGENCY,
320322
);
321-
self::$loggerStreamsConfigured = true;
323+
self::$loggerConfigured = true;
322324
}
323325

324326
private function handleShutdown():void {
@@ -378,7 +380,7 @@ private function logError(Throwable $throwable):void {
378380
return;
379381
}
380382

381-
if(self::$loggerStreamsConfigured) {
383+
if(self::$loggerConfigured) {
382384
Log::error((string)$throwable);
383385
return;
384386
}
@@ -409,7 +411,10 @@ private function getStderrMinimumLogLevel():string {
409411
return LogLevel::ERROR;
410412
}
411413

412-
/** @return array<string, mixed> */
414+
/**
415+
* @return array<string, mixed>
416+
* @SuppressWarnings("PHPMD.Superglobals")
417+
*/
413418
private function getLogContext():array {
414419
$uri = $this->request->getUri();
415420
$uriPath = $uri->getPath();

src/Debug/Timer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ public function logDelta():void {
6262
return;
6363
}
6464

65-
$this->deltaLogCallback->call($this, "Timer ended with $message delta time: $delta seconds. https://www.php.gt/webengine/slow-delta");
65+
$this->deltaLogCallback->call(
66+
$this,
67+
"Timer ended with $message delta time: $delta seconds. "
68+
. "https://www.php.gt/webengine/slow-delta"
69+
);
6670
}
6771

6872
}

src/Dispatch/Dispatcher.php

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
use GT\WebEngine\View\ViewStreamer;
4646
use Throwable;
4747

48+
/**
49+
* @SuppressWarnings("PHPMD.TooManyFields")
50+
* @SuppressWarnings("PHPMD.ExcessiveClassComplexity")
51+
*/
4852
class Dispatcher {
4953
private Config $config;
5054
private Request $request;
@@ -70,12 +74,15 @@ class Dispatcher {
7074
private ViewStreamer $viewStreamer;
7175

7276
private HeaderManager $headerManager;
73-
private Closure $viewModelInitCallback;
77+
private Closure $viewInitCb;
7478
private bool $redirectPrepared = false;
7579

7680
/**
7781
* @param array<string, array<string, string|array<string, string>>> $globals
82+
* @SuppressWarnings("PHPMD.ExcessiveMethodLength")
83+
* @SuppressWarnings("PHPMD.ExcessiveParameterList")
7884
*/
85+
// phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
7986
public function __construct(
8087
Config $config,
8188
Request $request,
@@ -190,7 +197,7 @@ public function __construct(
190197
$this->config->getString("view.partial_directory"),
191198
);
192199
$this->viewModelProcessor = $viewModelInit->getViewModelProcessor();
193-
$this->viewModelInitCallback = $this->viewModel instanceof HTMLDocument
200+
$this->viewInitCb = $this->viewModel instanceof HTMLDocument
194201
? function()use($viewModelInit):void {
195202
$documentBinder = $this->serviceContainer->get(Binder::class);
196203
assert($documentBinder instanceof DocumentBinder);
@@ -215,6 +222,7 @@ public function __construct(
215222
$this->viewStreamer = $viewStreamer ?? new ViewStreamer();
216223
$this->headerManager = $headerManager ?? new HeaderManager();
217224
}
225+
// phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
218226

219227
public function generateResponse():Response {
220228
if($this->redirectPrepared) {
@@ -251,7 +259,7 @@ public function generateErrorResponse(Throwable $throwable):Response {
251259
throw new ErrorPageNotFoundException(code: $errorStatusCode);
252260
}
253261

254-
$this->processResponse(true, $throwable);
262+
$this->processResponse($throwable);
255263
$this->response = $this->response->withStatus($errorStatusCode);
256264
return $this->response;
257265
}
@@ -273,7 +281,8 @@ public function generateBasicErrorResponse(
273281
$errorMessage = "The server could not find the requested resource.";
274282

275283
if(!$this->config->getBool("app.production")) {
276-
$detail .= " Additionally, there was no error page found in your application at <strong>$errorPageDir/$errorStatusCode.html</strong>";
284+
$detail .= " Additionally, there was no error page found in your "
285+
. "application at <strong>$errorPageDir/$errorStatusCode.html</strong>";
277286
}
278287
}
279288
}
@@ -306,11 +315,13 @@ public function generateBasicErrorResponse(
306315

307316
$body = new Stream();
308317
$body->write($html);
309-
return new Response(request: $this->request)->withBody($body)->withStatus($errorStatusCode);
318+
$response = new Response(null, null, $this->request);
319+
$response = $response->withBody($body);
320+
return $response->withStatus($errorStatusCode);
310321
}
311322

312323
private function setupResponse():Response {
313-
$response = new Response(request: $this->request);
324+
$response = new Response(null, null, $this->request);
314325
$response->setExitCallback(function() {
315326
($this->finishCallback)($this->response);
316327
});
@@ -343,7 +354,8 @@ private function handleLogicExecution(
343354
}
344355

345356
foreach($this->logicExecutor->invoke($logicAssembly, "go_before", $extraArgs) as $file) {
346-
// TODO: Hook up to debug output
357+
// Force generator execution even when debug output is disabled.
358+
continue;
347359
}
348360

349361
// TODO: No need to have the whole Input class. Just pass a nullable string in called $doMethod, from $input->getString("do")
@@ -356,25 +368,28 @@ function(InputData $data)use($logicAssembly, $extraArgs) {
356368
);
357369

358370
foreach($this->logicExecutor->invoke($logicAssembly, $doName, $extraArgs) as $file) {
359-
// TODO: Hook up to debug output
371+
// Force generator execution even when debug output is disabled.
372+
continue;
360373
}
361374
}
362375
);
363376

364377
foreach($this->logicExecutor->invoke($logicAssembly, "go", $extraArgs) as $file) {
365-
// TODO: Hook up to debug output
378+
// Force generator execution even when debug output is disabled.
379+
continue;
366380
}
367381

368382
foreach($this->logicExecutor->invoke($logicAssembly, "go_after", $extraArgs) as $file) {
369-
// TODO: Hook up to debug output
383+
// Force generator execution even when debug output is disabled.
384+
continue;
370385
}
371386
}
372387

373388
/**
374389
* @return void
375390
*/
391+
// phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
376392
public function processResponse(
377-
bool $processingError = false,
378393
?Throwable $errorThrowable = null,
379394
):void {
380395
$dynamicPath = $this->serviceContainer->get(DynamicPath::class);
@@ -384,19 +399,19 @@ public function processResponse(
384399
$dynamicPath,
385400
);
386401

387-
$logicAssemblyComponentList = $this->viewModelProcessor?->processPartialContent(
402+
$componentList = $this->viewModelProcessor?->processPartialContent(
388403
$this->viewModel,
389404
);
390405

391406
// TODO: CSRF handling - needs to be done on any POST request.
392-
($this->viewModelInitCallback)();
393-
if($processingError && $errorThrowable) {
407+
($this->viewInitCb)();
408+
if($errorThrowable) {
394409
$this->bindErrorDetails($errorThrowable);
395410
}
396411

397-
foreach($logicAssemblyComponentList ?? [] as $logicAssemblyComponent) {
398-
$assembly = $logicAssemblyComponent->assembly;
399-
$componentElement = $logicAssemblyComponent->component;
412+
foreach($componentList ?? [] as $componentLogic) {
413+
$assembly = $componentLogic->assembly;
414+
$componentElement = $componentLogic->component;
400415
$this->serviceContainer->set($componentElement);
401416

402417
try {
@@ -407,7 +422,7 @@ public function processResponse(
407422
);
408423
}
409424
catch(Throwable $throwable) {
410-
if(!$processingError) {
425+
if(!$errorThrowable) {
411426
throw $throwable;
412427
}
413428
}
@@ -420,7 +435,7 @@ public function processResponse(
420435
);
421436
}
422437
catch(Throwable $throwable) {
423-
if(!$processingError) {
438+
if(!$errorThrowable) {
424439
throw $throwable;
425440
}
426441
}
@@ -438,6 +453,7 @@ public function processResponse(
438453

439454
$this->viewStreamer->stream($this->view, $this->viewModel);
440455
}
456+
// phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
441457

442458
public function getSessionInit():?SessionInit {
443459
return $this->sessionInit;
@@ -452,6 +468,7 @@ private function isRedirectPrepared():bool {
452468
return $this->response->hasHeader("Location");
453469
}
454470

471+
// phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
455472
private function bindErrorDetails(Throwable $throwable):void {
456473
$trace = $throwable->getTrace();
457474
array_unshift($trace, [
@@ -498,4 +515,5 @@ private function bindErrorDetails(Throwable $throwable):void {
498515
$binder->bindKeyValue("trace", $traceString);
499516
}
500517
}
518+
// phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
501519
}

src/Dispatch/RouterFactory.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,30 @@
66
use Gt\ServiceContainer\Container;
77

88
class RouterFactory {
9+
/** @SuppressWarnings("PHPMD.LongVariable") */
910
public function create(
1011
Container $container,
11-
string $configAppNamespace,
12-
string $configAppRouterFile,
13-
string $configAppRouterClass,
14-
string $configDefaultRouterFile,
15-
string $configDefaultRouterClass,
16-
int $configRedirectResponseCode,
17-
string $configDefaultContentType,
12+
string $appNamespace,
13+
string $appRouterFile,
14+
string $appRouterClass,
15+
string $defaultRouterFile,
16+
string $defaultRouterClass,
17+
int $redirectResponseCode,
18+
string $defaultContentType,
1819
?int $errorStatus = null,
1920
):BaseRouter {
20-
if(file_exists($configAppRouterFile)) {
21-
require_once($configAppRouterFile);
22-
$class = "\\$configAppNamespace\\$configAppRouterClass";
21+
if(file_exists($appRouterFile)) {
22+
require_once($appRouterFile);
23+
$class = "\\$appNamespace\\$appRouterClass";
2324
}
2425
else {
25-
require_once($configDefaultRouterFile);
26-
$class = $configDefaultRouterClass;
26+
require_once($defaultRouterFile);
27+
$class = $defaultRouterClass;
2728
}
2829

2930
$routerConfig = new RouterConfig(
30-
$configRedirectResponseCode,
31-
$configDefaultContentType,
31+
$redirectResponseCode,
32+
$defaultContentType,
3233
);
3334

3435
/** @var BaseRouter $router */

src/Init/RouterInit.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class RouterInit {
2323
private Assembly $viewAssembly;
2424
private Assembly $logicAssembly;
2525

26+
/** @SuppressWarnings("PHPMD.ExcessiveParameterList") */
2627
public function __construct(
2728
Request $request,
2829
Response $response,

0 commit comments

Comments
 (0)