Skip to content

Commit

Permalink
Merge pull request #563 from skipperbent/v4-development
Browse files Browse the repository at this point in the history
Version 4.3.6.0
  • Loading branch information
skipperbent committed Jun 9, 2021
2 parents 319ce7a + a35400b commit e990b95
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 56 deletions.
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
},
"require-dev": {
"phpunit/phpunit": "^7",
"mockery/mockery": "^1"
"mockery/mockery": "^1",
"phpstan/phpstan": "^0",
"phpstan/phpstan-phpunit": "^0",
"phpstan/phpstan-deprecation-rules": "^0",
"phpstan/phpstan-strict-rules": "^0"
},
"scripts": {
"test": [
Expand All @@ -44,4 +48,4 @@
"Pecee\\": "src/Pecee/"
}
}
}
}
22 changes: 22 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
parameters:
level: 6
paths:
- src
fileExtensions:
- php
bootstrapFiles:
- ./vendor/autoload.php
ignoreErrors:
reportUnmatchedIgnoredErrors: true
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
parallel:
processTimeout: 300.0
jobSize: 10
maximumNumberOfProcesses: 4
minimumNumberOfJobsPerProcess: 4
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
6 changes: 6 additions & 0 deletions src/Pecee/Http/Input/IInputItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ public function getName(): ?string;

public function setName(string $name): self;

/**
* @return mixed
*/
public function getValue();

/**
* @param mixed $value
*/
public function setValue($value): self;

public function __toString(): string;
Expand Down
4 changes: 2 additions & 2 deletions src/Pecee/Http/Input/InputHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function parseFiles(array $files, ?string $parentKey = null): array

