From 537ea032d88527cff47f932914f7cd4905fbd654 Mon Sep 17 00:00:00 2001 From: n0nag0n Date: Sat, 27 Jan 2024 09:57:37 -0700 Subject: [PATCH 1/3] request header shortcut and lots of unit test fixes for phpcs --- flight/Engine.php | 1 + flight/Flight.php | 3 ++- flight/net/Request.php | 13 ++++++++++++ flight/net/Response.php | 18 ++++++++--------- flight/util/ReturnTypeWillChange.php | 3 ++- tests/AutoloadTest.php | 7 +++++-- tests/CollectionTest.php | 5 ++++- tests/DispatcherTest.php | 10 +++++++--- tests/EngineTest.php | 3 +++ tests/FilterTest.php | 4 +++- tests/FlightTest.php | 30 +++++++++++++++++----------- tests/LoaderTest.php | 23 ++++++++++++--------- tests/MapTest.php | 6 +++++- tests/PdoWrapperTest.php | 7 ++++--- tests/RedirectTest.php | 4 +++- tests/RegisterTest.php | 13 +++++++----- tests/RenderTest.php | 4 +++- tests/RequestTest.php | 18 ++++++++++++++++- tests/ResponseTest.php | 7 ++++--- tests/RouterTest.php | 4 +++- tests/VariableTest.php | 5 ++++- tests/ViewTest.php | 3 +++ tests/classes/Factory.php | 2 ++ tests/classes/Hello.php | 2 ++ tests/classes/TesterClass.php | 2 ++ tests/classes/User.php | 2 ++ 26 files changed, 143 insertions(+), 56 deletions(-) diff --git a/flight/Engine.php b/flight/Engine.php index ac71828..c60192c 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -61,6 +61,7 @@ use flight\net\Route; * @method void etag($id, string $type = 'strong') Handles ETag HTTP caching. * @method void lastModified(int $time) Handles last modified HTTP caching. */ +// phpcs:ignoreFile Generic.Files.LineLength.TooLong, PSR2.Methods.MethodDeclaration.Underscore class Engine { /** diff --git a/flight/Flight.php b/flight/Flight.php index b22cf0a..ec139d8 100644 --- a/flight/Flight.php +++ b/flight/Flight.php @@ -58,7 +58,8 @@ use flight\net\Route; * @method static void etag($id, $type = 'strong') Performs ETag HTTP caching. * @method static void lastModified($time) Performs last modified HTTP caching. */ -class Flight +// phpcs:ignoreFile Generic.Files.LineLength.TooLong, PSR1.Classes.ClassDeclaration.MissingNamespace +class Flight { /** * Framework engine. diff --git a/flight/net/Request.php b/flight/net/Request.php index 278af08..146c641 100644 --- a/flight/net/Request.php +++ b/flight/net/Request.php @@ -302,6 +302,19 @@ class Request return $_SERVER[$var] ?? $default; } + /** + * This will pull a header from the request. + * + * @param string $header Header name. Can be caps, lowercase, or mixed. + * @param string $default Default value if the header does not exist + * @return string + */ + public static function getHeader(string $header, $default = ''): string + { + $header = 'HTTP_' . strtoupper(str_replace('-', '_', $header)); + return self::getVar($header, $default); + } + /** * Parse query parameters from a URL. * diff --git a/flight/net/Response.php b/flight/net/Response.php index 247c6b5..0446c3d 100644 --- a/flight/net/Response.php +++ b/flight/net/Response.php @@ -312,15 +312,15 @@ class Response \strlen($this->body); } - /** - * Gets the response body - * - * @return string - */ - public function getBody(): string - { - return $this->body; - } + /** + * Gets the response body + * + * @return string + */ + public function getBody(): string + { + return $this->body; + } /** * Gets whether response body was sent. diff --git a/flight/util/ReturnTypeWillChange.php b/flight/util/ReturnTypeWillChange.php index 982800c..0a40639 100644 --- a/flight/util/ReturnTypeWillChange.php +++ b/flight/util/ReturnTypeWillChange.php @@ -1,5 +1,6 @@ app->register('user', 'User'); + $this->app->register('user', User::class); $loaders = spl_autoload_functions(); diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index 8be746b..5c8ae52 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -6,7 +6,10 @@ * @copyright Copyright (c) 2012, Mike Cao * @license MIT, http://flightphp.com/license */ -class CollectionTest extends PHPUnit\Framework\TestCase + +namespace tests; + +class CollectionTest extends \PHPUnit\Framework\TestCase { /** * @var \flight\util\Collection diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index cb8edf9..153890a 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -7,9 +7,13 @@ * @license MIT, http://flightphp.com/license */ +namespace tests; + +use Exception; use flight\core\Dispatcher; +use tests\classes\Hello; -class DispatcherTest extends PHPUnit\Framework\TestCase +class DispatcherTest extends \PHPUnit\Framework\TestCase { /** * @var Dispatcher|null @@ -95,7 +99,7 @@ class DispatcherTest extends PHPUnit\Framework\TestCase // Map a static function public function testStaticFunctionMapping() { - $this->dispatcher->set('map2', 'Hello::sayHi'); + $this->dispatcher->set('map2', 'tests\classes\Hello::sayHi'); $result = $this->dispatcher->run('map2'); @@ -117,7 +121,7 @@ class DispatcherTest extends PHPUnit\Framework\TestCase // Map a static class method public function testStaticClassMethodMapping() { - $this->dispatcher->set('map4', ['Hello', 'sayBye']); + $this->dispatcher->set('map4', ['\tests\classes\Hello', 'sayBye']); $result = $this->dispatcher->run('map4'); diff --git a/tests/EngineTest.php b/tests/EngineTest.php index 833cbbe..b0b358b 100644 --- a/tests/EngineTest.php +++ b/tests/EngineTest.php @@ -6,11 +6,14 @@ * @copyright Copyright (c) 2012, Mike Cao * @license MIT, http://flightphp.com/license */ +namespace tests; +use Exception; use flight\Engine; use flight\net\Response; use PHPUnit\Framework\TestCase; +// phpcs:ignoreFile PSR2.Methods.MethodDeclaration.Underscore class EngineTest extends TestCase { public function setUp(): void diff --git a/tests/FilterTest.php b/tests/FilterTest.php index fbf27e0..a05fb13 100644 --- a/tests/FilterTest.php +++ b/tests/FilterTest.php @@ -7,9 +7,11 @@ * @license MIT, http://flightphp.com/license */ +namespace tests; + use flight\Engine; -class FilterTest extends PHPUnit\Framework\TestCase +class FilterTest extends \PHPUnit\Framework\TestCase { /** * @var Engine diff --git a/tests/FlightTest.php b/tests/FlightTest.php index 07a7346..939295c 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -1,18 +1,24 @@ * @license MIT, http://flightphp.com/license */ -class FlightTest extends PHPUnit\Framework\TestCase + +namespace tests; + +use Exception; +use Flight; +use flight\Engine; +use flight\net\Request; +use flight\net\Response; +use flight\net\Router; +use flight\template\View; +use tests\classes\User; + +class FlightTest extends \PHPUnit\Framework\TestCase { protected function setUp(): void { @@ -70,7 +76,7 @@ class FlightTest extends PHPUnit\Framework\TestCase { Flight::path(__DIR__ . '/classes'); - Flight::register('user', 'User'); + Flight::register('user', User::class); $user = Flight::user(); $loaders = spl_autoload_functions(); @@ -79,11 +85,11 @@ class FlightTest extends PHPUnit\Framework\TestCase self::assertIsObject($user); self::assertInstanceOf(User::class, $user); - Flight::unregister('user'); + Flight::unregister('user'); - self::expectException(Exception::class); - self::expectExceptionMessage('user must be a mapped method.'); - $user = Flight::user(); + self::expectException(Exception::class); + self::expectExceptionMessage('user must be a mapped method.'); + $user = Flight::user(); } // Map a function diff --git a/tests/LoaderTest.php b/tests/LoaderTest.php index 2ed2be9..4d1ab3f 100644 --- a/tests/LoaderTest.php +++ b/tests/LoaderTest.php @@ -7,8 +7,13 @@ * @license MIT, http://flightphp.com/license */ +namespace tests; + use flight\core\Loader; +use tests\classes\Factory; +use tests\classes\User; use PHPUnit\Framework\TestCase; +use tests\classes\TesterClass; class LoaderTest extends TestCase { @@ -23,7 +28,7 @@ class LoaderTest extends TestCase // Autoload a class public function testAutoload() { - $this->loader->register('tests', 'User'); + $this->loader->register('tests', User::class); $test = $this->loader->load('tests'); @@ -34,7 +39,7 @@ class LoaderTest extends TestCase // Register a class public function testRegister() { - $this->loader->register('a', 'User'); + $this->loader->register('a', User::class); $user = $this->loader->load('a'); @@ -46,7 +51,7 @@ class LoaderTest extends TestCase // Register a class with constructor parameters public function testRegisterWithConstructor() { - $this->loader->register('b', 'User', ['Bob']); + $this->loader->register('b', User::class, ['Bob']); $user = $this->loader->load('b'); @@ -58,7 +63,7 @@ class LoaderTest extends TestCase // Register a class with initialization public function testRegisterWithInitialization() { - $this->loader->register('c', 'User', ['Bob'], function ($user) { + $this->loader->register('c', User::class, ['Bob'], function ($user) { $user->name = 'Fred'; }); @@ -72,7 +77,7 @@ class LoaderTest extends TestCase // Get a non-shared instance of a class public function testSharedInstance() { - $this->loader->register('d', 'User'); + $this->loader->register('d', User::class); $user1 = $this->loader->load('d'); $user2 = $this->loader->load('d'); @@ -85,7 +90,7 @@ class LoaderTest extends TestCase // Gets an object from a factory method public function testRegisterUsingCallable() { - $this->loader->register('e', ['Factory', 'create']); + $this->loader->register('e', ['\tests\classes\Factory', 'create']); $obj = $this->loader->load('e'); @@ -119,9 +124,9 @@ class LoaderTest extends TestCase public function testUnregisterClass() { - $this->loader->register('g', 'User'); + $this->loader->register('g', User::class); $current_class = $this->loader->get('g'); - $this->assertEquals([ 'User', [], null ], $current_class); + $this->assertEquals([ User::class, [], null ], $current_class); $this->loader->unregister('g'); $unregistered_class_result = $this->loader->get('g'); $this->assertNull($unregistered_class_result); @@ -129,7 +134,7 @@ class LoaderTest extends TestCase public function testNewInstance6Params() { - $TesterClass = $this->loader->newInstance('TesterClass', ['Bob','Fred', 'Joe', 'Jane', 'Sally', 'Suzie']); + $TesterClass = $this->loader->newInstance(TesterClass::class, ['Bob','Fred', 'Joe', 'Jane', 'Sally', 'Suzie']); $this->assertEquals('Bob', $TesterClass->param1); $this->assertEquals('Fred', $TesterClass->param2); $this->assertEquals('Joe', $TesterClass->param3); diff --git a/tests/MapTest.php b/tests/MapTest.php index dc75b56..6c0a611 100644 --- a/tests/MapTest.php +++ b/tests/MapTest.php @@ -7,7 +7,11 @@ * @license MIT, http://flightphp.com/license */ +namespace tests; + +use Exception; use flight\Engine; +use tests\classes\Hello; use PHPUnit\Framework\TestCase; class MapTest extends TestCase @@ -58,7 +62,7 @@ class MapTest extends TestCase // Map a static class method public function testStaticClassMethodMapping() { - $this->app->map('map4', ['Hello', 'sayBye']); + $this->app->map('map4', [Hello::class, 'sayBye']); $result = $this->app->map4(); diff --git a/tests/PdoWrapperTest.php b/tests/PdoWrapperTest.php index 32c3c99..fdb30e4 100644 --- a/tests/PdoWrapperTest.php +++ b/tests/PdoWrapperTest.php @@ -1,7 +1,5 @@ app->register('reg1', 'User'); + $this->app->register('reg1', User::class); $user = $this->app->reg1(); @@ -34,7 +37,7 @@ class RegisterTest extends TestCase // Register a class with constructor parameters public function testRegisterWithConstructor() { - $this->app->register('reg2', 'User', ['Bob']); + $this->app->register('reg2', User::class, ['Bob']); $user = $this->app->reg2(); @@ -46,7 +49,7 @@ class RegisterTest extends TestCase // Register a class with initialization public function testRegisterWithInitialization() { - $this->app->register('reg3', 'User', ['Bob'], function ($user) { + $this->app->register('reg3', User::class, ['Bob'], function ($user) { $user->name = 'Fred'; }); @@ -60,7 +63,7 @@ class RegisterTest extends TestCase // Get a non-shared instance of a class public function testSharedInstance() { - $this->app->register('reg4', 'User'); + $this->app->register('reg4', User::class); $user1 = $this->app->reg4(); $user2 = $this->app->reg4(); @@ -73,7 +76,7 @@ class RegisterTest extends TestCase // Map method takes precedence over register public function testMapOverridesRegister() { - $this->app->register('reg5', 'User'); + $this->app->register('reg5', User::class); $user = $this->app->reg5(); diff --git a/tests/RenderTest.php b/tests/RenderTest.php index c49bf53..b489f42 100644 --- a/tests/RenderTest.php +++ b/tests/RenderTest.php @@ -7,9 +7,11 @@ * @license MIT, http://flightphp.com/license */ +namespace tests; + use flight\Engine; -class RenderTest extends PHPUnit\Framework\TestCase +class RenderTest extends \PHPUnit\Framework\TestCase { private Engine $app; diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 7d31cf2..469c581 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -7,10 +7,12 @@ * @license MIT, http://flightphp.com/license */ +namespace tests; + use flight\net\Request; use flight\util\Collection; -class RequestTest extends PHPUnit\Framework\TestCase +class RequestTest extends \PHPUnit\Framework\TestCase { private Request $request; @@ -198,4 +200,18 @@ class RequestTest extends PHPUnit\Framework\TestCase $this->assertEquals([ 'foo' => 'bar' ], $request->data->getData()); $this->assertEquals('{"foo":"bar"}', $request->getBody()); } + + public function testGetHeader() + { + $_SERVER['HTTP_X_CUSTOM_HEADER'] = 'custom header value'; + $request = new Request(); + $this->assertEquals('custom header value', $request->getHeader('X-Custom-Header')); + + // or the headers that are already in $_SERVER + $this->assertEquals('XMLHttpRequest', $request->getHeader('X-REqUesTed-WiTH')); + $this->assertEquals('32.32.32.32', $request->getHeader('X-Forwarded-For')); + + // default values + $this->assertEquals('default value', $request->getHeader('X-Non-Existent-Header', 'default value')); + } } diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index 6d6f129..68b1e99 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -7,11 +7,12 @@ * @license MIT, http://flightphp.com/license */ -use flight\net\Request; +namespace tests; + +use Exception; use flight\net\Response; -use flight\util\Collection; -class ResponseTest extends PHPUnit\Framework\TestCase +class ResponseTest extends \PHPUnit\Framework\TestCase { protected function setUp(): void { diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 12f1543..a9465ef 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -7,11 +7,13 @@ * @license MIT, http://flightphp.com/license */ +namespace tests; + use flight\core\Dispatcher; use flight\net\Request; use flight\net\Router; -class RouterTest extends PHPUnit\Framework\TestCase +class RouterTest extends \PHPUnit\Framework\TestCase { private Router $router; diff --git a/tests/VariableTest.php b/tests/VariableTest.php index 8bc7462..cf11ef1 100644 --- a/tests/VariableTest.php +++ b/tests/VariableTest.php @@ -6,7 +6,10 @@ * @copyright Copyright (c) 2012, Mike Cao * @license MIT, http://flightphp.com/license */ -class VariableTest extends PHPUnit\Framework\TestCase + +namespace tests; + +class VariableTest extends \PHPUnit\Framework\TestCase { /** * @var \flight\Engine diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 7bd1dd4..236f9f4 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -1,5 +1,8 @@ Date: Sat, 27 Jan 2024 10:27:43 -0700 Subject: [PATCH 2/3] added getHeaders() --- flight/net/Request.php | 18 ++++++++++++++++++ tests/RequestTest.php | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/flight/net/Request.php b/flight/net/Request.php index 146c641..7fd8592 100644 --- a/flight/net/Request.php +++ b/flight/net/Request.php @@ -315,6 +315,24 @@ class Request return self::getVar($header, $default); } + /** + * Gets all the request headers + * + * @return array + */ + public static function getHeaders(): array + { + $headers = []; + foreach ($_SERVER as $key => $value) { + if (0 === strpos($key, 'HTTP_')) { + // converts headers like HTTP_CUSTOM_HEADER to Custom-Header + $key = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5))))); + $headers[$key] = $value; + } + } + return $headers; + } + /** * Parse query parameters from a URL. * diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 469c581..8a9f9b5 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -214,4 +214,39 @@ class RequestTest extends \PHPUnit\Framework\TestCase // default values $this->assertEquals('default value', $request->getHeader('X-Non-Existent-Header', 'default value')); } + + public function testGetHeaders() + { + $_SERVER = []; + $_SERVER['HTTP_X_CUSTOM_HEADER'] = 'custom header value'; + $request = new Request(); + $this->assertEquals(['X-Custom-Header' => 'custom header value'], $request->getHeaders()); + } + + public function testGetHeadersWithEmptyServer() + { + $_SERVER = []; + $request = new Request(); + $this->assertEquals([], $request->getHeaders()); + } + + public function testGetHeadersWithEmptyHeader() + { + $_SERVER = []; + $_SERVER['HTTP_X_CUSTOM_HEADER'] = ''; + $request = new Request(); + $this->assertEquals(['X-Custom-Header' => ''], $request->getHeaders()); + } + + public function testGetHeadersWithMultipleHeaders() + { + $_SERVER = []; + $_SERVER['HTTP_X_CUSTOM_HEADER'] = 'custom header value'; + $_SERVER['HTTP_X_CUSTOM_HEADER2'] = 'custom header value 2'; + $request = new Request(); + $this->assertEquals([ + 'X-Custom-Header' => 'custom header value', + 'X-Custom-Header2' => 'custom header value 2' + ], $request->getHeaders()); + } } From df90d697878a38190a1163ccf07f82ec69d45287 Mon Sep 17 00:00:00 2001 From: n0nag0n Date: Sat, 27 Jan 2024 16:19:02 -0700 Subject: [PATCH 3/3] so many phpcs fixes. No more, no more! --- flight/Engine.php | 3 +- flight/Flight.php | 6 +-- flight/core/Dispatcher.php | 2 + flight/core/Loader.php | 13 +++++- flight/database/PdoWrapper.php | 75 ++++++++++++++-------------------- flight/net/Request.php | 12 ++++-- flight/net/Response.php | 13 ++++-- flight/net/Route.php | 27 ++++++++---- flight/net/Router.php | 5 ++- flight/template/View.php | 9 +++- flight/util/Collection.php | 1 + phpcs.xml | 6 +++ tests/FlightTest.php | 1 - tests/PdoWrapperTest.php | 4 +- tests/ViewTest.php | 1 - 15 files changed, 103 insertions(+), 75 deletions(-) diff --git a/flight/Engine.php b/flight/Engine.php index 71ae8a5..32a1a70 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -57,8 +57,7 @@ use flight\net\Route; * @method void redirect(string $url, int $code = 303) Redirects the current request to another URL. * @method void json(mixed $data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0) * Sends a JSON response. - * @method void jsonp(mixed $data, string $param = 'jsonp', int $code = 200, - * bool $encode = true, string $charset = 'utf-8', int $option = 0) Sends a JSONP response. + * @method void jsonp(mixed $data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0) Sends a JSONP response. * * # HTTP caching * @method void etag($id, string $type = 'strong') Handles ETag HTTP caching. diff --git a/flight/Flight.php b/flight/Flight.php index f30fd32..1f1d2ae 100644 --- a/flight/Flight.php +++ b/flight/Flight.php @@ -56,10 +56,8 @@ use flight\net\Route; * @method static Request request() Returns Request instance. * @method static Response response() Returns Response instance. * @method static void redirect($url, $code = 303) Redirects to another URL. - * @method static void json($data, $code = 200, $encode = true, $charset = "utf8", - * $encodeOption = 0, $encodeDepth = 512) Sends a JSON response. - * @method static void jsonp($data, $param = 'jsonp', $code = 200, $encode = true, - * $charset = "utf8", $encodeOption = 0, $encodeDepth = 512) Sends a JSONP response. + * @method static void json($data, $code = 200, $encode = true, $charset = "utf8", $encodeOption = 0, $encodeDepth = 512) Sends a JSON response. + * @method static void jsonp($data, $param = 'jsonp', $code = 200, $encode = true, $charset = "utf8", $encodeOption = 0, $encodeDepth = 512) Sends a JSONP response. * @method static void error($exception) Sends an HTTP 500 response. * @method static void notFound() Sends an HTTP 404 response. * diff --git a/flight/core/Dispatcher.php b/flight/core/Dispatcher.php index 1f55269..ff9c9ec 100644 --- a/flight/core/Dispatcher.php +++ b/flight/core/Dispatcher.php @@ -20,12 +20,14 @@ class Dispatcher { /** * Mapped events. + * * @var array */ protected array $events = []; /** * Method filters. + * * @var array>> */ protected array $filters = []; diff --git a/flight/core/Loader.php b/flight/core/Loader.php index 046f8d8..bf83e3f 100644 --- a/flight/core/Loader.php +++ b/flight/core/Loader.php @@ -20,30 +20,36 @@ class Loader { /** * Registered classes. + * * @var array, ?callable}> $classes */ protected array $classes = []; /** * Class instances. + * * @var array */ protected array $instances = []; /** * Autoload directories. + * * @var array */ protected static array $dirs = []; /** * Registers a class. - * @template T of object * * @param string $name Registry name * @param class-string|Closure(): T $class Class name or function to instantiate class * @param array $params Class initialization parameters * @param ?callable(T $instance): void $callback $callback Function to call after object instantiation + * + * @template T of object + * + * @return void */ public function register(string $name, $class, array $params = [], ?callable $callback = null): void { @@ -116,11 +122,12 @@ class Loader /** * Gets a new instance of a class. - * @template T of object * * @param class-string|Closure(): class-string $class Class name or callback function to instantiate class * @param array $params Class initialization parameters * + * @template T of object + * * @throws Exception * * @return T Class instance @@ -135,6 +142,8 @@ class Loader } /** + * Gets a registered callable + * * @param string $name Registry name * * @return mixed Class information or null if not registered diff --git a/flight/database/PdoWrapper.php b/flight/database/PdoWrapper.php index 87c930e..fd982a7 100644 --- a/flight/database/PdoWrapper.php +++ b/flight/database/PdoWrapper.php @@ -9,19 +9,6 @@ use PDOStatement; class PdoWrapper extends PDO { - /** - * How you create the connection for the database - * - * @param string $dsn - Ex: 'mysql:host=localhost;port=3306;dbname=testdb;charset=utf8mb4' - * @param string $username - Ex: 'root' - * @param string $password - Ex: 'password' - * @param array $options - PDO options you can pass in - */ - public function __construct(string $dsn, ?string $username = null, ?string $password = null, array $options = []) - { - parent::__construct($dsn, $username, $password, $options); - } - /** * Use this for INSERTS, UPDATES, or if you plan on using a SELECT in a while loop * @@ -35,6 +22,7 @@ class PdoWrapper extends PDO * * @param string $sql - Ex: "SELECT * FROM table WHERE something = ?" * @param array $params - Ex: [ $something ] + * * @return PDOStatement */ public function runQuery(string $sql, array $params = []): PDOStatement @@ -54,6 +42,7 @@ class PdoWrapper extends PDO * * @param string $sql - Ex: "SELECT id FROM table WHERE something = ?" * @param array $params - Ex: [ $something ] + * * @return mixed */ public function fetchField(string $sql, array $params = []) @@ -69,6 +58,7 @@ class PdoWrapper extends PDO * * @param string $sql - Ex: "SELECT * FROM table WHERE something = ?" * @param array $params - Ex: [ $something ] + * * @return array */ public function fetchRow(string $sql, array $params = []): array @@ -88,6 +78,7 @@ class PdoWrapper extends PDO * * @param string $sql - Ex: "SELECT * FROM table WHERE something = ?" * @param array $params - Ex: [ $something ] + * * @return array> */ public function fetchAll(string $sql, array $params = []): array @@ -109,48 +100,42 @@ class PdoWrapper extends PDO * * @param string $sql the sql statement * @param array $params the params for the sql statement + * * @return array> */ protected function processInStatementSql(string $sql, array $params = []): array { - /* Handle "IN(?)". This is to be used with a comma delimited string, but can also be used with an array. - Remove the spaces in variations of "IN ( ? )" where the space after IN is optional, and any number of - spaces before and after the question mark is optional. - Then loop through each "IN(?)" in the query and replace the single question mark with the correct - number of question marks. */ - $sql = preg_replace('/IN\s*\(\s*\?\s*\)/i', 'IN(?)', $sql); - $current_index = 0; - while (($current_index = strpos($sql, 'IN(?)', $current_index)) !== false) { - $preceeding_count = substr_count($sql, '?', 0, $current_index - 1); - - $param = $params[$preceeding_count]; - $question_marks = '?'; - - // If param is a string, explode it and replace the question mark with the correct number of question marks - if (is_string($param) || is_array($param)) { - $params_to_use = $param; - if (is_string($param)) { - $params_to_use = explode(',', $param); - } + // Replace "IN(?)" with "IN(?,?,?)" + $sql = preg_replace('/IN\s*\(\s*\?\s*\)/i', 'IN(?)', $sql); - foreach ($params_to_use as $key => $value) { - if (is_string($value)) { - $params_to_use[$key] = trim($value); - } - } + $current_index = 0; + while (($current_index = strpos($sql, 'IN(?)', $current_index)) !== false) { + $preceeding_count = substr_count($sql, '?', 0, $current_index - 1); - // Replace the single question mark with the appropriate number of question marks. - $question_marks = join(',', array_fill(0, count($params_to_use), '?')); - $sql = substr_replace($sql, $question_marks, $current_index + 3, 1); + $param = $params[$preceeding_count]; + $question_marks = '?'; - // Insert the new params into the params array. - array_splice($params, $preceeding_count, 1, $params_to_use); + if (is_string($param) || is_array($param)) { + $params_to_use = $param; + if (is_string($param)) { + $params_to_use = explode(',', $param); } - // Increment by the length of the question marks and accounting for the length of "IN()" - $current_index += strlen($question_marks) + 4; + foreach ($params_to_use as $key => $value) { + if (is_string($value)) { + $params_to_use[$key] = trim($value); + } + } + + $question_marks = join(',', array_fill(0, count($params_to_use), '?')); + $sql = substr_replace($sql, $question_marks, $current_index + 3, 1); + + array_splice($params, $preceeding_count, 1, $params_to_use); } - return [ 'sql' => $sql, 'params' => $params ]; + $current_index += strlen($question_marks) + 4; + } + + return [ 'sql' => $sql, 'params' => $params ]; } } diff --git a/flight/net/Request.php b/flight/net/Request.php index 6d90a2c..7fffd75 100644 --- a/flight/net/Request.php +++ b/flight/net/Request.php @@ -155,7 +155,7 @@ class Request 'scheme' => self::getScheme(), 'user_agent' => self::getVar('HTTP_USER_AGENT'), 'type' => self::getVar('CONTENT_TYPE'), - 'length' => (int) self::getVar('CONTENT_LENGTH', 0), + 'length' => intval(self::getVar('CONTENT_LENGTH', 0)), 'query' => new Collection($_GET), 'data' => new Collection($_POST), 'cookies' => new Collection($_COOKIE), @@ -174,7 +174,8 @@ class Request * Initialize request properties. * * @param array $properties Array of request properties - * @return $this + * + * @return self */ public function init(array $properties = []): self { @@ -303,6 +304,7 @@ class Request * * @param string $header Header name. Can be caps, lowercase, or mixed. * @param string $default Default value if the header does not exist + * * @return string */ public static function getHeader(string $header, $default = ''): string @@ -348,7 +350,11 @@ class Request return $params; } - /** @return 'http'|'https' */ + /** + * Gets the URL Scheme + * + * @return string 'http'|'https' + */ public static function getScheme(): string { if ( diff --git a/flight/net/Response.php b/flight/net/Response.php index 7bef496..55e2605 100644 --- a/flight/net/Response.php +++ b/flight/net/Response.php @@ -22,7 +22,9 @@ class Response public bool $content_length = true; /** - * @var array HTTP status codes + * HTTP status codes + * + * @var array $codes */ public static array $codes = [ 100 => 'Continue', @@ -100,7 +102,9 @@ class Response protected int $status = 200; /** - * @var array> HTTP headers + * HTTP response headers + * + * @var array> $headers */ protected array $headers = []; @@ -161,6 +165,7 @@ class Response /** * Returns the headers from the response. + * * @return array> */ public function headers(): array @@ -289,7 +294,9 @@ class Response * the same type. By default it will replace, but if you pass in false as the * second argument you can force multiple headers of the same type. * @param int $response_code The response code to send - * @return $this + * + * @return self + * * @codeCoverageIgnore */ public function setRealHeader(string $header_string, bool $replace = true, int $response_code = 0): self diff --git a/flight/net/Route.php b/flight/net/Route.php index bdf5827..27a4e70 100644 --- a/flight/net/Route.php +++ b/flight/net/Route.php @@ -20,17 +20,23 @@ class Route public string $pattern; /** - * @var mixed Callback function + * Callback function + * + * @var mixed */ public $callback; /** - * @var array HTTP methods + * HTTP methods + * + * @var array */ public array $methods = []; /** - * @var array Route parameters + * Route parameters + * + * @var array */ public array $params = []; @@ -55,7 +61,9 @@ class Route public string $alias = ''; /** - * @var array The middleware to be applied to the route + * The middleware to be applied to the route + * + * @var array */ public array $middleware = []; @@ -95,13 +103,13 @@ class Route $last_char = substr($this->pattern, -1); // Get splat - if ('*' === $last_char) { + if ($last_char === '*') { $n = 0; $len = \strlen($url); $count = substr_count($this->pattern, '/'); for ($i = 0; $i < $len; $i++) { - if ('/' === $url[$i]) { + if ($url[$i] === '/') { $n++; } if ($n === $count) { @@ -109,7 +117,7 @@ class Route } } - $this->splat = (string) substr($url, $i + 1); + $this->splat = strval(substr($url, $i + 1)); } // Build the regex for matching @@ -202,8 +210,9 @@ class Route /** * Sets the route middleware * - * @param array|callable $middleware - * @return $this + * @param array|callable $middleware + * + * @return self */ public function addMiddleware($middleware): self { diff --git a/flight/net/Router.php b/flight/net/Router.php index 6f6300a..485faa2 100644 --- a/flight/net/Router.php +++ b/flight/net/Router.php @@ -21,8 +21,11 @@ class Router * Case sensitive matching. */ public bool $case_sensitive = false; + /** - * @var array Mapped routes. + * Mapped routes. + * + * @var array $routes */ protected array $routes = []; diff --git a/flight/template/View.php b/flight/template/View.php index dfd29ee..d1bc07f 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -20,7 +20,11 @@ class View /** File extension. */ public string $extension = '.php'; - /** @var array View variables. */ + /** + * View variables. + * + * @var array $vars + */ protected array $vars = []; /** Template file. */ @@ -51,7 +55,8 @@ class View * * @param string|iterable $key * @param mixed $value Value - * @return $this + * + * @return self */ public function set($key, $value = null): self { diff --git a/flight/util/Collection.php b/flight/util/Collection.php index 0ede58a..6ffe0b5 100644 --- a/flight/util/Collection.php +++ b/flight/util/Collection.php @@ -22,6 +22,7 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable { /** * Collection data. + * * @var array */ private array $data; diff --git a/phpcs.xml b/phpcs.xml index b5bccbe..4cdfb52 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -29,8 +29,14 @@ + + + + flight/ tests/ + tests/views/* + tests/views/ diff --git a/tests/FlightTest.php b/tests/FlightTest.php index 2c964ff..2018d53 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -6,7 +6,6 @@ namespace tests; use Exception; use Flight; - use flight\Engine; use flight\net\Request; use flight\net\Response; diff --git a/tests/PdoWrapperTest.php b/tests/PdoWrapperTest.php index 53ff8e0..324c47b 100644 --- a/tests/PdoWrapperTest.php +++ b/tests/PdoWrapperTest.php @@ -98,7 +98,7 @@ class PdoWrapperTest extends TestCase public function testFetchAllWithInInt() { - $rows = $this->pdo_wrapper->fetchAll('SELECT id FROM test WHERE id IN(?)', [ [1,2 ]]); + $rows = $this->pdo_wrapper->fetchAll('SELECT id FROM test WHERE id IN(? )', [ [1,2 ]]); $this->assertEquals(2, count($rows)); } @@ -110,7 +110,7 @@ class PdoWrapperTest extends TestCase public function testFetchAllWithInStringCommas() { - $rows = $this->pdo_wrapper->fetchAll('SELECT id FROM test WHERE id > ? AND name IN(?)', [ 0, 'one,two' ]); + $rows = $this->pdo_wrapper->fetchAll('SELECT id FROM test WHERE id > ? AND name IN( ?) ', [ 0, 'one,two' ]); $this->assertEquals(2, count($rows)); } } diff --git a/tests/ViewTest.php b/tests/ViewTest.php index c29b051..fcb06c0 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace tests; use Exception; - use flight\template\View; use PHPUnit\Framework\TestCase;