From 0518a5bc4538415f9cd333ace486cf9bff48299a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Tue, 17 Jul 2018 23:25:11 +0200 Subject: [PATCH 1/5] Moving out some classes back to mvc.splash-common --- .../Splash/Controllers/Controller.php | 23 - .../Controllers/DefaultSplashTemplate.php | 26 - src/TheCodingMachine/Splash/HtmlResponse.php | 86 ---- .../Services/MoufExplorerUrlProvider.php | 52 -- .../SplashCreateControllerService.php | 477 ------------------ ...SplashCreateControllerServiceException.php | 26 - .../Splash/Utils/ExceptionUtils.php | 231 --------- src/views/400.php | 6 - src/views/500.php | 15 - src/views/template.php | 24 - 10 files changed, 966 deletions(-) delete mode 100644 src/TheCodingMachine/Splash/Controllers/Controller.php delete mode 100644 src/TheCodingMachine/Splash/Controllers/DefaultSplashTemplate.php delete mode 100644 src/TheCodingMachine/Splash/HtmlResponse.php delete mode 100644 src/TheCodingMachine/Splash/Services/MoufExplorerUrlProvider.php delete mode 100644 src/TheCodingMachine/Splash/Services/SplashCreateControllerService.php delete mode 100644 src/TheCodingMachine/Splash/Services/SplashCreateControllerServiceException.php delete mode 100644 src/TheCodingMachine/Splash/Utils/ExceptionUtils.php delete mode 100644 src/views/400.php delete mode 100644 src/views/500.php delete mode 100644 src/views/template.php diff --git a/src/TheCodingMachine/Splash/Controllers/Controller.php b/src/TheCodingMachine/Splash/Controllers/Controller.php deleted file mode 100644 index ac9af11..0000000 --- a/src/TheCodingMachine/Splash/Controllers/Controller.php +++ /dev/null @@ -1,23 +0,0 @@ -setContent($content); - } - - /** - * Renders the object in HTML. - * The Html is echoed directly into the output. - */ - public function toHtml() - { - header('Content-Type: text/html; charset=utf-8'); - - include __DIR__.'/../../../../views/template.php'; - } -} diff --git a/src/TheCodingMachine/Splash/HtmlResponse.php b/src/TheCodingMachine/Splash/HtmlResponse.php deleted file mode 100644 index 131a9ba..0000000 --- a/src/TheCodingMachine/Splash/HtmlResponse.php +++ /dev/null @@ -1,86 +0,0 @@ - - */ -class HtmlResponse extends Response -{ - /** - * @var HtmlElementInterface - */ - protected $htmlElement; - - /** - * @var Stream - */ - protected $stream; - - /** - * Constructor. - * - * @param HtmlElementInterface $htmlElement An HtmlElement to render. - * @param int $status The response status code - * @param array $headers An array of response headers - */ - public function __construct(HtmlElementInterface $htmlElement, $status = 200, $headers = array()) - { - parent::__construct('php://temp', $status, $headers); - - $this->htmlElement = $htmlElement; - } - - /** - * {@inheritdoc} - */ - public static function create(HtmlElementInterface $htmlElement, $status = 200, $headers = array()) - { - return new static($htmlElement, $status, $headers); - } - - /** - * Sets the HtmlElement to be rendered. - * - * @param HtmlElementInterface $htmlElement - */ - public function setHtmlElement(HtmlElementInterface $htmlElement) - { - $this->htmlElement = $htmlElement; - } - - /** - * Returns the HtmlElement to be rendered. - * - * @return \Mouf\Html\HtmlElement\HtmlElementInterface - */ - public function getHtmlElement() - { - return $this->htmlElement; - } - - /** - * Gets the body of the message. - * - * @return StreamInterface Returns the body as a stream. - */ - public function getBody() - { - if ($this->stream === null) { - ob_start(); - $this->htmlElement->toHtml(); - $content = ob_get_clean(); - $this->stream = new Stream('php://memory', 'wb+'); - $this->stream->write($content); - } - - return $this->stream; - } -} diff --git a/src/TheCodingMachine/Splash/Services/MoufExplorerUrlProvider.php b/src/TheCodingMachine/Splash/Services/MoufExplorerUrlProvider.php deleted file mode 100644 index bcc8e8e..0000000 --- a/src/TheCodingMachine/Splash/Services/MoufExplorerUrlProvider.php +++ /dev/null @@ -1,52 +0,0 @@ -findInstances('Mouf\\Mvc\\Splash\\Services\\UrlProviderInterface'); - - $urls = array(); - - foreach ($instanceNames as $instanceName) { - $urlProvider = $moufManager->getInstance($instanceName); - /* @var $urlProvider UrlProviderInterface */ - if ($urlProvider === $this) { - continue; - } - $tmpUrlList = $urlProvider->getUrlsList($instanceName); - $urls = array_merge($urls, $tmpUrlList); - } - - return $urls; - } - - /** - * Returns a unique tag representing the list of SplashRoutes returned. - * If the tag changes, the cache is flushed by Splash. - * - * Important! This must be quick to compute. - * - * @return mixed - */ - public function getExpirationTag() : string - { - return filemtime(__DIR__ . '/../../../../../../../../mouf/MoufComponents.php'); - } -} diff --git a/src/TheCodingMachine/Splash/Services/SplashCreateControllerService.php b/src/TheCodingMachine/Splash/Services/SplashCreateControllerService.php deleted file mode 100644 index 34e9fb8..0000000 --- a/src/TheCodingMachine/Splash/Services/SplashCreateControllerService.php +++ /dev/null @@ -1,477 +0,0 @@ -getPossibleFileNames($namespace.$controllerName); - if (!isset($possibleFileNames[0])) { - throw new SplashException("The class '".$namespace.$controllerName."' cannot be loaded using rules defined in composer autoload section"); - } - $fileName = $possibleFileNames[0]; - $controllerPhpDirectory = dirname($fileName); - $errors = array(); - if (!preg_match('/^[a-z_]\w*$/i', $controllerName)) { - $errors['controllerNameError'] = 'This is not a valid PHP class name.'; - } - if (!preg_match('/^[a-z_][\w\\\\]*$/i', $namespace)) { - $errors['namespaceError'] = 'This is not a valid PHP namespace.'; - } - - $namespace = trim($namespace, '\\'); - - if (!file_exists(ROOT_PATH.'../database.tdbm') && $injectDaoFactory) { - $injectDaoFactory = false; - } - - // Check that instance does not already exists - if ($moufManager->has($instanceName)) { - $errors['instanceError'] = 'This instance already exists.'; - } - - $injectTwig = false; - $importJsonResponse = false; - $importHtmlResponse = false; - $importRedirectResponse = false; - - foreach ($actions as $key => $action) { - // Check if the view file exists - if ($injectTemplate && $action['view'] == 'twig') { - $injectTwig = true; - $importHtmlResponse = true; - $twigFile = ltrim($action['twigFile'], '/\\'); - - $viewDirName = ROOT_PATH.'../../../'.dirname($twigFile); - $result = $this->createDirectory($viewDirName); - if (!$result) { - $errors['actions'][$key]['twigTemplateFileError'] = 'Unable to create directory "'.$viewDirName.'"'; - } - - if (file_exists(ROOT_PATH.'../../../'.$twigFile)) { - $errors['actions'][$key]['twigTemplateFileError'] = 'This file already exists.'; - } - } - if ($injectTemplate && $action['view'] == 'php') { - $importHtmlResponse = true; - - $phpFile = ltrim($action['phpFile'], '/\\'); - - $viewDirName = ROOT_PATH.'../../../'.dirname($phpFile); - $result = $this->createDirectory($viewDirName); - if (!$result) { - $errors['actions'][$key]['phpTemplateFileError'] = 'Unable to create directory "'.$viewDirName.'"'; - } - - if (file_exists(ROOT_PATH.'../../../'.$phpFile)) { - $errors['actions'][$key]['phpTemplateFileError'] = 'This file already exists.'; - } - } - if ($action['view'] == 'redirect') { - if (!isset($action['redirect']) || empty($action['redirect'])) { - $errors['actions'][$key]['redirectError'] = 'Redirection URL cannot be empty.'; - } - $importRedirectResponse = true; - } - if ($action['view'] == 'json') { - $importJsonResponse = true; - } - } - - // TODO: check that URLs are not in error. - - - if (!$errors) { - $result = $this->createDirectory(ROOT_PATH.'../../../'.$controllerPhpDirectory); - if (!$result) { - $errors['namespaceError'] = 'Unable to create directory: "'.$controllerPhpDirectory.'"'; - } elseif (file_exists(ROOT_PATH.'../../../'.$controllerPhpDirectory.$controllerName.'.php')) { - $errors['namespaceError'] = 'The file "'.$controllerPhpDirectory.$controllerName.'.php already exists."'; - } elseif (!is_writable(ROOT_PATH.'../../../'.$controllerPhpDirectory)) { - $errors['namespaceError'] = 'Unable to write file in directory: "'.$controllerPhpDirectory.'"'; - } - - if (!$errors) { - ob_start(); - echo ' -namespace ; - -use TheCodingMachine\Splash\Annotations\Get; -use TheCodingMachine\Splash\Annotations\Post; -use TheCodingMachine\Splash\Annotations\Put; -use TheCodingMachine\Splash\Annotations\Delete; -use TheCodingMachine\Splash\Annotations\URL; - -use Mouf\Html\Template\TemplateInterface; -use Mouf\Html\HtmlElement\HtmlBlock; - - -use Psr\Log\LoggerInterface; - - -use getVariable('tdbmDefaultDaoNamespace').'\\Generated\\'.$moufManager->getVariable('tdbmDefaultDaoFactoryName') ?>; - - -use \Twig_Environment; -use Mouf\Html\Renderer\Twig\TwigTemplate; - - -use Zend\Diactoros\Response\JsonResponse; - - -use Zend\Diactoros\Response\RedirectResponse; - - -use TheCodingMachine\Splash\HtmlResponse; - - -/** - * TODO: write controller comment - */ -class { - - - /** - * The logger used by this controller. - * @var LoggerInterface - */ - private $logger; - - - - /** - * The template used by this controller. - * @var TemplateInterface - */ - private $template; - - /** - * The main content block of the page. - * @var HtmlBlock - */ - private $content; - - - - /** - * The DAO factory object. - * @var DaoFactory - */ - private $daoFactory; - - - - /** - * The Twig environment (used to render Twig templates). - * @var Twig_Environment - */ - private $twig; - - - - /** - * Controller's constructor. - - */ - public function __construct() { - - $this->logger = $logger; - -$this->template = $template; -$this->content = $content; - -$this->daoFactory = $daoFactory; - -$this->twig = $twig; - - } - - - /** - * @URL("") - - - */ - public function () { - // TODO: write content of action here - - - // Let's add the twig file to the template. - $this->content->addHtmlElement(new TwigTemplate($this->twig, , array("message"=>"world"))); - - return new HtmlResponse($this->template); - - // Let's add the view to the content. - // Note: $this is passed as the scope, so in the view file, you can refer to protected - // and public variables and methods of this constructor using "$this". - $this->content->addFile(ROOT_PATH., $this); - - return new HtmlResponse($this->template); - - return new JsonResponse([ "status"=>"ok" ]); - - return new RedirectResponse(); - - } - -} - generateTwigView(); - - $twigFile = ltrim($action['twigFile'], '/\\'); - - file_put_contents(ROOT_PATH.'../../../'.$twigFile, $twigTemplateFile); - chmod(ROOT_PATH.'../../../'.$twigFile, 0664); - } elseif ($injectTemplate && $action['view'] == 'php') { - $phpTemplateFile = $this->generatePhpView($namespace.'\\'.$controllerName); - - $phpFile = ltrim($action['phpFile'], '/\\'); - - file_put_contents(ROOT_PATH.'../../../'.$phpFile, $phpTemplateFile); - chmod(ROOT_PATH.'../../../'.$phpFile, 0664); - } - } - - // Now, let's create the instance - $controllerInstance = $moufManager->createInstance($namespace.'\\'.$controllerName); - $controllerInstance->setName($instanceName); - if ($injectLogger) { - if ($moufManager->has('psr.errorLogLogger')) { - $controllerInstance->getProperty('logger')->setValue($moufManager->getInstanceDescriptor('psr.errorLogLogger')); - } - } - if ($injectTemplate) { - if ($moufManager->has('bootstrapTemplate')) { - $controllerInstance->getProperty('template')->setValue($moufManager->getInstanceDescriptor('bootstrapTemplate')); - } - if ($moufManager->has('block.content')) { - $controllerInstance->getProperty('content')->setValue($moufManager->getInstanceDescriptor('block.content')); - } - } - if ($injectDaoFactory) { - if ($moufManager->has('daoFactory')) { - $controllerInstance->getProperty('daoFactory')->setValue($moufManager->getInstanceDescriptor('daoFactory')); - } - } - if ($injectTwig) { - if ($moufManager->has('twigEnvironment')) { - $controllerInstance->getProperty('twig')->setValue($moufManager->getInstanceDescriptor('twigEnvironment')); - } - } - - $moufManager->rewriteMouf(); - - // There is a new class, let's purge the cache - $moufCache = new MoufCache(); - $moufCache->purgeAll(); - - // TODO: purge cache - } - } - - if ($errors) { - $exception = new SplashCreateControllerServiceException('Errors detected'); - $exception->setErrors($errors); - throw $exception; - } - } - - private function generateTwigView() - { - return '