// Handle array input
if (is_array($value['name']) === false) {
$values['index'] = $parentKey ?? $key;
$values = ['index' => $parentKey ?? $key];

try {
$list[$key] = InputFile::createFromArray($values + $value);
Expand Down Expand Up @@ -161,7 +161,7 @@ protected function rearrangeFile(array $values, array &$index, ?array $original)
try {

$file = InputFile::createFromArray([
'index' => (empty($key) === true && empty($originalIndex) === false) ? $originalIndex : $key,
'index' => ($key === '' && $originalIndex !== '') ? $originalIndex : $key,
'name' => $original['name'][$key],
'error' => $original['error'][$key],
'tmp_name' => $original['tmp_name'][$key],
Expand Down
2 changes: 1 addition & 1 deletion src/Pecee/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function __construct()

public function isSecure(): bool
{
return $this->getHeader('http-x-forwarded-proto') === 'https' || $this->getHeader('https') !== null || $this->getHeader('server-port') === 443;
return $this->getHeader('http-x-forwarded-proto') === 'https' || $this->getHeader('https') !== null || (int)$this->getHeader('server-port') === 443;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Pecee/Http/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public function parseUrl(string $url, int $component = -1): array
{
$encodedUrl = preg_replace_callback(
'/[^:\/@?&=#]+/u',
static function ($matches) {
static function ($matches): string {
return urlencode($matches[0]);
},
$url
Expand All @@ -414,7 +414,7 @@ public static function arrayToParams(array $getParams = [], bool $includeEmpty =
if (count($getParams) !== 0) {

if ($includeEmpty === false) {
$getParams = array_filter($getParams, static function ($item) {
$getParams = array_filter($getParams, static function ($item): bool {
return (trim($item) !== '');
});
}
Expand Down Expand Up @@ -458,15 +458,15 @@ public function getAbsoluteUrl(bool $includeParams = true): string
$port = $this->port !== null ? ':' . $this->port : '';
$user = $this->username ?? '';
$pass = $this->password !== null ? ':' . $this->password : '';
$pass = ($user || $pass) ? $pass . '@' : '';
$pass = ($user !== '' || $pass !== '') ? $pass . '@' : '';

return $scheme . $user . $pass . $host . $port . $this->getRelativeUrl($includeParams);
}

/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* @return string data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Pecee/SimpleRouter/Handlers/DebugEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DebugEventHandler implements IEventHandler

public function __construct()
{
$this->callback = static function (EventArgument $argument) {
$this->callback = static function (EventArgument $argument): void {
// todo: log in database
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Pecee/SimpleRouter/Route/IControllerRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Pecee\SimpleRouter\Route;

interface IControllerRoute extends IRoute
interface IControllerRoute extends ILoadableRoute
{
/**
* Get controller class-name
Expand Down
13 changes: 11 additions & 2 deletions src/Pecee/SimpleRouter/Route/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ abstract class Route implements IRoute
protected $urlRegex = '/^%s\/?$/u';
protected $group;
protected $parent;
/**
* @var string|callable|null
*/
protected $callback;
protected $defaultNamespace;

Expand Down Expand Up @@ -67,7 +70,7 @@ public function renderRoute(Request $request, Router $router): ?string

/* Filter parameters with null-value */
if ($this->filterEmptyParams === true) {
$parameters = array_filter($parameters, static function ($var) {
$parameters = array_filter($parameters, static function ($var): bool {
return ($var !== null);
});
}
Expand All @@ -82,6 +85,7 @@ public function renderRoute(Request $request, Router $router): ?string
}

/* When the callback is a function */

return $router->getClassLoader()->loadClosure($callback, $parameters);
}

Expand Down Expand Up @@ -280,7 +284,7 @@ public function setCallback($callback): IRoute
}

/**
* @return string|callable
* @return string|callable|null
*/
public function getCallback()
{
Expand Down Expand Up @@ -337,6 +341,11 @@ public function setClass(string $class): IRoute
*/
public function setNamespace(string $namespace): IRoute
{
// Do not set namespace when class-hinting is used
if (is_array($this->callback) === true) {
return $this;
}

$ns = $this->getNamespace();

if ($ns !== null) {
Expand Down
4 changes: 2 additions & 2 deletions src/Pecee/SimpleRouter/Route/RouteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function hasName(string $name): bool
public function findUrl(?string $method = null, $parameters = null, ?string $name = null): string
{
if (strpos($name, '.') !== false) {
$found = array_search(substr($name, strrpos($name, '.') + 1), $this->names, false);
$found = array_search(substr($name, strrpos($name, '.') + 1), $this->names, true);
if ($found !== false) {
$method = (string)$found;
}
Expand All @@ -67,7 +67,7 @@ public function findUrl(?string $method = null, $parameters = null, ?string $nam
foreach (Request::$requestTypes as $requestType) {

if (stripos($method, $requestType) === 0) {
$method = (string)substr($method, strlen($requestType));
$method = substr($method, strlen($requestType));
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Pecee/SimpleRouter/Route/RouteResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function hasName(string $name): bool

/* Remove method/type */
if (strpos($name, '.') !== false) {
$name = (string)substr($name, 0, strrpos($name, '.'));
$name = substr($name, 0, strrpos($name, '.'));
}

return (strtolower($this->name) === strtolower($name));
Expand All @@ -68,7 +68,7 @@ public function hasName(string $name): bool
*/
public function findUrl(?string $method = null, $parameters = null, ?string $name = null): string
{
$url = array_search($name, $this->names, false);
$url = array_search($name, $this->names, true);
if ($url !== false) {
return rtrim($this->url . $this->urls[$url], '/') . '/';
}
Expand Down
29 changes: 12 additions & 17 deletions src/Pecee/SimpleRouter/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Router

/**
* List of processed routes
* @var array
* @var array|ILoadableRoute[]
*/
protected $processedRoutes = [];

Expand All @@ -63,7 +63,7 @@ class Router

/**
* Csrf verifier class
* @var BaseCsrfVerifier
* @var BaseCsrfVerifier|null
*/
protected $csrfVerifier;

Expand Down Expand Up @@ -107,7 +107,7 @@ class Router

/**
* Class loader instance
* @var ClassLoader
* @var IClassLoader
*/
protected $classLoader;

Expand Down Expand Up @@ -215,7 +215,7 @@ protected function processRoutes(array $routes, ?IGroupRoute $group = null): voi
$exceptionHandlers = [];

// Stop processing routes if no valid route is found.
if ($this->request->getRewriteRoute() === null && $this->request->getUrl() === null) {
if ($this->request->getRewriteRoute() === null && $this->request->getUrl()->getOriginalUrl() === '') {
$this->debug('Halted route-processing as no valid route was found');

return;
Expand Down Expand Up @@ -575,7 +575,6 @@ public function findRoute(string $name): ?ILoadableRoute
'name' => $name,
]);

/* @var $route ILoadableRoute */
foreach ($this->processedRoutes as $route) {

/* Check if the name matches with a name on the route. Should match either router alias or controller alias. */
Expand All @@ -593,7 +592,7 @@ public function findRoute(string $name): ?ILoadableRoute
}

/* Using @ is most definitely a controller@method or alias@method */
if (is_string($name) === true && strpos($name, '@') !== false) {
if (strpos($name, '@') !== false) {
[$controller, $method] = array_map('strtolower', explode('@', $name));

if ($controller === strtolower($route->getClass()) && $method === strtolower($route->getMethod())) {
Expand All @@ -605,7 +604,7 @@ public function findRoute(string $name): ?ILoadableRoute

/* Check if callback matches (if it's not a function) */
$callback = $route->getCallback();
if (is_string($name) === true && is_string($callback) === true && is_callable($callback) === false && strpos($name, '@') !== false && strpos($callback, '@') !== false) {
if (is_string($callback) === true && is_callable($callback) === false && strpos($name, '@') !== false && strpos($callback, '@') !== false) {

/* Check if the entire callback is matching */
if (strpos($callback, $name) === 0 || strtolower($callback) === strtolower($name)) {
Expand Down Expand Up @@ -656,10 +655,6 @@ public function getUrl(?string $name = null, $parameters = null, ?array $getPara
'getParams' => $getParams,
]);

if ($getParams !== null && is_array($getParams) === false) {
throw new InvalidArgumentException('Invalid type for getParams. Must be array or null');
}

if ($name === '' && $parameters === '') {
return new Url('/');
}
Expand Down Expand Up @@ -703,21 +698,21 @@ public function getUrl(?string $name = null, $parameters = null, ?array $getPara
/* Loop through all the routes to see if we can find a match */

/* @var $route ILoadableRoute */
foreach ($this->processedRoutes as $route) {
foreach ($this->processedRoutes as $processedRoute) {

/* Check if the route contains the name/alias */
if ($route->hasName($controller) === true) {
if ($processedRoute->hasName($controller) === true) {
return $this->request
->getUrlCopy()
->setPath($route->findUrl($method, $parameters, $name))
->setPath($processedRoute->findUrl($method, $parameters, $name))
->setParams($getParams);
}

/* Check if the route controller is equal to the name */
if ($route instanceof IControllerRoute && strtolower($route->getController()) === strtolower($controller)) {
if ($processedRoute instanceof IControllerRoute && strtolower($processedRoute->getController()) === strtolower($controller)) {
return $this->request
->getUrlCopy()
->setPath($route->findUrl($method, $parameters, $name))
->setPath($processedRoute->findUrl($method, $parameters, $name))
->setParams($getParams);
}

Expand Down Expand Up @@ -842,7 +837,7 @@ public function setClassLoader(IClassLoader $loader): void
/**
* Get class loader
*
* @return ClassLoader
* @return IClassLoader
*/
public function getClassLoader(): IClassLoader
{
Expand Down
Loading

0 comments on commit e990b95

Please sign in to comment.