diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 7340766..b750635 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -7,8 +7,7 @@ on: jobs: ci: - uses: ray-di/.github/.github/workflows/continuous-integration.yml@next_stable + uses: ray-di/.github/.github/workflows/continuous-integration.yml@v1 with: - old_stable: '["7.4", "8.0"]' - current_stable: 8.1 - next_stable: 8.2 + old_stable: '["8.0", "8.1"]' + current_stable: 8.2 diff --git a/composer.json b/composer.json index d931d81..8afb2b5 100644 --- a/composer.json +++ b/composer.json @@ -14,19 +14,22 @@ } ], "require": { - "php": "^7.3 || ^8.0", + "php": "^8.0", "bear/resource": "^1.15.2", "bear/sunday": "^1.2", "bear/app-meta": "^1.1", - "twig/twig": "^2.14 || ^v3.3", + "twig/twig": "^2.15.3 || ^v3.4.3", "mobiledetect/mobiledetectlib": "^2.7", "ray/di": "^2.7", "doctrine/annotations": "^1.13.3" }, "require-dev": { "phpunit/phpunit": "^9.5.21", - "squizlabs/php_codesniffer": "^2.8", - "phpmd/phpmd": "^2.6" + "squizlabs/php_codesniffer": "^3.7", + "phpmd/phpmd": "^2.6", + "rector/rector": "^0.15.3", + "ray/rector-ray": "^1.0", + "doctrine/coding-standard": "^11.1" }, "autoload":{ "psr-4":{ @@ -48,5 +51,10 @@ "coverage": ["php -dzend_extension=xdebug.so -dxdebug.mode=coverage ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage"], "cs": ["php-cs-fixer fix -v --dry-run", "phpcs --standard=phpcs.xml src;"], "cs-fix": ["phpcbf src tests"] + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/phpcs.xml b/phpcs.xml index 88ecf9b..d71d60e 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,20 +1,90 @@ - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + src + tests + */tests/tmp/* + + + + + + + */src/*Interface.php + */src/Abstract*.php + */src/ResourceObject.php + + */src/*Interface.php + */src/Abstract*.php + */src/ResourceObject.php + + + + + + + + + + + + + + + + + + + + */tests/Fake/* + + + + + + + + + + + + + + + + + + + tests/* + + + + + + + + + + + + + + + */Fake/* + */tmp/* diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..7fc951c --- /dev/null +++ b/rector.php @@ -0,0 +1,43 @@ +paths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]); + + // register a single rule + $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); + $rectorConfig->rule(AnnotationBindingRector::class); + $rectorConfig->ruleWithConfiguration( + AnnotationToAttributeRector::class, + [ + new AnnotationToAttribute(TwigDebug::class), + new AnnotationToAttribute(TwigErrorPath::class), + new AnnotationToAttribute(TwigLoader::class), + new AnnotationToAttribute(TwigOptions::class), + new AnnotationToAttribute(TwigPaths::class), + new AnnotationToAttribute(TwigRedirectPath::class), + ] + ); + + // define sets of rules + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_80 + ]); +}; diff --git a/src/Annotation/TwigDebug.php b/src/Annotation/TwigDebug.php index fe44da5..933609b 100644 --- a/src/Annotation/TwigDebug.php +++ b/src/Annotation/TwigDebug.php @@ -1,14 +1,15 @@ value = $value; } } diff --git a/src/Annotation/TwigErrorPath.php b/src/Annotation/TwigErrorPath.php index a69c1b0..b06654f 100644 --- a/src/Annotation/TwigErrorPath.php +++ b/src/Annotation/TwigErrorPath.php @@ -1,14 +1,15 @@ value = $value; } } diff --git a/src/Annotation/TwigLoader.php b/src/Annotation/TwigLoader.php index 61d2c93..a2870bc 100644 --- a/src/Annotation/TwigLoader.php +++ b/src/Annotation/TwigLoader.php @@ -1,30 +1,28 @@ value = $value; } } diff --git a/src/Annotation/TwigOptions.php b/src/Annotation/TwigOptions.php index 1a18a35..08712e6 100644 --- a/src/Annotation/TwigOptions.php +++ b/src/Annotation/TwigOptions.php @@ -1,14 +1,15 @@ value = $value; } } diff --git a/src/Annotation/TwigPaths.php b/src/Annotation/TwigPaths.php index c6f644d..302145e 100644 --- a/src/Annotation/TwigPaths.php +++ b/src/Annotation/TwigPaths.php @@ -1,14 +1,15 @@ value = $value; } } diff --git a/src/Annotation/TwigRedirectPath.php b/src/Annotation/TwigRedirectPath.php index 4aaa4d4..7cfea4c 100644 --- a/src/Annotation/TwigRedirectPath.php +++ b/src/Annotation/TwigRedirectPath.php @@ -1,14 +1,15 @@ value = $value; } } diff --git a/src/AppPathProvider.php b/src/AppPathProvider.php index 64eb738..1266a06 100644 --- a/src/AppPathProvider.php +++ b/src/AppPathProvider.php @@ -1,9 +1,11 @@ appMeta = $appMeta; + public function __construct( + private AbstractAppMeta $appMeta, + ) { } /** * {@inheritdoc} */ - public function get() + public function get(): array { $appDir = $this->appMeta->appDir; - $paths = [ + + return [ $appDir . '/src/Resource', - $appDir . '/var/templates' + $appDir . '/var/templates', ]; - - return $paths; } } diff --git a/src/ErrorPagerRenderer.php b/src/ErrorPagerRenderer.php index a31509f..0873a8c 100644 --- a/src/ErrorPagerRenderer.php +++ b/src/ErrorPagerRenderer.php @@ -1,43 +1,36 @@ twig = $twig; - $this->errorPage = $errorPage; + public function __construct( + private Environment $twig, + #[TwigErrorPath] + private string $errorPage, + ) { } /** - * @throws \Twig\Error\LoaderError - * @throws \Twig\Error\RuntimeError - * @throws \Twig\Error\SyntaxError - * - * @return string + * @throws LoaderError + * @throws RuntimeError + * @throws SyntaxError */ - public function render(ResourceObject $ro) + public function render(ResourceObject $ro): string { $ro->view = $this->twig->render($this->errorPage, $ro->body); diff --git a/src/Exception/RuntimeException.php b/src/Exception/RuntimeException.php index 3ee789c..7c7a04d 100644 --- a/src/Exception/RuntimeException.php +++ b/src/Exception/RuntimeException.php @@ -1,9 +1,11 @@ userAgent = $userAgent; - $this->templateFinder = new TemplateFinder; - $this->paths = $paths; + public function __construct( + private mixed $userAgent = '', + #[TwigPaths] + private array $paths = [], + ) { + $this->templateFinder = new TemplateFinder(); } /** * {@inheritdoc} */ - public function __invoke(string $name) : string + public function __invoke(string $name): string { $templatePath = ($this->templateFinder)($name); - $detect = new \Mobile_Detect(null, $this->userAgent); + $detect = new Mobile_Detect(null, $this->userAgent); $isMobile = $detect->isMobile() && ! $detect->isTablet(); if ($isMobile) { - $mobilePath = \str_replace('.html.twig', '.mobile.twig', $templatePath); + $mobilePath = str_replace(TwigRenderer::EXT, '.mobile.twig', $templatePath); foreach ($this->paths as $path) { - $mobileFile = \sprintf('%s/%s', $path, $mobilePath); - if (\file_exists($mobileFile)) { + $mobileFile = sprintf('%s/%s', $path, $mobilePath); + if (file_exists($mobileFile)) { return $mobilePath; } } diff --git a/src/MobileTwigModule.php b/src/MobileTwigModule.php index 9899141..0764fe0 100644 --- a/src/MobileTwigModule.php +++ b/src/MobileTwigModule.php @@ -1,9 +1,11 @@ appMeta = $appMeta; - $this->isDebug = $isDebug; + /** @SuppressWarnings(PHPMD.BooleanArgumentFlag) */ + public function __construct( + private AbstractAppMeta $appMeta, + #[Named(TwigDebug::class)] + private bool $isDebug = false, + ) { } /** @@ -39,12 +32,11 @@ public function __construct(AbstractAppMeta $appMeta, bool $isDebug = false) public function get() { $tmpDir = $this->appMeta->tmpDir . '/twig'; - ! \file_exists($tmpDir) && \mkdir($tmpDir); - $options = [ + ! file_exists($tmpDir) && mkdir($tmpDir); + + return [ 'debug' => $this->isDebug, - 'cache' => $tmpDir + 'cache' => $tmpDir, ]; - - return $options; } } diff --git a/src/TemplateFinder.php b/src/TemplateFinder.php index 15e6dd7..02dab86 100644 --- a/src/TemplateFinder.php +++ b/src/TemplateFinder.php @@ -1,22 +1,27 @@ transfer = $transfer; - $this->errorPage = $errorPage; - $this->logger = $logger; + public function __construct( + private TwigErrorPage $errorPage, + private TransferInterface $transfer, + private LoggerInterface $logger, + ) { } /** * {@inheritdoc} */ - public function handle(\Exception $e, Request $request) + public function handle(Throwable $e, Request $request) { unset($request); $code = $this->getCode($e); $eStr = (string) $e; - $logRef = \crc32($eStr); + $logRef = crc32($eStr); if ($code >= 500) { - $this->logger->error(\sprintf('logref:%s %s', $logRef, $eStr)); + $this->logger->error(sprintf('logref:%s %s', $logRef, $eStr)); } + $this->errorPage->code = $code; $this->errorPage->body = [ 'status' => [ 'code' => $code, - 'message' => (new Code)->statusText[$code] + 'message' => (new Code())->statusText[$code], ], 'e' => [ 'code' => $e->getCode(), - 'class' => \get_class($e), - 'message' => $e->getMessage() + 'class' => $e::class, + 'message' => $e->getMessage(), ], - 'logref' => (string) $logRef + 'logref' => (string) $logRef, ]; return $this; @@ -74,7 +67,7 @@ public function transfer() ($this->transfer)($this->errorPage, []); } - private function getCode(\Exception $e) : int + private function getCode(Throwable $e): int { if ($e instanceof NotFound || $e instanceof BadRequest) { return $e->getCode(); diff --git a/src/TwigErrorPage.php b/src/TwigErrorPage.php index 5375ad5..b6216ea 100644 --- a/src/TwigErrorPage.php +++ b/src/TwigErrorPage.php @@ -1,9 +1,11 @@ 'text/html; charset=utf-8' - ]; + /** @var array */ + public $headers = ['content-type' => 'text/html; charset=utf-8']; protected $renderer; public function __sleep() @@ -30,8 +28,10 @@ public function __sleep() * @Inject * @Named("error_page") */ - #[Inject, Named('error_page')] - public function setRenderer(RenderInterface $renderer) + #[Inject] + + #[Named('error_page')] + public function setRenderer(RenderInterface $renderer): void { $this->renderer = $renderer; } diff --git a/src/TwigErrorPageHandler.php b/src/TwigErrorPageHandler.php index 9d8906d..8c847a5 100644 --- a/src/TwigErrorPageHandler.php +++ b/src/TwigErrorPageHandler.php @@ -1,9 +1,11 @@ transfer = $transfer; - $this->errorPage = $errorPage; - $this->logger = $logger; + public function __construct( + private TwigErrorPage $errorPage, + private TransferInterface $transfer, + private LoggerInterface $logger, + ) { } /** * {@inheritdoc} */ - public function handle(\Exception $e, Request $request) + public function handle(Throwable $e, Request $request) { unset($request); $code = $this->isCodeExists($e) ? $e->getCode() : 503; if ($code >= 500) { $eStr = (string) $e; - $this->logger->error(\sprintf('logref:%s %s', \crc32($eStr), $eStr)); + $this->logger->error(sprintf('logref:%s %s', crc32($eStr), $eStr)); } + $this->errorPage->code = $code; $this->errorPage->body = [ 'status' => [ 'code' => $code, - 'message' => (new Code)->statusText[$code] + 'message' => (new Code())->statusText[$code], ], 'e' => [ 'code' => $e->getCode(), - 'message' => $e->getMessage() - ] + 'message' => $e->getMessage(), + ], ]; return $this; @@ -72,12 +66,12 @@ public function transfer() ($this->transfer)($this->errorPage, []); } - private function isCodeExists(\Exception $e) : bool + private function isCodeExists(Throwable $e): bool { if (! ($e instanceof NotFound) && ! ($e instanceof BadRequest) && ! ($e instanceof ServerError)) { return false; } - return \array_key_exists($e->getCode(), (new Code)->statusText); + return array_key_exists($e->getCode(), (new Code())->statusText); } } diff --git a/src/TwigErrorPageModule.php b/src/TwigErrorPageModule.php index 45f495b..456217e 100644 --- a/src/TwigErrorPageModule.php +++ b/src/TwigErrorPageModule.php @@ -1,9 +1,11 @@ paths = $paths; - $this->options = $options; + public function __construct( + private array $paths = [], + private array $options = [], + AbstractModule|null $module = null, + ) { parent::__construct($module); } @@ -56,25 +51,25 @@ protected function configure() $this->bindTwigRedirectPath(); } - private function bindRender() + private function bindRender(): void { $this->bind(RenderInterface::class) ->to(TwigRenderer::class) ->in(Scope::SINGLETON); } - private function bindTwigLoader() + private function bindTwigLoader(): void { $this ->bind(LoaderInterface::class) ->annotatedWith(TwigLoader::class) ->toConstructor( FilesystemLoader::class, - 'paths=Madapaja\TwigModule\Annotation\TwigPaths' + 'paths=Madapaja\TwigModule\Annotation\TwigPaths', ); } - private function bindTwigEnvironment() + private function bindTwigEnvironment(): void { $this ->bind(Environment::class) @@ -83,8 +78,8 @@ private function bindTwigEnvironment() Environment::class, [ 'loader' => TwigLoader::class, - 'options' => TwigOptions::class - ] + 'options' => TwigOptions::class, + ], ); $this @@ -93,38 +88,40 @@ private function bindTwigEnvironment() Environment::class, [ 'loader' => TwigLoader::class, - 'options' => TwigOptions::class - ] + 'options' => TwigOptions::class, + ], ); } - private function bindTwigPaths() + private function bindTwigPaths(): void { if ($this->isNotEmpty($this->paths)) { $this->bind()->annotatedWith(TwigPaths::class)->toInstance($this->paths); return; } + $this->bind()->annotatedWith(TwigPaths::class)->toProvider(AppPathProvider::class); } - private function bindTwigOptions() + private function bindTwigOptions(): void { if ($this->isNotEmpty($this->options)) { $this->bind()->annotatedWith(TwigOptions::class)->toInstance($this->options); return; } + $this->bind()->annotatedWith(TwigOptions::class)->toProvider(OptionProvider::class); } - private function bindTwigRedirectPath() + private function bindTwigRedirectPath(): void { $this->bind()->annotatedWith(TwigRedirectPath::class)->toInstance('/redirect/redirect.html.twig'); } private function isNotEmpty($var) { - return \is_array($var) && ! empty($var); + return is_array($var) && ! empty($var); } } diff --git a/src/TwigRenderer.php b/src/TwigRenderer.php index 0e520bd..b3ac1cd 100644 --- a/src/TwigRenderer.php +++ b/src/TwigRenderer.php @@ -1,9 +1,11 @@ twig = $twig; - $this->redirectPage = $redirectPage; - $this->templateFinder = $templateFinder ?: new TemplateFinder; + $this->templateFinder = $templateFinder ?: new TemplateFinder(); } /** @@ -63,21 +55,25 @@ public function render(ResourceObject $ro) return $ro->view; } + if ($this->isRedirect($ro)) { $ro->view = $this->renderRedirectView($ro); return $ro->view; } + $ro->view = $this->renderView($ro); return $ro->view; } - private function setContentType(ResourceObject $ro) + private function setContentType(ResourceObject $ro): void { - if (! isset($ro->headers['Content-Type'])) { - $ro->headers['Content-Type'] = 'text/html; charset=utf-8'; + if (isset($ro->headers['Content-Type'])) { + return; } + + $ro->headers['Content-Type'] = 'text/html; charset=utf-8'; } private function renderView(ResourceObject $ro) @@ -91,15 +87,12 @@ private function renderRedirectView(ResourceObject $ro) { try { return $this->twig->render($this->redirectPage, ['url' => $ro->headers['Location']]); - } catch (LoaderError $e) { + } catch (LoaderError) { return ''; } } - /** - * @return null|\Twig\TemplateWrapper - */ - private function load(ResourceObject $ro) + private function load(ResourceObject $ro): TemplateWrapper|null { try { return $this->loadTemplate($ro); @@ -108,16 +101,18 @@ private function load(ResourceObject $ro) throw new Exception\TemplateNotFound($e->getMessage(), 500, $e); } } + + return null; } - private function isNoContent(ResourceObject $ro) : bool + private function isNoContent(ResourceObject $ro): bool { return $ro->code === Code::NO_CONTENT || $ro->view === ''; } - private function isRedirect(ResourceObject $ro) : bool + private function isRedirect(ResourceObject $ro): bool { - return \in_array($ro->code, [ + return in_array($ro->code, [ Code::MOVED_PERMANENTLY, Code::FOUND, Code::SEE_OTHER, @@ -126,7 +121,7 @@ private function isRedirect(ResourceObject $ro) : bool ], true) && isset($ro->headers['Location']); } - private function loadTemplate(ResourceObject $ro) : TemplateWrapper + private function loadTemplate(ResourceObject $ro): TemplateWrapper { $loader = $this->twig->getLoader(); if ($loader instanceof FilesystemLoader) { @@ -139,18 +134,18 @@ private function loadTemplate(ResourceObject $ro) : TemplateWrapper return $this->twig->load($this->getReflection($ro)->name . self::EXT); } - private function getReflection(ResourceObject $ro) : \ReflectionClass + private function getReflection(ResourceObject $ro): ReflectionClass { if ($ro instanceof WeavedInterface) { - return (new \ReflectionClass($ro))->getParentClass(); + return (new ReflectionClass($ro))->getParentClass(); } - return new \ReflectionClass($ro); + return new ReflectionClass($ro); } - private function buildBody(ResourceObject $ro) : array + private function buildBody(ResourceObject $ro): array { - $body = \is_array($ro->body) ? $ro->body : []; + $body = is_array($ro->body) ? $ro->body : []; $body += ['_ro' => $ro]; return $body; diff --git a/tests/AppMetaTest.php b/tests/AppMetaTest.php index bb62c36..b686f66 100644 --- a/tests/AppMetaTest.php +++ b/tests/AppMetaTest.php @@ -1,9 +1,11 @@ getInstance(RenderInterface::class); diff --git a/tests/AppPathProviderTest.php b/tests/AppPathProviderTest.php index bb46814..6f49bb7 100644 --- a/tests/AppPathProviderTest.php +++ b/tests/AppPathProviderTest.php @@ -1,18 +1,23 @@ getInstance(TwigRenderer::class); + $renderer = (new Injector(new AppPathProviderTestModule()))->getInstance(TwigRenderer::class); /** @var FilesystemLoader $loader */ $loader = $renderer->twig->getLoader(); $this->assertInstanceOf(FilesystemLoader::class, $loader); diff --git a/tests/AppPathProviderTestModule.php b/tests/AppPathProviderTestModule.php index dbe9e98..79e8cf6 100644 --- a/tests/AppPathProviderTestModule.php +++ b/tests/AppPathProviderTestModule.php @@ -1,9 +1,11 @@ dir = $dir; - parent::__construct(null); - } - /** * {@inheritdoc} */ diff --git a/tests/ArrayLoaderTest.php b/tests/ArrayLoaderTest.php index 5d77ef8..f9a85d6 100644 --- a/tests/ArrayLoaderTest.php +++ b/tests/ArrayLoaderTest.php @@ -1,9 +1,11 @@ injector = new Injector(new TwigArrayLoaderTestModule); + $this->injector = new Injector(new TwigArrayLoaderTestModule()); } - public function testRenderer() + public function testRenderer(): void { $ro = $this->injector->getInstance(Index::class); - $prop = (new \ReflectionClass($ro))->getProperty('renderer'); + $prop = (new ReflectionClass($ro))->getProperty('renderer'); $prop->setAccessible(true); $this->assertInstanceOf(TwigRenderer::class, $prop->getValue($ro)); } - public function testIndex() + public function testIndex(): void { $ro = $this->injector->getInstance(Index::class); $this->assertSame('Hello, BEAR.Sunday!', (string) $ro->onGet()); } - public function testIndexWithArg() + public function testIndexWithArg(): void { $ro = $this->injector->getInstance(Index::class); $this->assertSame('Hello, Madapaja!', (string) $ro->onGet('Madapaja')); } - public function testTemplateNotFoundException() + public function testTemplateNotFoundException(): void { $this->expectException(TemplateNotFound::class); $ro = $this->injector->getInstance(NoTemplate::class); - $prop = (new \ReflectionClass($ro))->getProperty('renderer'); + $prop = (new ReflectionClass($ro))->getProperty('renderer'); $prop->setAccessible(true); $prop->getValue($ro)->render($ro); } - public function testPage() + public function testPage(): void { $ro = $this->injector->getInstance(Page::class); diff --git a/tests/ExtensionTest.php b/tests/ExtensionTest.php index 81bd761..c7188c0 100644 --- a/tests/ExtensionTest.php +++ b/tests/ExtensionTest.php @@ -1,30 +1,32 @@ injector = new Injector(new TwigExtensionTestModule([$_ENV['TEST_DIR'] . '/Fake/src/Resource/'])); } - public function testTwigFilter() + public function testTwigFilter(): void { $ro = $this->injector->getInstance(TwigFilter::class); - $this->assertSame('Twig => "' . \str_rot13('Twig') . '"', (string) \trim($ro->onGet())); + $this->assertSame('Twig => "' . str_rot13('Twig') . '"', (string) trim((string) $ro->onGet())); } } diff --git a/tests/Fake/src/ExtendedTwigEnvironmentProvider.php b/tests/Fake/src/ExtendedTwigEnvironmentProvider.php index 9872648..86ea87c 100644 --- a/tests/Fake/src/ExtendedTwigEnvironmentProvider.php +++ b/tests/Fake/src/ExtendedTwigEnvironmentProvider.php @@ -12,17 +12,12 @@ class ExtendedTwigEnvironmentProvider implements ProviderInterface { - private $twig; - /** * @Named("original") - * @param Environment $twig */ #[Named('original')] - public function __construct(Environment $twig) + public function __construct(private Environment $twig) { - // $twig is an original Twig\Environment instance - $this->twig = $twig; } public function get() diff --git a/tests/Fake/src/MyTwigExtension.php b/tests/Fake/src/MyTwigExtension.php index 4966fa9..69ab3e0 100644 --- a/tests/Fake/src/MyTwigExtension.php +++ b/tests/Fake/src/MyTwigExtension.php @@ -14,9 +14,7 @@ class MyTwigExtension extends AbstractExtension public function getFilters() { return [ - new TwigFilter('rot13', function ($string) { - return str_rot13($string); - }), + new TwigFilter('rot13', fn($string) => str_rot13($string)), ]; } diff --git a/tests/Fake/src/OptionProviderTestModule.php b/tests/Fake/src/OptionProviderTestModule.php index cd1f085..c11e846 100644 --- a/tests/Fake/src/OptionProviderTestModule.php +++ b/tests/Fake/src/OptionProviderTestModule.php @@ -14,17 +14,12 @@ class OptionProviderTestModule extends AbstractModule { - private $tmpDir; - private $isDebug; - /** * @param string $tmpDir * @param bool $isDebug */ - public function __construct($tmpDir, $isDebug) + public function __construct(private $tmpDir, private $isDebug) { - $this->tmpDir = $tmpDir; - $this->isDebug = $isDebug; parent::__construct(null); } diff --git a/tests/Fake/src/TwigExtensionTestModule.php b/tests/Fake/src/TwigExtensionTestModule.php index 8ce8c21..2bd3cfd 100644 --- a/tests/Fake/src/TwigExtensionTestModule.php +++ b/tests/Fake/src/TwigExtensionTestModule.php @@ -12,11 +12,8 @@ class TwigExtensionTestModule extends AbstractModule { - private $paths; - - public function __construct(array $paths = []) + public function __construct(private array $paths = []) { - $this->paths = $paths; parent::__construct(); } diff --git a/tests/Fake/src/TwigFileLoaderTestModule.php b/tests/Fake/src/TwigFileLoaderTestModule.php index d64c448..60f5c5f 100644 --- a/tests/Fake/src/TwigFileLoaderTestModule.php +++ b/tests/Fake/src/TwigFileLoaderTestModule.php @@ -10,14 +10,8 @@ class TwigFileLoaderTestModule extends AbstractModule { - private $paths; - private $options; - - public function __construct(array $paths = [], array $options = []) + public function __construct(private array $paths = [], private array $options = []) { - $this->paths = $paths; - $this->options = $options; - parent::__construct(); } diff --git a/tests/Fake/src/TwigWeavedResourceTestModule.php b/tests/Fake/src/TwigWeavedResourceTestModule.php index e28bbbd..c04eebf 100644 --- a/tests/Fake/src/TwigWeavedResourceTestModule.php +++ b/tests/Fake/src/TwigWeavedResourceTestModule.php @@ -11,11 +11,8 @@ class TwigWeavedResourceTestModule extends AbstractModule { - private $paths; - - public function __construct(array $paths = []) + public function __construct(private array $paths = []) { - $this->paths = $paths; parent::__construct(); } diff --git a/tests/FileLoaderTest.php b/tests/FileLoaderTest.php index f7ee78f..981d7e5 100644 --- a/tests/FileLoaderTest.php +++ b/tests/FileLoaderTest.php @@ -1,9 +1,11 @@ getInjector(); $ro = $injector->getInstance(Index::class); - $prop = (new \ReflectionClass($ro))->getProperty('renderer'); + $prop = (new ReflectionClass($ro))->getProperty('renderer'); $prop->setAccessible(true); $this->assertInstanceOf(TwigRenderer::class, $prop->getValue($ro)); } - public function testTwigOptions() + public function testTwigOptions(): void { /** @var TwigRenderer $renderer */ $renderer = (new Injector(new TwigFileLoaderTestModule([$_ENV['TEST_DIR'] . '/Fake/src/Resource'], ['debug' => true])))->getInstance(TwigRenderer::class); @@ -43,63 +48,63 @@ public function testTwigOptions() $this->assertFalse($renderer->twig->isDebug()); } - public function testIndex() + public function testIndex(): void { $injector = $this->getInjector(); $ro = $injector->getInstance(Index::class); - $this->assertSame('Hello, BEAR.Sunday!', \trim((string) $ro->onGet())); + $this->assertSame('Hello, BEAR.Sunday!', trim((string) $ro->onGet())); } - public function testTemplatePath() + public function testTemplatePath(): void { $injector = $this->getInjector(); $ro = $injector->getInstance(Index::class); - $this->assertSame('Your Name is MyName!', \trim((string) $ro->onPost('MyName'))); + $this->assertSame('Your Name is MyName!', trim((string) $ro->onPost('MyName'))); } - public function testIndexWithArg() + public function testIndexWithArg(): void { $injector = $this->getInjector(); $ro = $injector->getInstance(Index::class); - $this->assertSame('Hello, Madapaja!', \trim((string) $ro->onGet('Madapaja'))); + $this->assertSame('Hello, Madapaja!', trim((string) $ro->onGet('Madapaja'))); } - public function testTemplateNotFoundException() + public function testTemplateNotFoundException(): void { $this->expectException(TemplateNotFound::class); $injector = $this->getInjector(); $ro = $injector->getInstance(NoTemplate::class); - $prop = (new \ReflectionClass($ro))->getProperty('renderer'); + $prop = (new ReflectionClass($ro))->getProperty('renderer'); $prop->setAccessible(true); $prop->getValue($ro)->render($ro); } - public function testNoViewWhenCode301() + public function testNoViewWhenCode301(): void { $injector = $this->getInjector(); $ro = $injector->getInstance(NoTemplate::class); $ro->code = 303; - $prop = (new \ReflectionClass($ro))->getProperty('renderer'); + $prop = (new ReflectionClass($ro))->getProperty('renderer'); $prop->setAccessible(true); $view = $prop->getValue($ro)->render($ro); $this->assertSame('', $view); } - public function testNoContent() + public function testNoContent(): void { $injector = $this->getInjector(); $ro = $injector->getInstance(NoTemplate::class); $ro->code = Code::NO_CONTENT; - $prop = (new \ReflectionClass($ro))->getProperty('renderer'); + $prop = (new ReflectionClass($ro))->getProperty('renderer'); $prop->setAccessible(true); $view = $prop->getValue($ro)->render($ro); $this->assertSame('', $view); } - public function testPage() + public function testPage(): void { $injector = $this->getInjector(); $ro = $injector->getInstance(Page::class); @@ -107,7 +112,7 @@ public function testPage() $this->assertSame('PageHello, BEAR.Sunday!', (string) $ro->onGet()); } - public function testCode() + public function testCode(): void { $injector = $this->getInjector(); $ro = $injector->getInstance(\Madapaja\TwigModule\Resource\Page\Code::class); @@ -115,25 +120,25 @@ public function testCode() $this->assertSame('code:200 date:Tue, 15 Nov 1994 12:45:26 GMT', (string) $ro->onGet()); } - public function testIndexTemplateWithoutPaths() + public function testIndexTemplateWithoutPaths(): void { - $injector = new Injector(new TwigAppMetaTestModule); + $injector = new Injector(new TwigAppMetaTestModule()); $ro = $injector->getInstance(Index::class); $ro->onGet(); - $this->assertSame('Hello, BEAR.Sunday!', \trim((string) $ro)); + $this->assertSame('Hello, BEAR.Sunday!', trim((string) $ro)); } - public function testNoRedirectOnGet() + public function testNoRedirectOnGet(): void { $injector = $this->getInjector(); $ro = $injector->getInstance(Redirect::class); $ro->onGet(); $this->assertSame(Code::OK, $ro->code); - $this->assertSame('foo is bar.', \trim((string) $ro)); + $this->assertSame('foo is bar.', trim((string) $ro)); } - public function testRedirectOnPost() + public function testRedirectOnPost(): void { $injector = $this->getInjector(); $ro = $injector->getInstance(Redirect::class); @@ -141,6 +146,6 @@ public function testRedirectOnPost() $this->assertSame(Code::FOUND, $ro->code); $this->assertSame('/path/to/baz', $ro->headers['Location']); - $this->assertMatchesRegularExpression('#^.*Redirecting to /path/to/baz.*$#s', \trim((string) $ro)); + $this->assertMatchesRegularExpression('#^.*Redirecting to /path/to/baz.*$#s', trim((string) $ro)); } } diff --git a/tests/MobileTemplateFinderTest.php b/tests/MobileTemplateFinderTest.php index f0a38c5..ff4a641 100644 --- a/tests/MobileTemplateFinderTest.php +++ b/tests/MobileTemplateFinderTest.php @@ -1,9 +1,11 @@ injector = new Injector(new MobileTwigModule()); } - public function testMobileTemplate() + public function testMobileTemplate(): void { $iphone = 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A523 Safari/8536.25'; $paths = [$_ENV['TEST_DIR'] . '/Fake/src/Resource']; @@ -31,7 +30,7 @@ public function testMobileTemplate() $this->assertSame($expected, $file); } - public function testPcTemplate() + public function testPcTemplate(): void { $pc = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)'; $paths = [$_ENV['TEST_DIR'] . '/Fake/src/Resource']; @@ -41,7 +40,7 @@ public function testPcTemplate() $this->assertSame($expected, $file); } - public function testMobileTemplateFinder() + public function testMobileTemplateFinder(): void { $templateFinder = $this->injector->getInstance(TemplateFinderInterface::class); $this->assertInstanceOf(MobileTemplateFinder::class, $templateFinder); diff --git a/tests/OptionProviderTest.php b/tests/OptionProviderTest.php index 0e7920e..b15f21f 100644 --- a/tests/OptionProviderTest.php +++ b/tests/OptionProviderTest.php @@ -1,14 +1,19 @@ tmpDir = __DIR__ . '/tmp/optionProvider/tmp'; - if (! \is_dir($this->tmpDir)) { - \mkdir($this->tmpDir, 0777, true); + if (is_dir($this->tmpDir)) { + return; } + + mkdir($this->tmpDir, 0777, true); } - public function testOptionProvider() + public function testOptionProvider(): void { /** @var TwigRenderer $renderer */ $renderer = (new Injector(new OptionProviderTestModule($this->tmpDir, true)))->getInstance(TwigRenderer::class); @@ -30,7 +37,7 @@ public function testOptionProvider() $this->assertTrue($renderer->twig->isDebug()); } - public function testOptionProviderDebugFalse() + public function testOptionProviderDebugFalse(): void { /** @var TwigRenderer $renderer */ $renderer = (new Injector(new OptionProviderTestModule($this->tmpDir, false)))->getInstance(TwigRenderer::class); diff --git a/tests/TwigErrorPageHandlerTest.php b/tests/TwigErrorPageHandlerTest.php index 7bf85c6..1f431b7 100644 --- a/tests/TwigErrorPageHandlerTest.php +++ b/tests/TwigErrorPageHandlerTest.php @@ -1,9 +1,11 @@ setRenderer(new ErrorPagerRenderer($twig, '/error/error.html.twig')); $this->handler = new TwigErrorHandler( $errorPage, new FakeHttpResponder(), - new NullLogger + new NullLogger(), ); } public function testHandle() { - $request = new RouterMatch; + $request = new RouterMatch(); $request->method = 'get'; $request->path = '/'; $request->query = []; - $handler = $this->handler->handle(new NotFound, $request); + $handler = $this->handler->handle(new NotFound(), $request); $this->assertInstanceOf(TwigErrorHandler::class, $handler); return $handler; } - /** - * @depends testHandle - */ - public function testTransfer() + /** @depends testHandle */ + public function testTransfer(): void { - $request = new RouterMatch; + $request = new RouterMatch(); $request->method = 'get'; $request->path = '/'; $request->query = []; - $handler = $this->handler->handle(new ServerError, $request); + $handler = $this->handler->handle(new ServerError(), $request); $handler->transfer(); $this->assertSame(503, FakeHttpResponder::$code); $this->assertSame('text/html; charset=utf-8', FakeHttpResponder::$headers['content-type']); diff --git a/tests/WeavedResourceTest.php b/tests/WeavedResourceTest.php index 9cde9a9..9ec1194 100644 --- a/tests/WeavedResourceTest.php +++ b/tests/WeavedResourceTest.php @@ -1,40 +1,42 @@ injector = new Injector(new TwigWeavedResourceTestModule([$_ENV['TEST_DIR'] . '/Fake/src/Resource'])); } - public function testRenderer() + public function testRenderer(): void { $ro = $this->injector->getInstance(Index::class); - $prop = (new \ReflectionClass($ro))->getProperty('renderer'); + $prop = (new ReflectionClass($ro))->getProperty('renderer'); $prop->setAccessible(true); $this->assertInstanceOf(TwigRenderer::class, $prop->getValue($ro)); } - public function testIndex() + public function testIndex(): void { $ro = $this->injector->getInstance(Index::class); - $this->assertSame('Hello, BEAR.Sunday!', \trim((string) $ro->onGet())); + $this->assertSame('Hello, BEAR.Sunday!', trim((string) $ro->onGet())); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index cb9295c..200a0e2 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,22 +1,22 @@ addPsr4('Madapaja\TwigModule\\', [__DIR__]); -AnnotationRegistry::registerLoader([$loader, 'loadClass']); $_ENV['TEST_DIR'] = __DIR__; $_ENV['TMP_DIR'] = __DIR__ . '/tmp'; // no annotation in PHP 8