Skip to content

Commit 8f7c48c

Browse files
authored
Merge pull request #529 from flightphp/coding-style
added PSR12 coding style to files.
2 parents 0d7b79b + cc4338a commit 8f7c48c

34 files changed

+2071
-1819
lines changed

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"phpunit/phpunit": "^9.5",
3838
"phpstan/phpstan": "^1.10",
3939
"phpstan/extension-installer": "^1.3",
40-
"rregeer/phpunit-coverage-check": "^0.3.1"
40+
"rregeer/phpunit-coverage-check": "^0.3.1",
41+
"squizlabs/php_codesniffer": "^3.8"
4142
},
4243
"config": {
4344
"allow-plugins": {
@@ -47,7 +48,9 @@
4748
"scripts": {
4849
"test": "phpunit",
4950
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage --coverage-clover=clover.xml && vendor/bin/coverage-check clover.xml 100",
50-
"lint": "phpstan --no-progress -cphpstan.neon"
51+
"lint": "phpstan --no-progress -cphpstan.neon",
52+
"beautify": "phpcbf --standard=phpcs.xml",
53+
"phpcs": "phpcs --standard=phpcs.xml"
5154
},
5255
"suggest": {
5356
"latte/latte": "Latte template engine",

flight/Engine.php

Lines changed: 68 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
/**
56
* Flight: An extensible micro-framework.
67
*
@@ -32,7 +33,7 @@
3233
* @method void start() Starts engine
3334
* @method void stop() Stops framework and outputs current response
3435
* @method void halt(int $code = 200, string $message = '') Stops processing and returns a given response.
35-
*
36+
*
3637
* Routing
3738
* @method Route route(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a URL to a callback function with all applicable methods
3839
* @method void group(string $pattern, callable $callback, array $group_middlewares = []) Groups a set of routes together under a common prefix.
@@ -78,12 +79,12 @@ class Engine
7879
*/
7980
protected Dispatcher $dispatcher;
8081

81-
/**
82-
* If the framework has been initialized or not
83-
*
84-
* @var boolean
85-
*/
86-
protected bool $initialized = false;
82+
/**
83+
* If the framework has been initialized or not
84+
*
85+
* @var boolean
86+
*/
87+
protected bool $initialized = false;
8788

8889
/**
8990
* Constructor.
@@ -377,7 +378,7 @@ public function _start(): void
377378
ob_start();
378379

379380
// Route the request
380-
$failed_middleware_check = false;
381+
$failed_middleware_check = false;
381382
while ($route = $router->route($request)) {
382383
$params = array_values($route->params);
383384

@@ -386,55 +387,52 @@ public function _start(): void
386387
$params[] = $route;
387388
}
388389

389-
// Run any before middlewares
390-
if(count($route->middleware) > 0) {
391-
foreach($route->middleware as $middleware) {
392-
393-
$middleware_object = (is_callable($middleware) === true ? $middleware : (method_exists($middleware, 'before') === true ? [ $middleware, 'before' ]: false));
390+
// Run any before middlewares
391+
if (count($route->middleware) > 0) {
392+
foreach ($route->middleware as $middleware) {
393+
$middleware_object = (is_callable($middleware) === true ? $middleware : (method_exists($middleware, 'before') === true ? [ $middleware, 'before' ] : false));
394394

395-
if($middleware_object === false) {
396-
continue;
397-
}
395+
if ($middleware_object === false) {
396+
continue;
397+
}
398398

399-
// It's assumed if you don't declare before, that it will be assumed as the before method
400-
$middleware_result = $middleware_object($route->params);
399+
// It's assumed if you don't declare before, that it will be assumed as the before method
400+
$middleware_result = $middleware_object($route->params);
401401

402-
if ($middleware_result === false) {
403-
$failed_middleware_check = true;
404-
break 2;
405-
}
406-
}
407-
}
402+
if ($middleware_result === false) {
403+
$failed_middleware_check = true;
404+
break 2;
405+
}
406+
}
407+
}
408408

409409
// Call route handler
410410
$continue = $this->dispatcher->execute(
411411
$route->callback,
412412
$params
413413
);
414-
415414

416-
// Run any before middlewares
417-
if(count($route->middleware) > 0) {
418-
419-
// process the middleware in reverse order now
420-
foreach(array_reverse($route->middleware) as $middleware) {
421415

422-
// must be an object. No functions allowed here
423-
$middleware_object = is_object($middleware) === true && !($middleware instanceof Closure) && method_exists($middleware, 'after') === true ? [ $middleware, 'after' ] : false;
416+
// Run any before middlewares
417+
if (count($route->middleware) > 0) {
418+
// process the middleware in reverse order now
419+
foreach (array_reverse($route->middleware) as $middleware) {
420+
// must be an object. No functions allowed here
421+
$middleware_object = is_object($middleware) === true && !($middleware instanceof Closure) && method_exists($middleware, 'after') === true ? [ $middleware, 'after' ] : false;
424422

425-
// has to have the after method, otherwise just skip it
426-
if($middleware_object === false) {
427-
continue;
428-
}
423+
// has to have the after method, otherwise just skip it
424+
if ($middleware_object === false) {
425+
continue;
426+
}
429427

430-
$middleware_result = $middleware_object($route->params);
428+
$middleware_result = $middleware_object($route->params);
431429

432-
if ($middleware_result === false) {
433-
$failed_middleware_check = true;
434-
break 2;
435-
}
436-
}
437-
}
430+
if ($middleware_result === false) {
431+
$failed_middleware_check = true;
432+
break 2;
433+
}
434+
}
435+
}
438436

439437
$dispatched = true;
440438

@@ -447,9 +445,9 @@ public function _start(): void
447445
$dispatched = false;
448446
}
449447

450-
if($failed_middleware_check === true) {
451-
$this->halt(403, 'Forbidden');
452-
} else if($dispatched === false) {
448+
if ($failed_middleware_check === true) {
449+
$this->halt(403, 'Forbidden');
450+
} elseif ($dispatched === false) {
453451
$this->notFound();
454452
}
455453
}
@@ -476,11 +474,11 @@ public function _error($e): void
476474
->status(500)
477475
->write($msg)
478476
->send();
479-
// @codeCoverageIgnoreStart
477+
// @codeCoverageIgnoreStart
480478
} catch (Throwable $t) {
481479
exit($msg);
482480
}
483-
// @codeCoverageIgnoreEnd
481+
// @codeCoverageIgnoreEnd
484482
}
485483

486484
/**
@@ -512,20 +510,20 @@ public function _stop(?int $code = null): void
512510
* @param string $pattern URL pattern to match
513511
* @param callable $callback Callback function
514512
* @param bool $pass_route Pass the matching route object to the callback
515-
* @param string $alias the alias for the route
516-
* @return Route
513+
* @param string $alias the alias for the route
514+
* @return Route
517515
*/
518516
public function _route(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): Route
519517
{
520518
return $this->router()->map($pattern, $callback, $pass_route, $alias);
521519
}
522520

523-
/**
521+
/**
524522
* Routes a URL to a callback function.
525523
*
526-
* @param string $pattern URL pattern to match
527-
* @param callable $callback Callback function that includes the Router class as first parameter
528-
* @param array<callable> $group_middlewares The middleware to be applied to the route
524+
* @param string $pattern URL pattern to match
525+
* @param callable $callback Callback function that includes the Router class as first parameter
526+
* @param array<callable> $group_middlewares The middleware to be applied to the route
529527
*/
530528
public function _group(string $pattern, callable $callback, array $group_middlewares = []): void
531529
{
@@ -585,7 +583,7 @@ public function _delete(string $pattern, callable $callback, bool $pass_route =
585583
*
586584
* @param int $code HTTP status code
587585
* @param string $message Response message
588-
*
586+
*
589587
*/
590588
public function _halt(int $code = 200, string $message = ''): void
591589
{
@@ -594,10 +592,10 @@ public function _halt(int $code = 200, string $message = ''): void
594592
->status($code)
595593
->write($message)
596594
->send();
597-
// apologies for the crappy hack here...
598-
if($message !== 'skip---exit') {
599-
exit(); // @codeCoverageIgnore
600-
}
595+
// apologies for the crappy hack here...
596+
if ($message !== 'skip---exit') {
597+
exit(); // @codeCoverageIgnore
598+
}
601599
}
602600

603601
/**
@@ -728,7 +726,7 @@ public function _etag(string $id, string $type = 'strong'): void
728726
{
729727
$id = (('weak' === $type) ? 'W/' : '') . $id;
730728

731-
$this->response()->header('ETag', '"'.str_replace('"', '\"', $id).'"');
729+
$this->response()->header('ETag', '"' . str_replace('"', '\"', $id) . '"');
732730

733731
if (
734732
isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
@@ -755,14 +753,14 @@ public function _lastModified(int $time): void
755753
}
756754
}
757755

758-
/**
759-
* Gets a url from an alias that's supplied.
760-
*
761-
* @param string $alias the route alias.
762-
* @param array<string, mixed> $params The params for the route if applicable.
763-
*/
764-
public function _getUrl(string $alias, array $params = []): string
765-
{
766-
return $this->router()->getUrlByAlias($alias, $params);
767-
}
756+
/**
757+
* Gets a url from an alias that's supplied.
758+
*
759+
* @param string $alias the route alias.
760+
* @param array<string, mixed> $params The params for the route if applicable.
761+
*/
762+
public function _getUrl(string $alias, array $params = []): string
763+
{
764+
return $this->router()->getUrlByAlias($alias, $params);
765+
}
768766
}

flight/Flight.php

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
/**
56
* Flight: An extensible micro-framework.
67
*
@@ -61,34 +62,34 @@ class Flight
6162
{
6263
/**
6364
* Framework engine.
64-
*
65-
* @var Engine $engine
65+
*
66+
* @var Engine $engine
6667
*/
6768
private static Engine $engine;
6869

69-
/**
70-
* Whether or not the app has been initialized
71-
*
72-
* @var boolean
73-
*/
74-
private static bool $initialized = false;
70+
/**
71+
* Whether or not the app has been initialized
72+
*
73+
* @var boolean
74+
*/
75+
private static bool $initialized = false;
7576

76-
/**
77-
* Don't allow object instantiation
78-
*
79-
* @codeCoverageIgnore
80-
* @return void
81-
*/
77+
/**
78+
* Don't allow object instantiation
79+
*
80+
* @codeCoverageIgnore
81+
* @return void
82+
*/
8283
private function __construct()
8384
{
8485
}
8586

86-
/**
87-
* Forbid cloning the class
88-
*
89-
* @codeCoverageIgnore
90-
* @return void
91-
*/
87+
/**
88+
* Forbid cloning the class
89+
*
90+
* @codeCoverageIgnore
91+
* @return void
92+
*/
9293
private function __clone()
9394
{
9495
}
@@ -145,14 +146,14 @@ public static function app(): Engine
145146
return self::$engine;
146147
}
147148

148-
/**
149-
* Set the engine instance
150-
*
151-
* @param Engine $engine Vroom vroom!
152-
* @return void
153-
*/
154-
public static function setEngine(Engine $engine): void
155-
{
156-
self::$engine = $engine;
157-
}
149+
/**
150+
* Set the engine instance
151+
*
152+
* @param Engine $engine Vroom vroom!
153+
* @return void
154+
*/
155+
public static function setEngine(Engine $engine): void
156+
{
157+
self::$engine = $engine;
158+
}
158159
}

flight/autoload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
/**
56
* Flight: An extensible micro-framework.
67
*

0 commit comments

Comments
 (0)