so many phpcs fixes. No more, no more!

pull/533/head
n0nag0n 1 year ago
parent ff6fa74d51
commit df90d69787

@ -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.

@ -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.
*

@ -20,12 +20,14 @@ class Dispatcher
{
/**
* Mapped events.
*
* @var array<string, callable>
*/
protected array $events = [];
/**
* Method filters.
*
* @var array<string, array<'before'|'after', array<int, callable>>>
*/
protected array $filters = [];

@ -20,30 +20,36 @@ class Loader
{
/**
* Registered classes.
*
* @var array<string, array{class-string|Closure(): object, array<int, mixed>, ?callable}> $classes
*/
protected array $classes = [];
/**
* Class instances.
*
* @var array<string, object>
*/
protected array $instances = [];
/**
* Autoload directories.
*
* @var array<int, string>
*/
protected static array $dirs = [];
/**
* Registers a class.
* @template T of object
*
* @param string $name Registry name
* @param class-string<T>|Closure(): T $class Class name or function to instantiate class
* @param array<int, mixed> $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<T>|Closure(): class-string<T> $class Class name or callback function to instantiate class
* @param array<int, string> $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

@ -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<int,mixed> $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<int|string,mixed> $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<int|string,mixed> $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<int|string,mixed> $params - Ex: [ $something ]
*
* @return array<string,mixed>
*/
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<int|string,mixed> $params - Ex: [ $something ]
*
* @return array<int,array<string,mixed>>
*/
public function fetchAll(string $sql, array $params = []): array
@ -109,48 +100,42 @@ class PdoWrapper extends PDO
*
* @param string $sql the sql statement
* @param array<int|string,mixed> $params the params for the sql statement
*
* @return array<string,string|array<int|string,mixed>>
*/
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 ];
}
}

@ -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<string, mixed> $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 (

@ -22,7 +22,9 @@ class Response
public bool $content_length = true;
/**
* @var array<int, ?string> HTTP status codes
* HTTP status codes
*
* @var array<int, ?string> $codes
*/
public static array $codes = [
100 => 'Continue',
@ -100,7 +102,9 @@ class Response
protected int $status = 200;
/**
* @var array<string, int|string|array<int, string>> HTTP headers
* HTTP response headers
*
* @var array<string,int|string|array<int,string>> $headers
*/
protected array $headers = [];
@ -161,6 +165,7 @@ class Response
/**
* Returns the headers from the response.
*
* @return array<string, int|string|array<int, string>>
*/
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

@ -20,17 +20,23 @@ class Route
public string $pattern;
/**
* @var mixed Callback function
* Callback function
*
* @var mixed
*/
public $callback;
/**
* @var array<int, string> HTTP methods
* HTTP methods
*
* @var array<int, string>
*/
public array $methods = [];
/**
* @var array<int, ?string> Route parameters
* Route parameters
*
* @var array<int, ?string>
*/
public array $params = [];
@ -55,7 +61,9 @@ class Route
public string $alias = '';
/**
* @var array<int,callable|object> The middleware to be applied to the route
* The middleware to be applied to the route
*
* @var array<int,callable|object>
*/
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<int, callable>|callable $middleware
* @return $this
* @param array<int,callable>|callable $middleware
*
* @return self
*/
public function addMiddleware($middleware): self
{

@ -21,8 +21,11 @@ class Router
* Case sensitive matching.
*/
public bool $case_sensitive = false;
/**
* @var array<int,Route> Mapped routes.
* Mapped routes.
*
* @var array<int,Route> $routes
*/
protected array $routes = [];

@ -20,7 +20,11 @@ class View
/** File extension. */
public string $extension = '.php';
/** @var array<string, mixed> View variables. */
/**
* View variables.
*
* @var array<string, mixed> $vars
*/
protected array $vars = [];
/** Template file. */
@ -51,7 +55,8 @@ class View
*
* @param string|iterable<string, mixed> $key
* @param mixed $value Value
* @return $this
*
* @return self
*/
public function set($key, $value = null): self
{

@ -22,6 +22,7 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
{
/**
* Collection data.
*
* @var array<string, mixed>
*/
private array $data;

@ -29,8 +29,14 @@
<exclude name="Generic.Commenting.DocComment.SpacingBeforeShort" />
<exclude name="Generic.Commenting.DocComment.ContentAfterOpen" />
<exclude name="Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine" />
<exclude name="Generic.PHP.DisallowRequestSuperglobal.Found" />
<exclude name="Generic.Commenting.DocComment.ContentBeforeClose" />
<exclude name="Generic.ControlStructures.DisallowYodaConditions.Found" />
<exclude name="Generic.Strings.UnnecessaryStringConcat.Found" />
<exclude name="Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition" />
</rule>
<file>flight/</file>
<file>tests/</file>
<exclude-pattern>tests/views/*</exclude-pattern>
<ignore>tests/views/</ignore>
</ruleset>

@ -6,7 +6,6 @@ namespace tests;
use Exception;
use Flight;
use flight\Engine;
use flight\net\Request;
use flight\net\Response;

@ -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));
}
}

@ -5,7 +5,6 @@ declare(strict_types=1);
namespace tests;
use Exception;
use flight\template\View;
use PHPUnit\Framework\TestCase;

Loading…
Cancel
Save