Hello {{message}}

'; - } - - private function generatePhpView($controllerFQCN) - { - return ' -This is your PHP view. You can access the controller protected and public variables / functions using the $this object'; - } - - /** - * @param string $directory - * - * @return bool - */ - private function createDirectory($directory) - { - if (!file_exists($directory)) { - // Let's create the directory: - $old = umask(0); - $result = @mkdir($directory, 0775, true); - umask($old); - - return $result; - } - - return true; - } -} diff --git a/src/TheCodingMachine/Splash/Services/SplashCreateControllerServiceException.php b/src/TheCodingMachine/Splash/Services/SplashCreateControllerServiceException.php deleted file mode 100644 index 61221b0..0000000 --- a/src/TheCodingMachine/Splash/Services/SplashCreateControllerServiceException.php +++ /dev/null @@ -1,26 +0,0 @@ -errors; - } - - /** - * @param mixed $errors - */ - public function setErrors($errors) - { - $this->errors = $errors; - } -} diff --git a/src/TheCodingMachine/Splash/Utils/ExceptionUtils.php b/src/TheCodingMachine/Splash/Utils/ExceptionUtils.php deleted file mode 100644 index 6433d9a..0000000 --- a/src/TheCodingMachine/Splash/Utils/ExceptionUtils.php +++ /dev/null @@ -1,231 +0,0 @@ -'; - $str .= ((isset($step['class'])) ? htmlspecialchars($step['class'], ENT_NOQUOTES, 'UTF-8') : ''). - ((isset($step['type'])) ? htmlspecialchars($step['type'], ENT_NOQUOTES, 'UTF-8') : '').htmlspecialchars($step['function'], ENT_NOQUOTES, 'UTF-8').'('; - - if (is_array($step['args'])) { - $drawn = false; - $params = ''; - foreach ($step['args'] as $param) { - $params .= self::getPhpVariableAsText($param); - //$params .= var_export($param, true); - $params .= ', '; - $drawn = true; - } - $str .= htmlspecialchars($params, ENT_NOQUOTES, 'UTF-8'); - if ($drawn == true) { - $str = substr($str, 0, strlen($str) - 2); - } - } - $str .= ')'; - $str .= ''; - $str .= ((isset($step['file'])) ? htmlspecialchars(self::displayFile($step['file']), ENT_NOQUOTES, 'UTF-8') : ''); - $str .= ''; - $str .= ((isset($step['line'])) ? $step['line'] : ''); - $str .= ''; - } - } - - return $str; - } - - /** - * Function called to display an exception if it occurs. - * It will make sure to purge anything in the buffer before calling the exception displayer. - * - * @param Exception $exception - */ - public static function getHtmlForException(\Exception $exception) - { - //global $sys_error_reporting_mail; - //global $sys_error_messages; - $msg = ''; - - $msg = ''; - - $display_errors = ini_get('display_errors'); - $color = '#FF0000'; - $type = 'Uncaught '.get_class($exception); - if ($exception->getCode() != null) { - $type .= ' with error code '.$exception->getCode(); - } - - $msg .= ""; - - $msg .= ""; - $msg .= ""; - $msg .= ""; - - $msg .= "'; - $msg .= "'; - $msg .= "'; - $msg .= self::getHTMLBackTrace($exception->getTrace()); - $msg .= '
$type
Context/MessageFileLine
".nl2br($exception->getMessage()).'".self::displayFile($exception->getFile()).'".$exception->getLine().'
'; - - return $msg; - } - - /** - * Function called to display an exception if it occurs. - * It will make sure to purge anything in the buffer before calling the exception displayer. - * - * @param Exception $exception - */ - public static function getTextForException(\Exception $exception) - { - // Now, let's compute the same message, but without the HTML markup for the error log. - $textTrace = 'Message: '.$exception->getMessage()."\n"; - $textTrace .= 'File: '.$exception->getFile()."\n"; - $textTrace .= 'Line: '.$exception->getLine()."\n"; - $textTrace .= "Stacktrace:\n"; - $textTrace .= self::getTextBackTrace($exception->getTrace()); - - return $textTrace; - } - /** - * Returns the Exception Backtrace as a text string. - * - * @param array $backtrace - * - * @return unknown - */ - private static function getTextBackTrace(array $backtrace): string - { - $str = ''; - - foreach ($backtrace as $step) { - if ($step['function'] != 'getTextBackTrace' && $step['function'] != 'handle_error') { - if (isset($step['file']) && isset($step['line'])) { - $str .= 'In '.$step['file'].' at line '.$step['line'].': '; - } - if (isset($step['class']) && isset($step['type']) && isset($step['function'])) { - $str .= $step['class'].$step['type'].$step['function'].'('; - } - - if (is_array($step['args'])) { - $drawn = false; - $params = ''; - foreach ($step['args'] as $param) { - $params .= self::getPhpVariableAsText($param); - //$params .= var_export($param, true); - $params .= ', '; - $drawn = true; - } - $str .= $params; - if ($drawn == true) { - $str = substr($str, 0, strlen($str) - 2); - } - } - $str .= ')'; - $str .= "\n"; - } - } - - return $str; - } - - /** - * Used by the debug function to display a nice view of the parameters. - * - * @param mixed $var - * - * @return string - */ - private static function getPhpVariableAsText($var): string - { - if (is_string($var)) { - return('"'.str_replace(array("\x00", "\x0a", "\x0d", "\x1a", "\x09"), array('\0', '\n', '\r', '\Z', '\t'), $var).'"'); - } elseif (is_int($var) || is_float($var)) { - return($var); - } elseif (is_bool($var)) { - if ($var) { - return('true'); - } else { - return('false'); - } - } elseif (is_array($var)) { - $result = 'array( '; - $comma = ''; - foreach ($var as $key => $val) { - $result .= $comma.self::getPhpVariableAsText($key).' => '.self::getPhpVariableAsText($val); - $comma = ', '; - } - $result .= ' )'; - - return($result); - } elseif (is_object($var)) { - return 'Object '.get_class($var); - } elseif (is_resource($var)) { - return 'Resource '.get_resource_type($var); - } - - return 'Unknown type variable'; - } - - private static function displayFile($file) - { - $realpath = realpath($file); - if (!$realpath) { - // If the file is a phar::// or something... - - return $file; - } - $cwd = getcwd().DIRECTORY_SEPARATOR; - - return self::getRelativePath($cwd, $realpath); - } - - /** - * Returns a relative path based on 2 absolute paths. - * - * @param string $from - * @param string $to - * - * @return string - */ - private static function getRelativePath($from, $to) - { - $from = explode('/', $from); - $to = explode('/', $to); - $relPath = $to; - - foreach ($from as $depth => $dir) { - // find first non-matching dir - if (isset($to[$depth]) && $dir === $to[$depth]) { - // ignore this directory - array_shift($relPath); - } else { - // get number of remaining dirs to $from - $remaining = count($from) - $depth; - if ($remaining > 1) { - // add traversals up to first matching dir - $padLength = (count($relPath) + $remaining - 1) * -1; - $relPath = array_pad($relPath, $padLength, '..'); - break; - } else { - $relPath[0] = './'.$relPath[0]; - } - } - } - - return implode('/', $relPath); - } -} diff --git a/src/views/400.php b/src/views/400.php deleted file mode 100644 index 27e4eb7..0000000 --- a/src/views/400.php +++ /dev/null @@ -1,6 +0,0 @@ -

Bad request

- -
The server cannot process the request due to an apparent client error. Check the request syntax.
-
exception->getMessage(); -?>
diff --git a/src/views/500.php b/src/views/500.php deleted file mode 100644 index 7b905f2..0000000 --- a/src/views/500.php +++ /dev/null @@ -1,15 +0,0 @@ - -

An error occured

- -
An error occured in the application. Please try again, or contact an administrator.
-debugMode) { - echo '
'.nl2br($this->exception->getMessage()).'
'; - - echo '
'.ExceptionUtils::getHtmlForException($this->exception).'
'; -} ?> diff --git a/src/views/template.php b/src/views/template.php deleted file mode 100644 index a7da8c6..0000000 --- a/src/views/template.php +++ /dev/null @@ -1,24 +0,0 @@ - - - - <?php echo $this->getTitle(); ?> - - - - content->toHtml(); ?> - - \ No newline at end of file From d695c0fbcc947b55e763bd9cfffbb44d4bf06d16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 18 Jul 2018 16:03:17 +0200 Subject: [PATCH 2/5] Fixing PHPStan level 2 --- composer.json | 2 +- .../Splash/Routers/ControllerHandler.php | 2 +- src/TheCodingMachine/Splash/Routers/SplashRouter.php | 9 +-------- .../Splash/Services/ControllerAnalyzer.php | 7 +++---- .../Splash/Services/ControllerDetector.php | 2 -- .../Splash/Services/ControllerRegistry.php | 5 ++--- src/TheCodingMachine/Splash/Services/SplashRoute.php | 3 --- .../Splash/Services/SplashRouteInterface.php | 3 --- .../Splash/Services/UrlProviderInterface.php | 2 -- src/TheCodingMachine/Splash/UrlEntryPoint.php | 2 -- 10 files changed, 8 insertions(+), 29 deletions(-) diff --git a/composer.json b/composer.json index 6ca724d..95aa661 100644 --- a/composer.json +++ b/composer.json @@ -59,7 +59,7 @@ "scripts": { "cs-check": "phpcs", "cs-fix": "phpcbf", - "phpstan": "phpstan analyse src -c phpstan.neon --level=0 --no-progress -vvv" + "phpstan": "phpstan analyse src -c phpstan.neon --level=2 --no-progress -vvv" }, "minimum-stability": "dev", "prefer-stable": true, diff --git a/src/TheCodingMachine/Splash/Routers/ControllerHandler.php b/src/TheCodingMachine/Splash/Routers/ControllerHandler.php index c087ded..1128af7 100644 --- a/src/TheCodingMachine/Splash/Routers/ControllerHandler.php +++ b/src/TheCodingMachine/Splash/Routers/ControllerHandler.php @@ -22,7 +22,7 @@ class ControllerHandler implements RequestHandlerInterface */ private $splashRoute; /** - * @var + * @var object */ private $controller; /** diff --git a/src/TheCodingMachine/Splash/Routers/SplashRouter.php b/src/TheCodingMachine/Splash/Routers/SplashRouter.php index 1c06c6e..74b08ed 100644 --- a/src/TheCodingMachine/Splash/Routers/SplashRouter.php +++ b/src/TheCodingMachine/Splash/Routers/SplashRouter.php @@ -131,14 +131,7 @@ public function setHttp400Handler($http400Handler) return $this; } - /** - * @param ServerRequestInterface $request - * @param ResponseInterface $response - * @param callable|null $out - * - * @return ResponseInterface - */ - private function route(ServerRequestInterface $request, RequestHandlerInterface $next = null, $retry = false) : ResponseInterface + private function route(ServerRequestInterface $request, RequestHandlerInterface $next = null, bool $retry = false) : ResponseInterface { $this->purgeExpiredRoutes(); diff --git a/src/TheCodingMachine/Splash/Services/ControllerAnalyzer.php b/src/TheCodingMachine/Splash/Services/ControllerAnalyzer.php index ec2eae7..f2710d0 100644 --- a/src/TheCodingMachine/Splash/Services/ControllerAnalyzer.php +++ b/src/TheCodingMachine/Splash/Services/ControllerAnalyzer.php @@ -12,6 +12,7 @@ use TheCodingMachine\Splash\Annotations\Put; use TheCodingMachine\Splash\Annotations\Title; use ReflectionMethod; +use TheCodingMachine\Splash\Utils\SplashException; /** * This class is in charge of analyzing a controller instance and returning the routes it contains. @@ -72,11 +73,9 @@ public function isController(string $className) : bool /** * Returns an array of SplashRoute for the controller passed in parameter. * - * @param object $controller - * * @return SplashRoute[] * - * @throws \Mouf\Mvc\Splash\Utils\SplashException + * @throws SplashException */ public function analyzeController(string $controllerInstanceName) : array { @@ -90,9 +89,9 @@ public function analyzeController(string $controllerInstanceName) : array foreach ($refClass->getMethods() as $refMethod) { $title = null; // Now, let's check the "Title" annotation (note: we do not support multiple title annotations for the same method) + /** @var Title */ $titleAnnotation = $this->annotationReader->getMethodAnnotation($refMethod, Title::class); if ($titleAnnotation !== null) { - /* @var $titleAnnotation TitleAnnotation */ $title = $titleAnnotation->getTitle(); } diff --git a/src/TheCodingMachine/Splash/Services/ControllerDetector.php b/src/TheCodingMachine/Splash/Services/ControllerDetector.php index 165c51e..47ef32d 100644 --- a/src/TheCodingMachine/Splash/Services/ControllerDetector.php +++ b/src/TheCodingMachine/Splash/Services/ControllerDetector.php @@ -22,8 +22,6 @@ public function getControllerIdentifiers(ControllerAnalyzer $controllerAnalyzer) * If the tag changes, the cache is flushed by Splash. * * Important! This must be quick to compute. - * - * @return mixed */ public function getExpirationTag() : string; } diff --git a/src/TheCodingMachine/Splash/Services/ControllerRegistry.php b/src/TheCodingMachine/Splash/Services/ControllerRegistry.php index 3a3c2fe..8e01f4e 100644 --- a/src/TheCodingMachine/Splash/Services/ControllerRegistry.php +++ b/src/TheCodingMachine/Splash/Services/ControllerRegistry.php @@ -3,6 +3,7 @@ namespace TheCodingMachine\Splash\Services; use TheCodingMachine\Splash\Annotations\Action; +use TheCodingMachine\Splash\Utils\SplashException; /** * This class is in charge of registering controller's routes. @@ -58,7 +59,7 @@ public function addController(string $controller) : ControllerRegistry * * @return SplashRoute[] * - * @throws \Mouf\Mvc\Splash\Utils\SplashException + * @throws SplashException */ public function getUrlsList($instanceName) { @@ -88,8 +89,6 @@ public function getUrlsList($instanceName) * If the tag changes, the cache is flushed by Splash. * * Important! This must be quick to compute. - * - * @return mixed */ public function getExpirationTag() : string { diff --git a/src/TheCodingMachine/Splash/Services/SplashRoute.php b/src/TheCodingMachine/Splash/Services/SplashRoute.php index edac49d..135eb29 100644 --- a/src/TheCodingMachine/Splash/Services/SplashRoute.php +++ b/src/TheCodingMachine/Splash/Services/SplashRoute.php @@ -100,9 +100,6 @@ public function __construct(string $url, string $controllerInstanceName, string } } - /** - * @return mixed - */ public function getUrl() : string { return $this->url; diff --git a/src/TheCodingMachine/Splash/Services/SplashRouteInterface.php b/src/TheCodingMachine/Splash/Services/SplashRouteInterface.php index 0b54c4e..1243a8a 100644 --- a/src/TheCodingMachine/Splash/Services/SplashRouteInterface.php +++ b/src/TheCodingMachine/Splash/Services/SplashRouteInterface.php @@ -9,9 +9,6 @@ */ interface SplashRouteInterface { - /** - * @return mixed - */ public function getUrl() : string; /** diff --git a/src/TheCodingMachine/Splash/Services/UrlProviderInterface.php b/src/TheCodingMachine/Splash/Services/UrlProviderInterface.php index c3ea718..8be2676 100644 --- a/src/TheCodingMachine/Splash/Services/UrlProviderInterface.php +++ b/src/TheCodingMachine/Splash/Services/UrlProviderInterface.php @@ -23,8 +23,6 @@ public function getUrlsList($instanceName); * If the tag changes, the cache is flushed by Splash. * * Important! This must be quick to compute. - * - * @return mixed */ public function getExpirationTag() : string; } diff --git a/src/TheCodingMachine/Splash/UrlEntryPoint.php b/src/TheCodingMachine/Splash/UrlEntryPoint.php index 81bf246..506252b 100644 --- a/src/TheCodingMachine/Splash/UrlEntryPoint.php +++ b/src/TheCodingMachine/Splash/UrlEntryPoint.php @@ -66,8 +66,6 @@ public function getUrl() * If the tag changes, the cache is flushed by Splash. * * Important! This must be quick to compute. - * - * @return mixed */ public function getExpirationTag() : string { From c0444ed6b541547e7e149843be0076df4d3f452c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 18 Jul 2018 16:07:21 +0200 Subject: [PATCH 3/5] PHPStan level 3 --- composer.json | 2 +- src/TheCodingMachine/Splash/Routers/SplashRouter.php | 2 +- src/TheCodingMachine/Splash/Store/SplashUrlNode.php | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 95aa661..fc8b796 100644 --- a/composer.json +++ b/composer.json @@ -59,7 +59,7 @@ "scripts": { "cs-check": "phpcs", "cs-fix": "phpcbf", - "phpstan": "phpstan analyse src -c phpstan.neon --level=2 --no-progress -vvv" + "phpstan": "phpstan analyse src -c phpstan.neon --level=3 --no-progress -vvv" }, "minimum-stability": "dev", "prefer-stable": true, diff --git a/src/TheCodingMachine/Splash/Routers/SplashRouter.php b/src/TheCodingMachine/Splash/Routers/SplashRouter.php index 74b08ed..0571998 100644 --- a/src/TheCodingMachine/Splash/Routers/SplashRouter.php +++ b/src/TheCodingMachine/Splash/Routers/SplashRouter.php @@ -71,7 +71,7 @@ class SplashRouter implements MiddlewareInterface private $debug; /** - * @var ParameterFetcher[] + * @var ParameterFetcherRegistry */ private $parameterFetcherRegistry; diff --git a/src/TheCodingMachine/Splash/Store/SplashUrlNode.php b/src/TheCodingMachine/Splash/Store/SplashUrlNode.php index ffb565a..e2f0bce 100644 --- a/src/TheCodingMachine/Splash/Store/SplashUrlNode.php +++ b/src/TheCodingMachine/Splash/Store/SplashUrlNode.php @@ -33,14 +33,14 @@ class SplashUrlNode /** * A list of callbacks (assicated to there HTTP method). * - * @var array + * @var array */ private $callbacks = array(); /** * A list of callbacks (assicated to there HTTP method) finishing with "*". * - * @var array + * @var array */ private $wildcardCallbacks = array(); @@ -123,7 +123,7 @@ protected function addUrl(array $urlParts, SplashRouteInterface $callback) * @param string $url * @param ServerRequestInterface $request * - * @return SplashRoute + * @return SplashRouteInterface */ public function walk($url, ServerRequestInterface $request) { @@ -138,7 +138,7 @@ public function walk($url, ServerRequestInterface $request) * @param array $parameters * @param SplashRoute $closestWildcardRoute The last wildcard (*) route encountered while navigating the tree. * - * @return SplashRoute + * @return SplashRouteInterface * * @throws SplashException */ From f05271440d3557a24a35a841a7600074a54ea729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 18 Jul 2018 16:17:45 +0200 Subject: [PATCH 4/5] PHPStan level 5 --- composer.json | 2 +- src/TheCodingMachine/Splash/Routers/SplashRouter.php | 3 ++- src/TheCodingMachine/Splash/Services/ControllerRegistry.php | 2 +- src/TheCodingMachine/Splash/Services/UrlProviderInterface.php | 2 +- src/TheCodingMachine/Splash/Store/SplashUrlNode.php | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index fc8b796..8b1cb40 100644 --- a/composer.json +++ b/composer.json @@ -59,7 +59,7 @@ "scripts": { "cs-check": "phpcs", "cs-fix": "phpcbf", - "phpstan": "phpstan analyse src -c phpstan.neon --level=3 --no-progress -vvv" + "phpstan": "phpstan analyse src -c phpstan.neon --level=5 --no-progress -vvv" }, "minimum-stability": "dev", "prefer-stable": true, diff --git a/src/TheCodingMachine/Splash/Routers/SplashRouter.php b/src/TheCodingMachine/Splash/Routers/SplashRouter.php index 0571998..9541b34 100644 --- a/src/TheCodingMachine/Splash/Routers/SplashRouter.php +++ b/src/TheCodingMachine/Splash/Routers/SplashRouter.php @@ -11,6 +11,7 @@ use TheCodingMachine\Splash\Services\ParameterFetcher; use TheCodingMachine\Splash\Services\ParameterFetcherRegistry; use TheCodingMachine\Splash\Services\SplashRoute; +use TheCodingMachine\Splash\Services\SplashRouteInterface; use TheCodingMachine\Splash\Services\UrlProviderInterface; use TheCodingMachine\Splash\Utils\SplashException; use Psr\Cache\CacheItemPoolInterface; @@ -267,7 +268,7 @@ public function getSplashActionsList(): array * Generates the URLNodes from the list of URLS. * URLNodes are a very efficient way to know whether we can access our page or not. * - * @param UrlProviderInterface[] $urlsList + * @param SplashRouteInterface[] $urlsList * * @return SplashUrlNode */ diff --git a/src/TheCodingMachine/Splash/Services/ControllerRegistry.php b/src/TheCodingMachine/Splash/Services/ControllerRegistry.php index 8e01f4e..76c6b12 100644 --- a/src/TheCodingMachine/Splash/Services/ControllerRegistry.php +++ b/src/TheCodingMachine/Splash/Services/ControllerRegistry.php @@ -13,7 +13,7 @@ class ControllerRegistry implements UrlProviderInterface private $controllers; /** - * @var ControllerDetector + * @var ControllerDetector|null */ private $controllerDetector; diff --git a/src/TheCodingMachine/Splash/Services/UrlProviderInterface.php b/src/TheCodingMachine/Splash/Services/UrlProviderInterface.php index 8be2676..d065dd7 100644 --- a/src/TheCodingMachine/Splash/Services/UrlProviderInterface.php +++ b/src/TheCodingMachine/Splash/Services/UrlProviderInterface.php @@ -12,7 +12,7 @@ interface UrlProviderInterface /** * Returns the list of URLs that can be accessed, and the function/method that should be called when the URL is called. * - * @param string $instanceName The identifier for this object in the container. + * @param string|null $instanceName The identifier for this object in the container. * * @return SplashRoute[] */ diff --git a/src/TheCodingMachine/Splash/Store/SplashUrlNode.php b/src/TheCodingMachine/Splash/Store/SplashUrlNode.php index e2f0bce..a1ff727 100644 --- a/src/TheCodingMachine/Splash/Store/SplashUrlNode.php +++ b/src/TheCodingMachine/Splash/Store/SplashUrlNode.php @@ -136,7 +136,7 @@ public function walk($url, ServerRequestInterface $request) * @param array $urlParts * @param ServerRequestInterface $request * @param array $parameters - * @param SplashRoute $closestWildcardRoute The last wildcard (*) route encountered while navigating the tree. + * @param SplashRouteInterface $closestWildcardRoute The last wildcard (*) route encountered while navigating the tree. * * @return SplashRouteInterface * From 277e2732f70e21462a40eb39810cd778fb3545af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 18 Jul 2018 16:19:16 +0200 Subject: [PATCH 5/5] Enforcing PHPStan in CI --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index c152e9c..4a2c68e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,7 @@ before_script: - mkdir -p build/logs script: - composer cs-check + - composer phpstan - ./vendor/bin/phpunit after_script: