Fixing PHPStan issues.

This commit is contained in:
Greyscale 2020-06-19 09:53:59 +02:00
parent 2a64baf123
commit 6adb6d5d42
20 changed files with 242 additions and 85 deletions

View file

@ -325,19 +325,11 @@ class App
return $this;
}
public function makeClean(): self
{
$this->setup();
$this->loadAllRoutes();
return $this;
}
public static function Log(int $level = Logger::DEBUG, $message)
{
return self::Instance()
->getContainer()
->get(Log\Logger::class)
->get(Logger::class)
->log($level, ($message instanceof \Exception) ? $message->__toString() : $message)
;
}
@ -355,44 +347,6 @@ class App
return $this;
}
public static function waitForMySQLToBeReady($connection = null)
{
if (!$connection) {
/** @var DbConfig $configs */
$dbConfig = self::Instance()->getContainer()->get(DatabaseConfig::class);
$configs = $dbConfig->__toArray();
if (isset($configs['Default'])) {
$connection = $configs['Default'];
} else {
foreach ($configs as $option => $connection) {
self::waitForMySQLToBeReady($connection);
}
return;
}
}
$ready = false;
echo "Waiting for MySQL ({$connection['hostname']}:{$connection['port']}) to come up...";
while (false == $ready) {
$conn = @fsockopen($connection['hostname'], $connection['port']);
if (is_resource($conn)) {
fclose($conn);
$ready = true;
} else {
echo '.';
usleep(500000);
}
}
echo " [DONE]\n";
/** @var Services\EnvironmentService $environmentService */
$environmentService = self::Container()->get(Services\EnvironmentService::class);
$environmentService->rebuildEnvironmentVariables();
}
public function runHttp(): void
{
$this->app->run();

View file

@ -2,10 +2,10 @@
namespace Benzine\Controllers;
use Slim\Http\Request;
use Slim\Http\Response;
use ⌬\Filters\Exceptions\FilterDecodeException;
use ⌬\Filters\Filter;
use Benzine\Exceptions\FilterDecodeException;
use Benzine\Controllers\Filters\Filter;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
abstract class Controller
{

View file

@ -4,8 +4,8 @@ namespace Benzine\Controllers;
use Benzine\ORM\Interfaces\ModelInterface;
use Laminas\Db\Adapter\Exception\InvalidQueryException;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
abstract class CrudController extends Controller
{

View file

@ -0,0 +1,177 @@
<?php
namespace Benzine\Controllers\Filters;
use Benzine\Exceptions\FilterDecodeException;
use Laminas\Db\Sql\Expression;
class Filter
{
protected $limit;
protected $offset;
protected $wheres;
protected $order;
protected $orderDirection;
/**
* @return mixed
*/
public function getOrderDirection()
{
return $this->orderDirection;
}
/**
* @param mixed $orderDirection
*
* @throws FilterDecodeException
*
* @return Filter
*/
public function setOrderDirection($orderDirection): self
{
if (!in_array(strtoupper($orderDirection), ['ASC', 'DESC', 'RAND'], true)) {
throw new FilterDecodeException("Failed to decode Filter Order, Direction unknown: {$orderDirection} must be ASC|DESC|RAND");
}
$this->orderDirection = strtoupper($orderDirection);
return $this;
}
/**
* @param $header
*
* @throws FilterDecodeException
*/
public function parseFromHeader($header): self
{
foreach ($header as $key => $value) {
switch ($key) {
case 'limit':
$this->setLimit($value);
break;
case 'offset':
$this->setOffset($value);
break;
case 'wheres':
$this->setWheres($value);
break;
case 'order':
$this->parseOrder($value);
break;
default:
throw new FilterDecodeException("Failed to decode Filter, unknown key: {$key}");
}
}
return $this;
}
/**
* @return mixed
*/
public function getLimit()
{
return $this->limit;
}
/**
* @param mixed $limit
*
* @return Filter
*/
public function setLimit($limit): self
{
$this->limit = $limit;
return $this;
}
/**
* @return mixed
*/
public function getOffset()
{
return $this->offset;
}
/**
* @param mixed $offset
*
* @return Filter
*/
public function setOffset($offset): self
{
$this->offset = $offset;
return $this;
}
/**
* @return mixed
*/
public function getWheres()
{
return $this->wheres;
}
/**
* @param mixed $wheres
*
* @return Filter
*/
public function setWheres($wheres): self
{
$this->wheres = $wheres;
return $this;
}
/**
* @return mixed
*/
public function getOrder()
{
return $this->order;
}
/**
* @param mixed $order
*
* @return Filter
*/
public function setOrder($order): self
{
$this->order = $order;
return $this;
}
public function setOrderRandom(): self
{
$this->setOrder(new Expression('RAND()'));
return $this;
}
public function parseOrder($orderArray): self
{
if (in_array(strtolower($orderArray['column']), ['rand', 'random', 'rand()'], true)) {
$this->setOrderRandom();
} elseif (isset($orderArray['column'], $orderArray['direction'])) {
$this->setOrder($orderArray['column']);
if (isset($orderArray['direction'])) {
$this->setOrderDirection($orderArray['direction']);
}
} else {
throw new FilterDecodeException("Could not find properties 'column' or 'direction' of the order array given.");
}
return $this;
}
}

View file

@ -0,0 +1,17 @@
<?php
namespace Benzine\Controllers\Filters;
class FilterCondition
{
public const CONDITION_EQUAL = '=';
public const CONDITION_NOT_EQUAL = '!=';
public const CONDITION_GREATER_THAN = '>';
public const CONDITION_LESS_THAN = '<';
public const CONDITION_GREATER_THAN_OR_EQUAL = '>=';
public const CONDITION_LESS_THAN_OR_EQUAL = '<=';
public const CONDITION_LIKE = 'LIKE';
public const CONDITION_NOT_LIKE = 'NOT LIKE';
public const CONDITION_IN = 'IN';
public const CONDITION_NOT_IN = 'NOT IN';
}

View file

@ -2,8 +2,8 @@
namespace Benzine\Controllers;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
use Slim\Views\Twig;
abstract class HtmlController extends Controller

View file

@ -2,6 +2,6 @@
namespace Benzine\Exceptions;
class BenzineConfigurationException extends Exception
class BenzineConfigurationException extends BenzineException
{
}

View file

@ -2,6 +2,6 @@
namespace Benzine\Exceptions;
class Exception extends \Exception
class BenzineException extends \Exception
{
}

View file

@ -2,6 +2,6 @@
namespace Benzine\Exceptions;
class DbConfigException extends Exception
class DbConfigException extends BenzineException
{
}

View file

@ -0,0 +1,7 @@
<?php
namespace Benzine\Exceptions;
class FilterDecodeException extends BenzineException
{
}

View file

@ -3,9 +3,9 @@
namespace Benzine\Middleware;
use Faker\Factory;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Route;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
use Benzine\Router\Route;
class AccessRequirements
{

View file

@ -2,8 +2,8 @@
namespace Benzine\Middleware;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
class CORSHeadersOnResponse
{

View file

@ -4,21 +4,20 @@ namespace Benzine\Middleware;
use Benzine\Configuration;
use Benzine\ORM\Profiler;
use Benzine\Services\ConfigurationService;
use Benzine\⌬;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
class EnvironmentHeadersOnResponse
{
protected $apiExplorerEnabled = true;
/** @var Configuration\Configuration */
protected $configuration;
/** @var Profiler\Profiler */
protected $profiler;
protected ConfigurationService $configuration;
protected Profiler\Profiler $profiler;
public function __construct(
Configuration\Configuration $configuration,
ConfigurationService $configuration,
Profiler\Profiler $profiler
) {
$this->configuration = $configuration;
@ -38,8 +37,8 @@ class EnvironmentHeadersOnResponse
$json = json_decode($body->getContents(), true);
$gitVersion = null;
if (file_exists($this->configuration->get(Configuration\Configuration::KEY_APP_ROOT).'/version.txt')) {
$gitVersion = trim(file_get_contents($this->configuration->get(Configuration\Configuration::KEY_APP_ROOT).'/version.txt'));
if (file_exists(APP_ROOT.'/version.txt')) {
$gitVersion = trim(file_get_contents(APP_ROOT.'/version.txt'));
$gitVersion = explode(' ', $gitVersion, 2);
$gitVersion = reset($gitVersion);
}
@ -55,7 +54,7 @@ class EnvironmentHeadersOnResponse
'Human' => date('Y-m-d H:i:s'),
'Epoch' => time(),
],
'Exec' => number_format(microtime(true) - $this->configuration->get(Configuration\Configuration::KEY_APP_START), 4).' sec',
'Exec' => number_format(microtime(true) - APP_START, 4).' sec',
] : null,
'Memory' => defined('DEBUG') && DEBUG ? [
'Used' => number_format(memory_get_usage(false) / 1024 / 1024, 2).'MB',
@ -63,7 +62,6 @@ class EnvironmentHeadersOnResponse
'Limit' => ini_get('memory_limit'),
] : null,
'SQL' => defined('DEBUG') && DEBUG ? $this->profiler->getQueriesArray() : null,
'API' => defined('DEBUG') && DEBUG && class_exists('\Gone\SDK\Common\Profiler') ? \Gone\SDK\Common\Profiler::debugArray() : null,
]);
if (isset($json['Status'])) {
@ -88,8 +86,8 @@ class EnvironmentHeadersOnResponse
'json' => $json,
'json_pretty_printed_rows' => explode("\n", json_encode($json, JSON_PRETTY_PRINT)),
'inline_css' => $this->renderInlineCss([
$this->configuration->get(Configuration\Configuration::KEY_APP_ROOT).'/vendor/benzine/benzine-http-assets/css/reset.css',
$this->configuration->get(Configuration\Configuration::KEY_APP_ROOT).'/vendor/benzine/benzine-http-assets/css/api-explorer.css',
APP_ROOT.'/vendor/benzine/benzine-http-assets/css/reset.css',
APP_ROOT.'/vendor/benzine/benzine-http-assets/css/api-explorer.css',
]),
]);
$response = $response->withHeader('Content-type', 'text/html');

View file

@ -2,8 +2,8 @@
namespace Benzine\Middleware;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
class ForceSSLMiddleware
{

View file

@ -2,8 +2,8 @@
namespace Benzine\Middleware;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
class JSONResponseLinter
{

View file

@ -88,6 +88,7 @@ class ConfigurationService
{
define('APP_ROOT', $this->appRoot);
define('APP_NAME', $this->get('application/name'));
define('APP_START', microtime(true));
}
/**

View file

@ -2,7 +2,7 @@
namespace Benzine\Services;
use \UUID\UUID;
use Gone\UUID\UUID;
class QueueService
{

View file

@ -3,6 +3,7 @@
namespace Benzine\Workers;
use Benzine\Services\EnvironmentService;
use Benzine\Services\QueueService;
use Monolog\Logger;
abstract class AbstractQueueWorker extends AbstractWorker
@ -35,7 +36,7 @@ abstract class AbstractQueueWorker extends AbstractWorker
}
$this->logger->debug(
sprintf(
'Listening to "%s" and outputting on %d channel(s)',
'Worker %s: Listening to "%s" and outputting on %d channel(s)',
$this->getClassWithoutNamespace(),
$this->inputQueue,
count($this->outputQueues)
@ -128,7 +129,9 @@ abstract class AbstractQueueWorker extends AbstractWorker
foreach ($items as $item) {
$processResults = $this->process($item);
if (is_array($processResults)) {
$resultItems[] += $processResults;
foreach($processResults as $processResult) {
$resultItems[] = $processResult;
}
} else {
$resultItems[] = $processResults;
}

View file

@ -5,7 +5,7 @@ namespace Benzine\Workers;
use Benzine\Services\EnvironmentService;
use Monolog\Logger;
abstract class AbstractWorker
abstract class AbstractWorker implements WorkerInterface
{
protected Logger $logger;
protected EnvironmentService $environmentService;

View file

@ -2,7 +2,7 @@
namespace Benzine\Workers;
use ⌬\Controllers\Abstracts\Model;
use Benzine\ORM\Abstracts\Model;
class WorkerWorkItem
{