From e10f6cefef572d9c99d61a27b12db5d73dd07091 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 16 Aug 2020 16:06:55 +0200 Subject: [PATCH 01/10] tests: test() with description --- tests/Application/Application.run.phpt | 12 +++------ tests/Application/MicroPresenter.invoke.phpt | 4 +-- .../Application/MicroPresenter.response.phpt | 14 +++++----- ...PresenterFactory.formatPresenterClass.phpt | 8 +++--- ...esenterFactory.unformatPresenterClass.phpt | 4 +-- tests/Application/Request.phpt | 4 +-- .../ApplicationExtension.basic.phpt | 2 +- .../ApplicationExtension.invalidLink.phpt | 8 +++--- .../Bridges.DI/ApplicationExtension.scan.phpt | 8 +++--- tests/Bridges.DI/RoutingExtension.basic.phpt | 4 +-- tests/Bridges.DI/RoutingExtension.cache.phpt | 5 ++-- .../TemplateFactory.customTemplate.phpt | 4 +-- .../TemplateFactory.onCompile.phpt | 6 ++--- .../TemplateFactory.onCreate.phpt | 2 +- tests/Responses/CallbackResponse.phpt | 2 +- .../FileResponse.contentDisposition.phpt | 6 ++--- tests/Responses/FileResponse.full.phpt | 4 +-- tests/Responses/FileResponse.range.phpt | 16 +++++------ tests/Responses/JsonResponse.contentType.phpt | 4 +-- tests/Routers/LinkGenerator.phpt | 6 ++--- tests/Routers/Route.scalarParams.phpt | 12 ++++----- tests/Routers/SimpleRouter.invalid.phpt | 4 +-- tests/UI/Component.redirect().phpt | 14 +++++----- tests/UI/ComponentReflection.combineArgs.phpt | 14 +++++----- tests/UI/Control.isControlInvalid.phpt | 4 +-- tests/UI/Form.phpt | 4 +-- .../Presenter.formatLayoutTemplateFiles.phpt | 10 +++---- tests/UI/Presenter.formatTemplateFiles.phpt | 8 +++--- tests/UI/Presenter.isModuleCurrent().phpt | 4 +-- tests/UI/Presenter.parameters.phpt | 27 +++++++------------ tests/UI/Presenter.storeRequest().phpt | 4 +-- tests/bootstrap.php | 2 +- 32 files changed, 108 insertions(+), 122 deletions(-) diff --git a/tests/Application/Application.run.phpt b/tests/Application/Application.run.phpt index af205ddba..308b391ab 100644 --- a/tests/Application/Application.run.phpt +++ b/tests/Application/Application.run.phpt @@ -106,8 +106,7 @@ Assert::exception(function () use ($httpRequest, $httpResponse) { }, BadRequestException::class, 'No route for HTTP request.'); -// no route with error presenter -test(function () use ($httpRequest, $httpResponse) { +test('no route with error presenter', function () use ($httpRequest, $httpResponse) { $errorPresenter = new ErrorPresenter; $presenterFactory = Mockery::mock(IPresenterFactory::class); @@ -132,8 +131,7 @@ test(function () use ($httpRequest, $httpResponse) { }); -// route to error presenter -test(function () use ($httpRequest, $httpResponse) { +test('route to error presenter', function () use ($httpRequest, $httpResponse) { $errorPresenter = new ErrorPresenter; $presenterFactory = Mockery::mock(IPresenterFactory::class); @@ -173,8 +171,7 @@ Assert::exception(function () use ($httpRequest, $httpResponse) { }, BadRequestException::class); -// missing presenter with error presenter -test(function () use ($httpRequest, $httpResponse) { +test('missing presenter with error presenter', function () use ($httpRequest, $httpResponse) { $errorPresenter = new ErrorPresenter; $presenterFactory = Mockery::mock(IPresenterFactory::class); @@ -215,8 +212,7 @@ Assert::exception(function () use ($httpRequest, $httpResponse) { }, BadException::class); -// presenter error with error presenter -test(function () use ($httpRequest, $httpResponse) { +test('presenter error with error presenter', function () use ($httpRequest, $httpResponse) { $errorPresenter = new ErrorPresenter; $presenterFactory = Mockery::mock(IPresenterFactory::class); diff --git a/tests/Application/MicroPresenter.invoke.phpt b/tests/Application/MicroPresenter.invoke.phpt index ea13ff0e1..7266a4e51 100644 --- a/tests/Application/MicroPresenter.invoke.phpt +++ b/tests/Application/MicroPresenter.invoke.phpt @@ -13,7 +13,7 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; -test(function () { +test('', function () { $presenter = $p = new NetteModule\MicroPresenter; $presenter->run(new Request('Nette:Micro', 'GET', [ @@ -30,7 +30,7 @@ test(function () { }); -test(function () { +test('', function () { $container = Mockery::mock(Nette\DI\Container::class) ->shouldReceive('getByType')->with('stdClass', false)->once()->andReturn(new stdClass) ->mock(); diff --git a/tests/Application/MicroPresenter.response.phpt b/tests/Application/MicroPresenter.response.phpt index 90b4ab524..e43222f7e 100644 --- a/tests/Application/MicroPresenter.response.phpt +++ b/tests/Application/MicroPresenter.response.phpt @@ -37,7 +37,7 @@ function createContainer() } -test(function () { +test('', function () { $presenter = new NetteModule\MicroPresenter(createContainer()); $response = $presenter->run(new Request('Nette:Micro', 'GET', [ 'callback' => function () { @@ -50,7 +50,7 @@ test(function () { }); -test(function () { +test('', function () { $presenter = new NetteModule\MicroPresenter(createContainer()); $response = $presenter->run(new Request('Nette:Micro', 'GET', [ 'callback' => function ($param) { @@ -64,7 +64,7 @@ test(function () { }); -test(function () { +test('', function () { $presenter = new NetteModule\MicroPresenter(createContainer()); $response = $presenter->run(new Request('Nette:Micro', 'GET', [ 'callback' => function () { @@ -77,7 +77,7 @@ test(function () { }); -test(function () { +test('', function () { $presenter = new NetteModule\MicroPresenter(createContainer()); $response = $presenter->run(new Request('Nette:Micro', 'GET', [ 'callback' => function () { @@ -91,7 +91,7 @@ test(function () { }); -test(function () { +test('', function () { $presenter = new NetteModule\MicroPresenter; $response = $presenter->run(new Request('Nette:Micro', 'GET', [ @@ -111,7 +111,7 @@ test(function () { }); -test(function () { +test('', function () { $presenter = new NetteModule\MicroPresenter; $response = $presenter->run(new Request('Nette:Micro', 'GET', [ @@ -132,7 +132,7 @@ test(function () { }); -test(function () { +test('', function () { $filename = 'notfound.latte'; Assert::exception(function () use ($filename) { $presenter = new NetteModule\MicroPresenter; diff --git a/tests/Application/PresenterFactory.formatPresenterClass.phpt b/tests/Application/PresenterFactory.formatPresenterClass.phpt index b95139f16..694c3017e 100644 --- a/tests/Application/PresenterFactory.formatPresenterClass.phpt +++ b/tests/Application/PresenterFactory.formatPresenterClass.phpt @@ -13,7 +13,7 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; -test(function () { +test('', function () { $factory = new PresenterFactory; $factory->setMapping([ @@ -36,7 +36,7 @@ test(function () { }); -test(function () { +test('', function () { $factory = new PresenterFactory; $factory->setMapping([ @@ -53,7 +53,7 @@ test(function () { }); -test(function () { +test('', function () { $factory = new PresenterFactory; $factory->setMapping([ '*' => ['App', 'Module\*', 'Presenter\*'], @@ -63,7 +63,7 @@ test(function () { }); -test(function () { +test('', function () { $factory = new PresenterFactory; $factory->setMapping([ '*' => ['', '*', '*'], diff --git a/tests/Application/PresenterFactory.unformatPresenterClass.phpt b/tests/Application/PresenterFactory.unformatPresenterClass.phpt index c16230201..41ff7d44c 100644 --- a/tests/Application/PresenterFactory.unformatPresenterClass.phpt +++ b/tests/Application/PresenterFactory.unformatPresenterClass.phpt @@ -15,7 +15,7 @@ require __DIR__ . '/../bootstrap.php'; $factory = new PresenterFactory; -test(function () use ($factory) { +test('', function () use ($factory) { $factory->setMapping([ 'Foo2' => 'App2\*\*Presenter', 'Foo3' => 'My\App\*Mod\*Presenter', @@ -37,7 +37,7 @@ test(function () use ($factory) { }); -test(function () use ($factory) { +test('', function () use ($factory) { $factory->setMapping([ 'Foo2' => 'App2\*Presenter', 'Foo3' => 'My\App\*Presenter', diff --git a/tests/Application/Request.phpt b/tests/Application/Request.phpt index 859149980..7ae61fed5 100644 --- a/tests/Application/Request.phpt +++ b/tests/Application/Request.phpt @@ -13,7 +13,7 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; -test(function () { +test('', function () { $request = new Request('Homepage', 'GET', ['a' => 1, 'b' => null]); Assert::same(1, $request->getParameter('a')); @@ -21,7 +21,7 @@ test(function () { }); -test(function () { +test('', function () { $request = new Request('Homepage', 'GET', [], ['a' => 1, 'b' => null]); Assert::same(['a' => 1, 'b' => null], $request->getPost()); diff --git a/tests/Bridges.DI/ApplicationExtension.basic.phpt b/tests/Bridges.DI/ApplicationExtension.basic.phpt index 6c601fb79..7f31806fc 100644 --- a/tests/Bridges.DI/ApplicationExtension.basic.phpt +++ b/tests/Bridges.DI/ApplicationExtension.basic.phpt @@ -10,7 +10,7 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; -test(function () { +test('', function () { $compiler = new DI\Compiler; $compiler->addExtension('application', new ApplicationExtension(false)); diff --git a/tests/Bridges.DI/ApplicationExtension.invalidLink.phpt b/tests/Bridges.DI/ApplicationExtension.invalidLink.phpt index 41d968fe3..4d41ab141 100644 --- a/tests/Bridges.DI/ApplicationExtension.invalidLink.phpt +++ b/tests/Bridges.DI/ApplicationExtension.invalidLink.phpt @@ -24,7 +24,7 @@ function createCompiler(string $config): DI\Compiler } -test(function () { +test('', function () { $compiler = createCompiler(' application: silentLinks: yes @@ -44,7 +44,7 @@ test(function () { }); -test(function () { +test('', function () { $compiler = createCompiler(' application: silentLinks: no @@ -64,7 +64,7 @@ test(function () { }); -test(function () { +test('', function () { $compiler = createCompiler(' application: silentLinks: yes @@ -84,7 +84,7 @@ test(function () { }); -test(function () { +test('', function () { $compiler = createCompiler(' application: silentLinks: no diff --git a/tests/Bridges.DI/ApplicationExtension.scan.phpt b/tests/Bridges.DI/ApplicationExtension.scan.phpt index 59723a575..d2e919e2b 100644 --- a/tests/Bridges.DI/ApplicationExtension.scan.phpt +++ b/tests/Bridges.DI/ApplicationExtension.scan.phpt @@ -11,7 +11,7 @@ require __DIR__ . '/../bootstrap.php'; require __DIR__ . '/files/MyPresenter.php'; -test(function () { +test('', function () { $compiler = new DI\Compiler; $compiler->addExtension('application', new ApplicationExtension); @@ -29,7 +29,7 @@ test(function () { }); -test(function () { +test('', function () { $compiler = new DI\Compiler; $compiler->addExtension('application', new ApplicationExtension); @@ -51,7 +51,7 @@ test(function () { }); -test(function () { +test('', function () { $compiler = new DI\Compiler; $compiler->addExtension('application', new ApplicationExtension(false, [__DIR__ . '/files'])); @@ -80,7 +80,7 @@ test(function () { }); -test(function () { +test('', function () { $robot = new Nette\Loaders\RobotLoader; $robot->addDirectory(__DIR__ . '/files'); $robot->setTempDirectory(getTempDir()); diff --git a/tests/Bridges.DI/RoutingExtension.basic.phpt b/tests/Bridges.DI/RoutingExtension.basic.phpt index 5b6dd36ea..b7de9326c 100644 --- a/tests/Bridges.DI/RoutingExtension.basic.phpt +++ b/tests/Bridges.DI/RoutingExtension.basic.phpt @@ -19,7 +19,7 @@ class Route extends Nette\Application\Routers\Route } -test(function () { +test('', function () { $loader = new DI\Config\Loader; $config = $loader->load(Tester\FileMock::create(' routing: @@ -45,7 +45,7 @@ test(function () { }); -test(function () { +test('', function () { $loader = new DI\Config\Loader; $config = $loader->load(Tester\FileMock::create(' routing: diff --git a/tests/Bridges.DI/RoutingExtension.cache.phpt b/tests/Bridges.DI/RoutingExtension.cache.phpt index 070708444..b8bad1ebc 100644 --- a/tests/Bridges.DI/RoutingExtension.cache.phpt +++ b/tests/Bridges.DI/RoutingExtension.cache.phpt @@ -37,7 +37,7 @@ class MyRouter implements Nette\Routing\Router } -test(function () { +test('', function () { $loader = new DI\Config\Loader; $config = $loader->load(Tester\FileMock::create(' services: @@ -55,7 +55,7 @@ test(function () { }); -test(function () { +test('', function () { $loader = new DI\Config\Loader; $config = $loader->load(Tester\FileMock::create(' routing: @@ -77,7 +77,6 @@ test(function () { Assert::exception(function () { - /** @return Nette\Routing\Router */ function myRouterFactory() { diff --git a/tests/Bridges.Latte/TemplateFactory.customTemplate.phpt b/tests/Bridges.Latte/TemplateFactory.customTemplate.phpt index f23c1145f..2faea806b 100644 --- a/tests/Bridges.Latte/TemplateFactory.customTemplate.phpt +++ b/tests/Bridges.Latte/TemplateFactory.customTemplate.phpt @@ -42,7 +42,7 @@ class TemplateMock extends Template } -test(function () { +test('', function () { $latteFactory = Mockery::mock(ILatteFactory::class); $latteFactory->shouldReceive('create')->andReturn(new Latte\Engine); $factory = new TemplateFactory($latteFactory); @@ -54,7 +54,7 @@ Assert::exception(function () { }, \Nette\InvalidArgumentException::class, 'Class stdClass does not extend Nette\Bridges\ApplicationLatte\Template or it does not exist.'); -test(function () { +test('', function () { $latteFactory = Mockery::mock(ILatteFactory::class); $latteFactory->shouldReceive('create')->andReturn(new Latte\Engine); $factory = new TemplateFactory($latteFactory, null, null, null, TemplateMock::class); diff --git a/tests/Bridges.Latte/TemplateFactory.onCompile.phpt b/tests/Bridges.Latte/TemplateFactory.onCompile.phpt index a10f24c4d..a69f2ab7e 100644 --- a/tests/Bridges.Latte/TemplateFactory.onCompile.phpt +++ b/tests/Bridges.Latte/TemplateFactory.onCompile.phpt @@ -15,7 +15,7 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; -test(function () { +test('', function () { $engine = new Latte\Engine; $latteFactory = Mockery::mock(ILatteFactory::class); $latteFactory->shouldReceive('create')->andReturn($engine); @@ -30,7 +30,7 @@ test(function () { }); -test(function () { +test('', function () { $engine = new Latte\Engine; $latteFactory = Mockery::mock(ILatteFactory::class); $latteFactory->shouldReceive('create')->andReturn($engine); @@ -45,7 +45,7 @@ test(function () { }); -test(function () { +test('', function () { class Event implements IteratorAggregate { public $events; diff --git a/tests/Bridges.Latte/TemplateFactory.onCreate.phpt b/tests/Bridges.Latte/TemplateFactory.onCreate.phpt index 5835cdb17..12ca81dce 100644 --- a/tests/Bridges.Latte/TemplateFactory.onCreate.phpt +++ b/tests/Bridges.Latte/TemplateFactory.onCreate.phpt @@ -16,7 +16,7 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; -test(function () { +test('', function () { $engine = new Latte\Engine; $latteFactory = Mockery::mock(ILatteFactory::class); $latteFactory->shouldReceive('create')->andReturn($engine); diff --git a/tests/Responses/CallbackResponse.phpt b/tests/Responses/CallbackResponse.phpt index 512f018ee..8579c95e8 100644 --- a/tests/Responses/CallbackResponse.phpt +++ b/tests/Responses/CallbackResponse.phpt @@ -14,7 +14,7 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; -test(function () { +test('', function () { $response = new CallbackResponse(function (Http\IRequest $request, Http\IResponse $response) use (&$ok) { $ok = true; }); diff --git a/tests/Responses/FileResponse.contentDisposition.phpt b/tests/Responses/FileResponse.contentDisposition.phpt index 34edfbe0e..1650b54ad 100644 --- a/tests/Responses/FileResponse.contentDisposition.phpt +++ b/tests/Responses/FileResponse.contentDisposition.phpt @@ -18,7 +18,7 @@ if (PHP_SAPI === 'cli') { } -test(function () { +test('', function () { $file = __FILE__; $fileResponse = new FileResponse($file); $origData = file_get_contents($file); @@ -34,7 +34,7 @@ test(function () { }); -test(function () { +test('', function () { $file = __FILE__; $fileResponse = new FileResponse($file, null, null, false); $origData = file_get_contents($file); @@ -50,7 +50,7 @@ test(function () { }); -test(function () { +test('', function () { $file = __FILE__; $fileName = 'žluťoučký kůň.txt'; $fileResponse = new FileResponse($file, $fileName); diff --git a/tests/Responses/FileResponse.full.phpt b/tests/Responses/FileResponse.full.phpt index 3c62a2525..dc5528913 100644 --- a/tests/Responses/FileResponse.full.phpt +++ b/tests/Responses/FileResponse.full.phpt @@ -15,7 +15,7 @@ require __DIR__ . '/../bootstrap.php'; /* A small file */ -test(function () { +test('', function () { $file = __FILE__; $fileResponse = new FileResponse($file); $origData = file_get_contents($file); @@ -26,7 +26,7 @@ test(function () { }); /* A big file */ -test(function () { +test('', function () { $file = Tester\FileMock::create(); $data = ''; diff --git a/tests/Responses/FileResponse.range.phpt b/tests/Responses/FileResponse.range.phpt index 710908301..b34a83261 100644 --- a/tests/Responses/FileResponse.range.phpt +++ b/tests/Responses/FileResponse.range.phpt @@ -19,7 +19,7 @@ $file = __FILE__; $fileResponse = new FileResponse($file); $origData = file_get_contents($file); -test(function () use ($fileResponse, $origData) { +test('', function () use ($fileResponse, $origData) { ob_start(); $fileResponse->send( new Http\Request(new Http\UrlScript, null, null, null, ['range' => 'bytes=10-20']), @@ -30,7 +30,7 @@ test(function () use ($fileResponse, $origData) { }); -test(function () use ($fileResponse, $origData) { +test('', function () use ($fileResponse, $origData) { ob_start(); $fileResponse->send( new Http\Request(new Http\UrlScript, null, null, null, ['range' => 'bytes=10-10']), @@ -40,7 +40,7 @@ test(function () use ($fileResponse, $origData) { }); -test(function () use ($fileResponse, $origData, $file) { +test('', function () use ($fileResponse, $origData, $file) { ob_start(); $fileResponse->send( new Http\Request(new Http\UrlScript, null, null, null, ['range' => 'bytes=10-' . filesize($file)]), @@ -50,7 +50,7 @@ test(function () use ($fileResponse, $origData, $file) { }); -test(function () use ($fileResponse, $origData) { // prefix +test('prefix', function () use ($fileResponse, $origData) { ob_start(); $fileResponse->send( new Http\Request(new Http\UrlScript, null, null, null, ['range' => 'bytes=20-']), @@ -60,7 +60,7 @@ test(function () use ($fileResponse, $origData) { // prefix }); -test(function () use ($fileResponse, $origData, $file) { // prefix +test('prefix', function () use ($fileResponse, $origData, $file) { ob_start(); $fileResponse->send( new Http\Request(new Http\UrlScript, null, null, null, ['range' => 'bytes=' . (filesize($file) - 1) . '-']), @@ -70,7 +70,7 @@ test(function () use ($fileResponse, $origData, $file) { // prefix }); -test(function () use ($fileResponse, $file) { // prefix +test('prefix', function () use ($fileResponse, $file) { ob_start(); $fileResponse->send( new Http\Request(new Http\UrlScript, null, null, null, ['range' => 'bytes=' . filesize($file) . '-']), @@ -81,7 +81,7 @@ test(function () use ($fileResponse, $file) { // prefix }); -test(function () use ($fileResponse, $origData) { // suffix +test('suffix', function () use ($fileResponse, $origData) { ob_start(); $fileResponse->send( new Http\Request(new Http\UrlScript, null, null, null, ['range' => 'bytes=-20']), @@ -91,7 +91,7 @@ test(function () use ($fileResponse, $origData) { // suffix }); -test(function () use ($fileResponse, $origData, $file) { // suffix +test('suffix', function () use ($fileResponse, $origData, $file) { ob_start(); $fileResponse->send( new Http\Request(new Http\UrlScript, null, null, null, ['range' => 'bytes=-' . filesize($file)]), diff --git a/tests/Responses/JsonResponse.contentType.phpt b/tests/Responses/JsonResponse.contentType.phpt index 723178de9..0cbe48d12 100644 --- a/tests/Responses/JsonResponse.contentType.phpt +++ b/tests/Responses/JsonResponse.contentType.phpt @@ -17,7 +17,7 @@ if (PHP_SAPI === 'cli') { Tester\Environment::skip('Requires CGI SAPI to work with HTTP headers.'); } -test(function () { +test('', function () { $data = ['text' => 'žluťoučký kůň']; $encoded = json_encode($data, JSON_UNESCAPED_UNICODE); $jsonResponse = new JsonResponse($data, 'application/json'); @@ -29,7 +29,7 @@ test(function () { Assert::same('application/json; charset=utf-8', $response->getHeader('Content-Type')); }); -test(function () { +test('', function () { $data = true; $encoded = json_encode($data, JSON_UNESCAPED_UNICODE); $jsonResponse = new JsonResponse($data, 'application/json'); diff --git a/tests/Routers/LinkGenerator.phpt b/tests/Routers/LinkGenerator.phpt index cb53526ce..600f0090e 100644 --- a/tests/Routers/LinkGenerator.phpt +++ b/tests/Routers/LinkGenerator.phpt @@ -49,7 +49,7 @@ namespace { $pf = new PresenterFactory; - test(function () use ($pf) { + test('', function () use ($pf) { $generator = new LinkGenerator(new Routers\SimpleRouter, new Http\UrlScript('http://nette.org/en/'), $pf); Assert::same('http://nette.org/en/?action=default&presenter=Homepage', $generator->link('Homepage:default')); Assert::same('http://nette.org/en/?action=default&presenter=Module%3AMy', $generator->link('Module:My:default')); @@ -80,7 +80,7 @@ namespace { }, Nette\Application\UI\InvalidLinkException::class, "Unable to pass parameters to action 'Homepage:missing', missing corresponding method."); - test(function () { + test('', function () { $generator = new LinkGenerator(new Routers\SimpleRouter, new Http\UrlScript('http://nette.org/en/')); Assert::same('http://nette.org/en/?action=default&presenter=Homepage', $generator->link('Homepage:default')); Assert::same('http://nette.org/en/?action=default&presenter=Module%3AMy', $generator->link('Module:My:default')); @@ -93,7 +93,7 @@ namespace { }); - test(function () { + test('', function () { $generator = new LinkGenerator(new Routers\SimpleRouter, new Http\UrlScript('http://nette.org/en/')); $generator2 = $generator->withReferenceUrl('http://nette.org/cs/'); Assert::same('http://nette.org/en/?action=default&presenter=Homepage', $generator->link('Homepage:default')); diff --git a/tests/Routers/Route.scalarParams.phpt b/tests/Routers/Route.scalarParams.phpt index c0b7dbe4e..2d1224792 100644 --- a/tests/Routers/Route.scalarParams.phpt +++ b/tests/Routers/Route.scalarParams.phpt @@ -15,7 +15,7 @@ require __DIR__ . '/../bootstrap.php'; require __DIR__ . '/Route.php'; -test(function () { +test('', function () { $route = new Route('/', [ ]); @@ -44,7 +44,7 @@ test(function () { }); -test(function () { +test('', function () { $route = new Route('/', [ 'param' => 12, ]); @@ -78,7 +78,7 @@ test(function () { }); -test(function () { +test('', function () { $route = new Route('/', [ 'param' => 12.1, ]); @@ -112,7 +112,7 @@ test(function () { }); -test(function () { +test('', function () { $route = new Route('/', [ 'param' => true, ]); @@ -146,7 +146,7 @@ test(function () { }); -test(function () { +test('', function () { $route = new Route('/', [ 'param' => false, ]); @@ -180,7 +180,7 @@ test(function () { }); -test(function () { +test('', function () { $route = new Route('/', [ 'param' => null, ]); diff --git a/tests/Routers/SimpleRouter.invalid.phpt b/tests/Routers/SimpleRouter.invalid.phpt index 03759164b..ab34229e0 100644 --- a/tests/Routers/SimpleRouter.invalid.phpt +++ b/tests/Routers/SimpleRouter.invalid.phpt @@ -16,7 +16,7 @@ require __DIR__ . '/../bootstrap.php'; -test(function () { +test('', function () { $router = new SimpleRouter; $url = new Http\UrlScript('http://nette.org?presenter[]=foo'); $httpRequest = new Http\Request($url); @@ -25,7 +25,7 @@ test(function () { Assert::same(['presenter' => ['foo']], $req); }); -test(function () { +test('', function () { $router = new SimpleRouter; $url = new Http\UrlScript('http://nette.org'); $httpRequest = new Http\Request($url); diff --git a/tests/UI/Component.redirect().phpt b/tests/UI/Component.redirect().phpt index 43721a787..fabe34617 100644 --- a/tests/UI/Component.redirect().phpt +++ b/tests/UI/Component.redirect().phpt @@ -42,7 +42,7 @@ $presenter->injectPrimary( ); -test(function () use ($presenter) { +test('', function () use ($presenter) { $presenter->redirect('foo'); Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response); Assert::same(302, $presenter->response->getCode()); @@ -50,7 +50,7 @@ test(function () use ($presenter) { }); -test(function () use ($presenter) { +test('', function () use ($presenter) { $presenter->redirect('foo', ['arg' => 1]); Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response); Assert::same(302, $presenter->response->getCode()); @@ -58,7 +58,7 @@ test(function () use ($presenter) { }); -test(function () use ($presenter) { +test('', function () use ($presenter) { $presenter->redirect('foo', 2); Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response); Assert::same(302, $presenter->response->getCode()); @@ -66,7 +66,7 @@ test(function () use ($presenter) { }); -test(function () use ($presenter) { +test('', function () use ($presenter) { @$presenter->redirect(301, 'foo', ['arg' => 1]); // @ is deprecated Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response); Assert::same(301, $presenter->response->getCode()); @@ -74,7 +74,7 @@ test(function () use ($presenter) { }); -test(function () use ($presenter) { +test('', function () use ($presenter) { @$presenter->redirect(301, 'foo', 2); // @ is deprecated Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response); Assert::same(301, $presenter->response->getCode()); @@ -82,7 +82,7 @@ test(function () use ($presenter) { }); -test(function () use ($presenter) { +test('', function () use ($presenter) { $presenter->redirectPermanent('foo', 2); Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response); Assert::same(301, $presenter->response->getCode()); @@ -90,7 +90,7 @@ test(function () use ($presenter) { }); -test(function () use ($presenter) { +test('', function () use ($presenter) { $presenter->redirectPermanent('foo', ['arg' => 1]); Assert::type(Nette\Application\Responses\RedirectResponse::class, $presenter->response); Assert::same(301, $presenter->response->getCode()); diff --git a/tests/UI/ComponentReflection.combineArgs.phpt b/tests/UI/ComponentReflection.combineArgs.phpt index 32c9e9118..d972d3151 100644 --- a/tests/UI/ComponentReflection.combineArgs.phpt +++ b/tests/UI/ComponentReflection.combineArgs.phpt @@ -50,7 +50,7 @@ class MyPresenter } -test(function () { +test('', function () { $method = new ReflectionMethod('MyPresenter', 'params'); Assert::same([null, null, null, null], Reflection::combineArgs($method, [])); @@ -65,7 +65,7 @@ test(function () { }); -test(function () { +test('', function () { $method = new ReflectionMethod('MyPresenter', 'hints'); Assert::same([1, true, 'abc', [1], [2]], Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => 'abc', 'arr' => [1], 'iter' => [2]])); @@ -101,7 +101,7 @@ test(function () { }); -test(function () { +test('', function () { $method = new ReflectionMethod('MyPresenter', 'hintsNulls'); Assert::same([null, null, null, null, null], Reflection::combineArgs($method, [])); @@ -131,7 +131,7 @@ test(function () { }); -test(function () { +test('', function () { $method = new ReflectionMethod('MyPresenter', 'hintsNullable'); Assert::same([null, null, null, null, null], Reflection::combineArgs($method, [])); @@ -161,7 +161,7 @@ test(function () { }); -test(function () { +test('', function () { $method = new ReflectionMethod('MyPresenter', 'hintsDefaults'); Assert::same([0, false, '', [], []], Reflection::combineArgs($method, [])); @@ -191,7 +191,7 @@ test(function () { }); -test(function () { +test('', function () { $method = new ReflectionMethod('MyPresenter', 'defaults'); Assert::same([0, false, '', []], Reflection::combineArgs($method, [])); @@ -221,7 +221,7 @@ test(function () { }); -test(function () { +test('', function () { $method = new ReflectionMethod('MyPresenter', 'objects'); Assert::equal([new stdClass, new stdClass, new stdClass], Reflection::combineArgs($method, ['req' => new stdClass, 'opt' => new stdClass, 'nullable' => new stdClass])); diff --git a/tests/UI/Control.isControlInvalid.phpt b/tests/UI/Control.isControlInvalid.phpt index e5fc3e1fc..0021c9b9e 100644 --- a/tests/UI/Control.isControlInvalid.phpt +++ b/tests/UI/Control.isControlInvalid.phpt @@ -18,7 +18,7 @@ class TestControl extends UI\Control } -test(function () { +test('', function () { $control = new TestControl; $child = new TestControl; $control->addComponent($child, 'foo'); @@ -29,7 +29,7 @@ test(function () { }); -test(function () { +test('', function () { $control = new TestControl; $child = new Nette\ComponentModel\Container; $grandChild = new TestControl; diff --git a/tests/UI/Form.phpt b/tests/UI/Form.phpt index b519b0320..828665fc1 100644 --- a/tests/UI/Form.phpt +++ b/tests/UI/Form.phpt @@ -18,14 +18,14 @@ class TestPresenter extends UI\Presenter } -test(function () { +test('', function () { $presenter = new TestPresenter; $form = new UI\Form($presenter, 'name'); $form->setMethod($form::GET); // must not throw exception }); -test(function () { // compatibility with 2.0 +test('compatibility with 2.0', function () { $presenter = new TestPresenter; $form = new UI\Form; $form->setAction('action'); diff --git a/tests/UI/Presenter.formatLayoutTemplateFiles.phpt b/tests/UI/Presenter.formatLayoutTemplateFiles.phpt index 09b5c015a..261147500 100644 --- a/tests/UI/Presenter.formatLayoutTemplateFiles.phpt +++ b/tests/UI/Presenter.formatLayoutTemplateFiles.phpt @@ -14,7 +14,7 @@ require __DIR__ . '/one/Presenter1.php'; require __DIR__ . '/two/Presenter2.php'; -test(function () { // with subdir templates +test('with subdir templates', function () { $presenter = new Presenter1; $presenter->setParent(null, 'One'); $presenter->setLayout('my'); @@ -27,7 +27,7 @@ test(function () { // with subdir templates }); -test(function () { // without subdir templates +test('without subdir templates', function () { $presenter = new Presenter2; $presenter->setParent(null, 'Two'); @@ -39,7 +39,7 @@ test(function () { // without subdir templates }); -test(function () { // with module & subdir templates +test('with module & subdir templates', function () { $presenter = new Presenter1; $presenter->setParent(null, 'Module:SubModule:One'); @@ -53,7 +53,7 @@ test(function () { // with module & subdir templates }); -test(function () { // with module & without subdir templates +test('with module & without subdir templates', function () { $presenter = new Presenter2; $presenter->setParent(null, 'Module:SubModule:Two'); @@ -67,7 +67,7 @@ test(function () { // with module & without subdir templates }); -test(function () { // direct file +test('direct file', function () { $presenter = new Presenter2; $presenter->setLayout(__DIR__ . '/file.latte'); diff --git a/tests/UI/Presenter.formatTemplateFiles.phpt b/tests/UI/Presenter.formatTemplateFiles.phpt index 19770561e..4f55d48c4 100644 --- a/tests/UI/Presenter.formatTemplateFiles.phpt +++ b/tests/UI/Presenter.formatTemplateFiles.phpt @@ -14,7 +14,7 @@ require __DIR__ . '/one/Presenter1.php'; require __DIR__ . '/two/Presenter2.php'; -test(function () { // with subdir templates +test('with subdir templates', function () { $presenter = new Presenter1; $presenter->setParent(null, 'One'); $presenter->setView('view'); @@ -26,7 +26,7 @@ test(function () { // with subdir templates }); -test(function () { // without subdir templates +test('without subdir templates', function () { $presenter = new Presenter2; $presenter->setParent(null, 'Two'); $presenter->setView('view'); @@ -38,7 +38,7 @@ test(function () { // without subdir templates }); -test(function () { // with module & subdir templates +test('with module & subdir templates', function () { $presenter = new Presenter1; $presenter->setParent(null, 'Module:One'); $presenter->setView('view'); @@ -50,7 +50,7 @@ test(function () { // with module & subdir templates }); -test(function () { // with module & without subdir templates +test('with module & without subdir templates', function () { $presenter = new Presenter2; $presenter->setParent(null, 'Module:Two'); $presenter->setView('view'); diff --git a/tests/UI/Presenter.isModuleCurrent().phpt b/tests/UI/Presenter.isModuleCurrent().phpt index ffa69b70b..1ac2f4295 100644 --- a/tests/UI/Presenter.isModuleCurrent().phpt +++ b/tests/UI/Presenter.isModuleCurrent().phpt @@ -16,7 +16,7 @@ class TestPresenter extends \Nette\Application\UI\Presenter } -test(function () { +test('', function () { $presenter = new TestPresenter; $presenter->setParent(null, 'Test'); @@ -26,7 +26,7 @@ test(function () { }); -test(function () { +test('', function () { $presenter = new TestPresenter; $presenter->setParent(null, 'First:Second:Third:Test'); diff --git a/tests/UI/Presenter.parameters.phpt b/tests/UI/Presenter.parameters.phpt index 55d9e59a8..c55be4093 100644 --- a/tests/UI/Presenter.parameters.phpt +++ b/tests/UI/Presenter.parameters.phpt @@ -42,8 +42,7 @@ function createPresenter() } -test(function () { - //signal in GET +test('signal in GET', function () { $presenter = createPresenter(); $presenter->run(new Application\Request('Foo', 'GET', [ 'do' => 'foo', @@ -51,8 +50,7 @@ test(function () { Assert::same(['', 'foo'], $presenter->getSignal()); }); -test(function () { - //signal for subcomponent in GET +test('signal for subcomponent in GET', function () { $presenter = createPresenter(); $presenter->run(new Application\Request('Foo', 'GET', [ 'do' => 'foo-bar', @@ -60,8 +58,7 @@ test(function () { Assert::same(['foo', 'bar'], $presenter->getSignal()); }); -test(function () { - //signal in POST +test('signal in POST', function () { $presenter = createPresenter(); $presenter->run(new Application\Request('Foo', 'POST', [], [ 'do' => 'foo', @@ -69,8 +66,7 @@ test(function () { Assert::null($presenter->getSignal()); }); -test(function () { - //_signal_ in POST +test('_signal_ in POST', function () { $presenter = createPresenter(); $presenter->run(new Application\Request('Foo', 'POST', [], [ '_do' => 'foo', @@ -78,8 +74,7 @@ test(function () { Assert::same(['', 'foo'], $presenter->getSignal()); }); -test(function () { - //signal in POST not overwriting GET +test('signal in POST not overwriting GET', function () { $presenter = createPresenter(); $presenter->run(new Application\Request('Foo', 'POST', ['do' => null], [ 'do' => 'foo', @@ -87,8 +82,7 @@ test(function () { Assert::null($presenter->getSignal()); }); -test(function () { - //_signal_ in POST overwriting GET +test('_signal_ in POST overwriting GET', function () { $presenter = createPresenter(); $presenter->run(new Application\Request('Foo', 'POST', ['do' => 'bar'], [ '_do' => 'foo', @@ -96,8 +90,7 @@ test(function () { Assert::same(['', 'foo'], $presenter->getSignal()); }); -test(function () { - //AJAX: signal in POST +test('AJAX: signal in POST', function () { $presenter = createPresenter(); $presenter->ajax = true; $presenter->run(new Application\Request('Foo', 'POST', [], [ @@ -106,8 +99,7 @@ test(function () { Assert::same(['', 'foo'], $presenter->getSignal()); }); -test(function () { - //AJAX: signal in POST overwriting GET +test('AJAX: signal in POST overwriting GET', function () { $presenter = createPresenter(); $presenter->ajax = true; $presenter->run(new Application\Request('Foo', 'POST', ['do' => 'bar'], [ @@ -116,8 +108,7 @@ test(function () { Assert::same(['', 'foo'], $presenter->getSignal()); }); -test(function () { - //AJAX: _signal_ in POST overwriting GET +test('AJAX: _signal_ in POST overwriting GET', function () { $presenter = createPresenter(); $presenter->ajax = true; $presenter->run(new Application\Request('Foo', 'POST', ['do' => 'bar'], [ diff --git a/tests/UI/Presenter.storeRequest().phpt b/tests/UI/Presenter.storeRequest().phpt index ca1fdcec9..b7cc73e41 100644 --- a/tests/UI/Presenter.storeRequest().phpt +++ b/tests/UI/Presenter.storeRequest().phpt @@ -113,7 +113,7 @@ class MockUser extends Security\User } } -test(function () { +test('', function () { $presenter = new TestPresenter; $presenter->injectPrimary( null, @@ -140,7 +140,7 @@ test(function () { Assert::same([$user->getId(), $applicationRequest], $section->storedValue); }); -test(function () { +test('', function () { $presenter = new TestPresenter; $presenter->injectPrimary( null, diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 4d8c52cf9..91c483705 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -43,7 +43,7 @@ function getTempDir(): string } -function test(\Closure $function): void +function test(string $title, Closure $function): void { $function(); Mockery::close(); From 872c14343e752b520bb497011d925dd24a2faa66 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 6 Oct 2020 18:12:05 +0200 Subject: [PATCH 02/10] ApplicationExtension, RoutingExtension: detects Tracy by presence of service [Closes nette/di#245] --- src/Bridges/ApplicationDI/ApplicationExtension.php | 13 ++++++++----- src/Bridges/ApplicationDI/RoutingExtension.php | 5 ++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Bridges/ApplicationDI/ApplicationExtension.php b/src/Bridges/ApplicationDI/ApplicationExtension.php index af975a456..7fa000764 100644 --- a/src/Bridges/ApplicationDI/ApplicationExtension.php +++ b/src/Bridges/ApplicationDI/ApplicationExtension.php @@ -50,7 +50,7 @@ public function __construct(bool $debugMode = false, array $scanDirs = null, str public function getConfigSchema(): Nette\Schema\Schema { return Expect::structure([ - 'debugger' => Expect::bool(interface_exists(Tracy\IBarPanel::class)), + 'debugger' => Expect::bool(), 'errorPresenter' => Expect::string('Nette:Error')->dynamic(), 'catchExceptions' => Expect::bool(!$this->debugMode)->dynamic(), 'mapping' => Expect::arrayOf('string|array'), @@ -72,14 +72,11 @@ public function loadConfiguration() ? UI\Presenter::INVALID_LINK_TEXTUAL | ($config->silentLinks ? 0 : UI\Presenter::INVALID_LINK_WARNING) : UI\Presenter::INVALID_LINK_WARNING; - $application = $builder->addDefinition($this->prefix('application')) + $builder->addDefinition($this->prefix('application')) ->setFactory(Nette\Application\Application::class) ->addSetup('$catchExceptions', [$config->catchExceptions]) ->addSetup('$errorPresenter', [$config->errorPresenter]); - if ($config->debugger) { - $application->addSetup([Nette\Bridges\ApplicationTracy\RoutingPanel::class, 'initializePanel']); - } $this->compiler->addExportedType(Nette\Application\Application::class); if ($this->debugMode && ($config->scanDirs || $this->robotLoader) && $this->tempDir) { @@ -112,6 +109,12 @@ public function loadConfiguration() public function beforeCompile() { $builder = $this->getContainerBuilder(); + + if ($this->config->debugger ?? $builder->getByType(Tracy\BlueScreen::class)) { + $builder->getDefinition($this->prefix('application')) + ->addSetup([Nette\Bridges\ApplicationTracy\RoutingPanel::class, 'initializePanel']); + } + $all = []; foreach ($builder->findByType(Nette\Application\IPresenter::class) as $def) { diff --git a/src/Bridges/ApplicationDI/RoutingExtension.php b/src/Bridges/ApplicationDI/RoutingExtension.php index deae3c536..597c841d2 100644 --- a/src/Bridges/ApplicationDI/RoutingExtension.php +++ b/src/Bridges/ApplicationDI/RoutingExtension.php @@ -28,7 +28,7 @@ public function __construct(bool $debugMode = false) $this->debugMode = $debugMode; $this->config = new class { - /** @var bool */ + /** @var ?bool */ public $debugger; /** @var string[] */ public $routes = []; @@ -37,7 +37,6 @@ public function __construct(bool $debugMode = false) /** @var bool */ public $cache = false; }; - $this->config->debugger = interface_exists(Tracy\IBarPanel::class); } @@ -65,7 +64,7 @@ public function beforeCompile() if ( $this->debugMode && - $this->config->debugger && + ($this->config->debugger ?? $builder->getByType(Tracy\Bar::class)) && ($name = $builder->getByType(Nette\Application\Application::class)) && ($application = $builder->getDefinition($name)) instanceof Definitions\ServiceDefinition ) { From 85994596b09a26b2c2be09f4234787fa69d71376 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 6 Oct 2020 18:49:25 +0200 Subject: [PATCH 03/10] MicroPresenter: throws BadRequestException on invalid argument --- src/Application/MicroPresenter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Application/MicroPresenter.php b/src/Application/MicroPresenter.php index 0beb1a46f..6da988f5d 100644 --- a/src/Application/MicroPresenter.php +++ b/src/Application/MicroPresenter.php @@ -69,7 +69,7 @@ public function run(Application\Request $request): Application\IResponse $params = $request->getParameters(); $callback = $params['callback'] ?? null; if (!$callback instanceof \Closure) { - throw new Nette\InvalidStateException('Parameter callback is not a valid closure.'); + throw new Application\BadRequestException('Parameter callback is not a valid closure.'); } $reflection = new \ReflectionFunction($callback); From 3b79aa3d1aeed3ae3502cd346a2c5411ba6e31c0 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 15 Oct 2020 22:16:27 +0200 Subject: [PATCH 04/10] tested on PHP 8.0 --- .github/workflows/tests.yml | 14 ++++++++------ .travis.yml | 1 + 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 708879fd6..aa8766a20 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,14 +7,14 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: ['7.1', '7.2', '7.3', '7.4'] + php: ['7.1', '7.2', '7.3', '7.4', '8.0'] fail-fast: false name: PHP ${{ matrix.php }} tests steps: - uses: actions/checkout@v2 - - uses: shivammathur/setup-php@v1 + - uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} coverage: none @@ -22,8 +22,10 @@ jobs: - run: composer install --no-progress --prefer-dist - run: vendor/bin/tester tests -s -C - if: failure() - run: for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done - shell: bash + uses: actions/upload-artifact@v2 + with: + name: output + path: tests/**/output lowest_dependencies: @@ -31,7 +33,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - uses: shivammathur/setup-php@v1 + - uses: shivammathur/setup-php@v2 with: php-version: 7.1 coverage: none @@ -45,7 +47,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - uses: shivammathur/setup-php@v1 + - uses: shivammathur/setup-php@v2 with: php-version: 7.4 coverage: none diff --git a/.travis.yml b/.travis.yml index edf25e2c5..53850c03f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ php: - 7.2 - 7.3 - 7.4 + - 8.0snapshot env: - PHP_BIN=php From 7a3f051a354f39de90ccc17d700b49af156037d2 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 15 Oct 2020 23:51:09 +0200 Subject: [PATCH 05/10] fixed compatibility with PHP 8 --- src/Application/MicroPresenter.php | 4 ++-- src/Bridges/ApplicationLatte/TemplateFactory.php | 2 +- tests/Bridges.Latte/templates/snippet-include.latte | 2 +- tests/UI/Presenter.paramChecking.phpt | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Application/MicroPresenter.php b/src/Application/MicroPresenter.php index 6da988f5d..9a0061460 100644 --- a/src/Application/MicroPresenter.php +++ b/src/Application/MicroPresenter.php @@ -75,8 +75,8 @@ public function run(Application\Request $request): Application\IResponse if ($this->context) { foreach ($reflection->getParameters() as $param) { - if ($param->getClass()) { - $params[$param->getName()] = $this->context->getByType($param->getClass()->getName(), false); + if ($param->getType()) { + $params[$param->getName()] = $this->context->getByType($param->getType()->getName(), false); } } } diff --git a/src/Bridges/ApplicationLatte/TemplateFactory.php b/src/Bridges/ApplicationLatte/TemplateFactory.php index a2ddf66c2..5f9b25be9 100644 --- a/src/Bridges/ApplicationLatte/TemplateFactory.php +++ b/src/Bridges/ApplicationLatte/TemplateFactory.php @@ -108,7 +108,7 @@ public function createTemplate(UI\Control $control = null): UI\ITemplate // default parameters $template->user = $this->user; $template->baseUrl = $this->httpRequest ? rtrim($this->httpRequest->getUrl()->withoutUserInfo()->getBaseUrl(), '/') : null; - $template->basePath = preg_replace('#https?://[^/]+#A', '', $template->baseUrl); + $template->basePath = $this->httpRequest ? preg_replace('#https?://[^/]+#A', '', $template->baseUrl) : null; $template->flashes = []; if ($control) { $template->control = $control; diff --git a/tests/Bridges.Latte/templates/snippet-include.latte b/tests/Bridges.Latte/templates/snippet-include.latte index b59d67c45..ed8671983 100644 --- a/tests/Bridges.Latte/templates/snippet-include.latte +++ b/tests/Bridges.Latte/templates/snippet-include.latte @@ -16,7 +16,7 @@ {/foreach} {/snippetArea} -{snippetArea} +{snippetArea foo} {include snippet-included.latte say => 'Hello include snippet'} {/snippetArea} diff --git a/tests/UI/Presenter.paramChecking.phpt b/tests/UI/Presenter.paramChecking.phpt index 2aee18e54..f792d4e3a 100644 --- a/tests/UI/Presenter.paramChecking.phpt +++ b/tests/UI/Presenter.paramChecking.phpt @@ -12,7 +12,7 @@ use Tester\Assert; require __DIR__ . '/../bootstrap.php'; -require __DIR__ . '/fixtures/ParamPresenter.php'; +@require __DIR__ . '/fixtures/ParamPresenter.php'; // @ Required parameter $c follows optional parameter $b $presenter = new ParamPresenter; From 98ccef447595d38b5f44245a337db6dceea5a166 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 29 Oct 2020 01:10:27 +0100 Subject: [PATCH 06/10] updated nette/coding-standard --- .github/workflows/coding-style.yml | 8 ++++---- .travis.yml | 7 +++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/.github/workflows/coding-style.yml b/.github/workflows/coding-style.yml index 4a708c531..d415d813d 100644 --- a/.github/workflows/coding-style.yml +++ b/.github/workflows/coding-style.yml @@ -10,7 +10,7 @@ jobs: - uses: actions/checkout@v2 - uses: shivammathur/setup-php@v1 with: - php-version: 7.4 + php-version: 7.1 coverage: none - run: composer create-project nette/code-checker temp/code-checker ^3 --no-progress @@ -24,8 +24,8 @@ jobs: - uses: actions/checkout@v2 - uses: shivammathur/setup-php@v1 with: - php-version: 7.4 + php-version: 8.0 coverage: none - - run: composer create-project nette/coding-standard temp/coding-standard ^2 --no-progress - - run: php temp/coding-standard/ecs check src tests --config temp/coding-standard/coding-standard-php71.yml + - run: composer create-project nette/coding-standard temp/coding-standard ^3 --no-progress --ignore-platform-reqs + - run: php temp/coding-standard/ecs check diff --git a/.travis.yml b/.travis.yml index 53850c03f..1c6bfc910 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,7 +33,6 @@ jobs: - name: Nette Code Checker - php: 7.4 install: - travis_retry composer create-project nette/code-checker temp/code-checker ^3 --no-progress script: @@ -41,11 +40,11 @@ jobs: - name: Nette Coding Standard - php: 7.4 + php: 8.0snapshot install: - - travis_retry composer create-project nette/coding-standard temp/coding-standard ^2 --no-progress + - travis_retry composer create-project nette/coding-standard temp/coding-standard ^3 --no-progress --ignore-platform-reqs script: - - php temp/coding-standard/ecs check src tests --config temp/coding-standard/coding-standard-php71.yml + - php temp/coding-standard/ecs check - stage: Static Analysis (informative) From a0fa39b6163c9b40df310ba0f7e218b46ca92f56 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 29 Oct 2020 01:43:50 +0100 Subject: [PATCH 07/10] updated phpstan --- composer.json | 2 +- phpstan.neon | 8 ++++++++ tests/phpstan.neon | 2 -- 3 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 phpstan.neon delete mode 100644 tests/phpstan.neon diff --git a/composer.json b/composer.json index aadd179f5..300527ade 100644 --- a/composer.json +++ b/composer.json @@ -47,7 +47,7 @@ }, "minimum-stability": "dev", "scripts": { - "phpstan": "phpstan analyse --level 5 --configuration tests/phpstan.neon src", + "phpstan": "phpstan analyse", "tester": "tester tests -s" }, "extra": { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 000000000..8a634adc9 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,8 @@ +parameters: + level: 5 + + paths: + - src + +includes: + - vendor/phpstan/phpstan-nette/extension.neon diff --git a/tests/phpstan.neon b/tests/phpstan.neon deleted file mode 100644 index 60616a105..000000000 --- a/tests/phpstan.neon +++ /dev/null @@ -1,2 +0,0 @@ -includes: - - ../vendor/phpstan/phpstan-nette/extension.neon From 79c50350ec00ac45ad6a3f1eeb2a52fb87a6f33e Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 27 Oct 2020 19:05:14 +0100 Subject: [PATCH 08/10] updated .gitattributes --- .gitattributes | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitattributes b/.gitattributes index ab7320736..3aa6270af 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,5 +2,9 @@ .gitignore export-ignore .github export-ignore .travis.yml export-ignore +ecs.php export-ignore +phpstan.neon export-ignore tests/ export-ignore + *.sh eol=lf +*.php* diff=php linguist-language=PHP From 6bc88c8d881033257cfc0699f6ee2c4dad3fe54b Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 28 Oct 2020 16:04:28 +0100 Subject: [PATCH 09/10] coding style, refactoring --- src/Application/Application.php | 17 ++- src/Application/LinkGenerator.php | 4 +- src/Application/MicroPresenter.php | 22 +++- src/Application/Request.php | 10 +- src/Application/Responses/FileResponse.php | 14 ++- src/Application/Routers/CliRouter.php | 16 +-- src/Application/Routers/Route.php | 23 ++-- src/Application/Routers/RouteList.php | 6 +- src/Application/UI/Component.php | 26 +++-- src/Application/UI/ComponentReflection.php | 8 +- src/Application/UI/Control.php | 43 ++++--- src/Application/UI/Form.php | 10 +- src/Application/UI/Presenter.php | 108 ++++++++++++------ .../ApplicationDI/ApplicationExtension.php | 11 +- src/Bridges/ApplicationDI/LatteExtension.php | 3 + .../ApplicationDI/RoutingExtension.php | 3 + src/Bridges/ApplicationLatte/Template.php | 4 +- .../ApplicationLatte/TemplateFactory.php | 22 +++- src/Bridges/ApplicationLatte/UIMacros.php | 14 ++- src/Bridges/ApplicationTracy/RoutingPanel.php | 27 +++-- tests/Bridges.DI/LatteExtension.basic.phpt | 4 +- .../Bridges.Latte/UIMacros.isLinkCurrent.phpt | 5 +- tests/Routers/Route.php | 4 +- tests/Routers/RouteList.addRoute.phpt | 4 +- tests/Routers/RouteList.modules.phpt | 4 +- tests/Routers/RouteList.withModule.phpt | 12 +- tests/UI/ComponentReflection.combineArgs.phpt | 18 ++- .../Presenter.formatLayoutTemplateFiles.phpt | 2 +- tests/UI/Presenter.isModuleCurrent().phpt | 2 +- ...link().74.phptx => Presenter.link().php74} | 0 tests/UI/Presenter.storeRequest().phpt | 6 +- 31 files changed, 299 insertions(+), 153 deletions(-) rename tests/UI/{Presenter.link().74.phptx => Presenter.link().php74} (100%) diff --git a/src/Application/Application.php b/src/Application/Application.php index 4a593a3fc..533d32d8f 100644 --- a/src/Application/Application.php +++ b/src/Application/Application.php @@ -66,8 +66,12 @@ class Application private $router; - public function __construct(IPresenterFactory $presenterFactory, Router $router, Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse) - { + public function __construct( + IPresenterFactory $presenterFactory, + Router $router, + Nette\Http\IRequest $httpRequest, + Nette\Http\IResponse $httpResponse + ) { $this->httpRequest = $httpRequest; $this->httpResponse = $httpResponse; $this->presenterFactory = $presenterFactory; @@ -138,14 +142,19 @@ public function processRequest(Request $request): void $this->requests[] = $request; $this->onRequest($this, $request); - if (!$request->isMethod($request::FORWARD) && !strcasecmp($request->getPresenterName(), (string) $this->errorPresenter)) { + if ( + !$request->isMethod($request::FORWARD) + && !strcasecmp($request->getPresenterName(), (string) $this->errorPresenter) + ) { throw new BadRequestException('Invalid request. Presenter is not achievable.'); } try { $this->presenter = $this->presenterFactory->createPresenter($request->getPresenterName()); } catch (InvalidPresenterException $e) { - throw count($this->requests) > 1 ? $e : new BadRequestException($e->getMessage(), 0, $e); + throw count($this->requests) > 1 + ? $e + : new BadRequestException($e->getMessage(), 0, $e); } $this->onPresenter($this, $this->presenter); $response = $this->presenter->run(clone $request); diff --git a/src/Application/LinkGenerator.php b/src/Application/LinkGenerator.php index 7738e1404..e51f7837d 100644 --- a/src/Application/LinkGenerator.php +++ b/src/Application/LinkGenerator.php @@ -52,7 +52,9 @@ public function link(string $dest, array $params = []): string [, $presenter, $action, $frag] = $m; try { - $class = $this->presenterFactory ? $this->presenterFactory->getPresenterClass($presenter) : null; + $class = $this->presenterFactory + ? $this->presenterFactory->getPresenterClass($presenter) + : null; } catch (InvalidPresenterException $e) { throw new UI\InvalidLinkException($e->getMessage(), 0, $e); } diff --git a/src/Application/MicroPresenter.php b/src/Application/MicroPresenter.php index 9a0061460..c71867689 100644 --- a/src/Application/MicroPresenter.php +++ b/src/Application/MicroPresenter.php @@ -37,8 +37,11 @@ final class MicroPresenter implements Application\IPresenter private $request; - public function __construct(Nette\DI\Container $context = null, Http\IRequest $httpRequest = null, Router $router = null) - { + public function __construct( + Nette\DI\Container $context = null, + Http\IRequest $httpRequest = null, + Router $router = null + ) { $this->context = $context; $this->httpRequest = $httpRequest; $this->router = $router; @@ -58,7 +61,12 @@ public function run(Application\Request $request): Application\IResponse { $this->request = $request; - if ($this->httpRequest && $this->router && !$this->httpRequest->isAjax() && ($request->isMethod('get') || $request->isMethod('head'))) { + if ( + $this->httpRequest + && $this->router + && !$this->httpRequest->isAjax() + && ($request->isMethod('get') || $request->isMethod('head')) + ) { $refUrl = $this->httpRequest->getUrl()->withoutUserInfo(); $url = $this->router->constructUrl($request->toArray(), $refUrl); if ($url !== null && !$refUrl->isEqual($url)) { @@ -113,8 +121,12 @@ public function run(Application\Request $request): Application\IResponse */ public function createTemplate(string $class = null, callable $latteFactory = null): Application\UI\ITemplate { - $latte = $latteFactory ? $latteFactory() : $this->getContext()->getByType(Nette\Bridges\ApplicationLatte\ILatteFactory::class)->create(); - $template = $class ? new $class : new Nette\Bridges\ApplicationLatte\Template($latte); + $latte = $latteFactory + ? $latteFactory() + : $this->getContext()->getByType(Nette\Bridges\ApplicationLatte\ILatteFactory::class)->create(); + $template = $class + ? new $class + : new Nette\Bridges\ApplicationLatte\Template($latte); $template->setParameters($this->request->getParameters()); $template->presenter = $this; diff --git a/src/Application/Request.php b/src/Application/Request.php index 79f0a5c76..f87cb13a7 100644 --- a/src/Application/Request.php +++ b/src/Application/Request.php @@ -59,8 +59,14 @@ final class Request /** * @param string $name presenter name (module:module:presenter) */ - public function __construct(string $name, string $method = null, array $params = [], array $post = [], array $files = [], array $flags = []) - { + public function __construct( + string $name, + string $method = null, + array $params = [], + array $post = [], + array $files = [], + array $flags = [] + ) { $this->name = $name; $this->method = $method; $this->params = $params; diff --git a/src/Application/Responses/FileResponse.php b/src/Application/Responses/FileResponse.php index 0eaebb87e..d8f18cc1e 100644 --- a/src/Application/Responses/FileResponse.php +++ b/src/Application/Responses/FileResponse.php @@ -35,8 +35,12 @@ final class FileResponse implements Nette\Application\IResponse private $forceDownload; - public function __construct(string $file, string $name = null, string $contentType = null, bool $forceDownload = true) - { + public function __construct( + string $file, + string $name = null, + string $contentType = null, + bool $forceDownload = true + ) { if (!is_file($file) || !is_readable($file)) { throw new Nette\Application\BadRequestException("File '$file' doesn't exist or is not readable."); } @@ -81,10 +85,12 @@ public function getContentType(): string public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse): void { $httpResponse->setContentType($this->contentType); - $httpResponse->setHeader('Content-Disposition', + $httpResponse->setHeader( + 'Content-Disposition', ($this->forceDownload ? 'attachment' : 'inline') . '; filename="' . $this->name . '"' - . '; filename*=utf-8\'\'' . rawurlencode($this->name)); + . '; filename*=utf-8\'\'' . rawurlencode($this->name) + ); $filesize = $length = filesize($this->file); $handle = fopen($this->file, 'r'); diff --git a/src/Application/Routers/CliRouter.php b/src/Application/Routers/CliRouter.php index cd7216bb3..541539ee5 100644 --- a/src/Application/Routers/CliRouter.php +++ b/src/Application/Routers/CliRouter.php @@ -64,13 +64,15 @@ public function match(Nette\Http\IRequest $httpRequest): ?array $flag = null; } - if ($opt !== '') { - $pair = explode('=', $opt, 2); - if (isset($pair[1])) { - $params[$pair[0]] = $pair[1]; - } else { - $flag = $pair[0]; - } + if ($opt === '') { + continue; + } + + $pair = explode('=', $opt, 2); + if (isset($pair[1])) { + $params[$pair[0]] = $pair[1]; + } else { + $flag = $pair[0]; } } diff --git a/src/Application/Routers/Route.php b/src/Application/Routers/Route.php index 5cca47530..9c5d428dc 100644 --- a/src/Application/Routers/Route.php +++ b/src/Application/Routers/Route.php @@ -25,18 +25,18 @@ class Route extends Nette\Routing\Route implements Nette\Application\IRouter private const UI_META = [ 'module' => [ self::PATTERN => '[a-z][a-z0-9.-]*', - self::FILTER_IN => [__CLASS__, 'path2presenter'], - self::FILTER_OUT => [__CLASS__, 'presenter2path'], + self::FILTER_IN => [self::class, 'path2presenter'], + self::FILTER_OUT => [self::class, 'presenter2path'], ], 'presenter' => [ self::PATTERN => '[a-z][a-z0-9.-]*', - self::FILTER_IN => [__CLASS__, 'path2presenter'], - self::FILTER_OUT => [__CLASS__, 'presenter2path'], + self::FILTER_IN => [self::class, 'path2presenter'], + self::FILTER_OUT => [self::class, 'presenter2path'], ], 'action' => [ self::PATTERN => '[a-z][a-z0-9-]*', - self::FILTER_IN => [__CLASS__, 'path2action'], - self::FILTER_OUT => [__CLASS__, 'action2path'], + self::FILTER_IN => [self::class, 'path2action'], + self::FILTER_OUT => [self::class, 'action2path'], ], ]; @@ -69,7 +69,7 @@ public function __construct(string $mask, $metadata = [], int $flags = 0) ]; } - $this->defaultMeta = $this->defaultMeta + self::UI_META; + $this->defaultMeta += self::UI_META; if (self::$styles) { trigger_error('Route::$styles is deprecated.', E_USER_DEPRECATED); array_replace_recursive($this->defaultMeta, self::$styles); @@ -118,11 +118,10 @@ public function constructUrl(array $params, Nette\Http\UrlScript $refUrl): ?stri if (isset($metadata[self::MODULE_KEY])) { // try split into module and [submodule:]presenter parts $presenter = $params[self::PRESENTER_KEY]; $module = $metadata[self::MODULE_KEY]; - if (isset($module['fixity'], $module[self::VALUE]) && strncmp($presenter, $module[self::VALUE] . ':', strlen($module[self::VALUE]) + 1) === 0) { - $a = strlen($module[self::VALUE]); - } else { - $a = strrpos($presenter, ':'); - } + $a = isset($module['fixity'], $module[self::VALUE]) + && strncmp($presenter, $module[self::VALUE] . ':', strlen($module[self::VALUE]) + 1) === 0 + ? strlen($module[self::VALUE]) + : strrpos($presenter, ':'); if ($a === false) { $params[self::MODULE_KEY] = isset($module[self::VALUE]) ? '' : null; } else { diff --git a/src/Application/Routers/RouteList.php b/src/Application/Routers/RouteList.php index 5fdcfe93c..adbfe1f87 100644 --- a/src/Application/Routers/RouteList.php +++ b/src/Application/Routers/RouteList.php @@ -51,11 +51,11 @@ public function match(Nette\Http\IRequest $httpRequest): ?array public function constructUrl(array $params, Nette\Http\UrlScript $refUrl): ?string { if ($this->module) { - if (strncmp($params[self::PRESENTER_KEY], $this->module, strlen($this->module)) === 0) { - $params[self::PRESENTER_KEY] = substr($params[self::PRESENTER_KEY], strlen($this->module)); - } else { + if (strncmp($params[self::PRESENTER_KEY], $this->module, strlen($this->module)) !== 0) { return null; } + + $params[self::PRESENTER_KEY] = substr($params[self::PRESENTER_KEY], strlen($this->module)); } return parent::constructUrl($params, $refUrl); diff --git a/src/Application/UI/Component.php b/src/Application/UI/Component.php index 4723692ad..1ca428df7 100644 --- a/src/Application/UI/Component.php +++ b/src/Application/UI/Component.php @@ -203,7 +203,9 @@ final public function getParameterId(string $name): string final public function getParam($name = null, $default = null) { trigger_error(__METHOD__ . '() is deprecated; use getParameter() or getParameters() instead.', E_USER_DEPRECATED); - return func_num_args() ? $this->getParameter($name, $default) : $this->getParameters(); + return func_num_args() + ? $this->getParameter($name, $default) + : $this->getParameters(); } @@ -217,7 +219,7 @@ final public function getParam($name = null, $default = null) public function signalReceived(string $signal): void { if (!$this->tryCall($this->formatSignalMethod($signal), $this->params)) { - $class = get_class($this); + $class = static::class; throw new BadSignalException("There is no handler for signal '$signal' in class $class."); } } @@ -244,7 +246,9 @@ public static function formatSignalMethod(string $signal): string public function link(string $destination, $args = []): string { try { - $args = func_num_args() < 3 && is_array($args) ? $args : array_slice(func_get_args(), 1); + $args = func_num_args() < 3 && is_array($args) + ? $args + : array_slice(func_get_args(), 1); return $this->getPresenter()->createRequest($this, $destination, $args, 'link'); } catch (InvalidLinkException $e) { @@ -260,7 +264,9 @@ public function link(string $destination, $args = []): string */ public function lazyLink(string $destination, $args = []): Link { - $args = func_num_args() < 3 && is_array($args) ? $args : array_slice(func_get_args(), 1); + $args = func_num_args() < 3 && is_array($args) + ? $args + : array_slice(func_get_args(), 1); return new Link($this, $destination, $args); } @@ -274,7 +280,9 @@ public function lazyLink(string $destination, $args = []): Link public function isLinkCurrent(string $destination = null, $args = []): bool { if ($destination !== null) { - $args = func_num_args() < 3 && is_array($args) ? $args : array_slice(func_get_args(), 1); + $args = func_num_args() < 3 && is_array($args) + ? $args + : array_slice(func_get_args(), 1); $this->getPresenter()->createRequest($this, $destination, $args, 'test'); } return $this->getPresenter()->getLastCreatedRequestFlag('current'); @@ -297,7 +305,9 @@ public function redirect(/*int $code, string */$destination, $args = []): void } } else { $code = null; - $args = func_num_args() < 3 && is_array($args) ? $args : array_slice(func_get_args(), 1); + $args = func_num_args() < 3 && is_array($args) + ? $args + : array_slice(func_get_args(), 1); } $presenter = $this->getPresenter(); @@ -313,7 +323,9 @@ public function redirect(/*int $code, string */$destination, $args = []): void */ public function redirectPermanent(string $destination, $args = []): void { - $args = func_num_args() < 3 && is_array($args) ? $args : array_slice(func_get_args(), 1); + $args = func_num_args() < 3 && is_array($args) + ? $args + : array_slice(func_get_args(), 1); $presenter = $this->getPresenter(); $presenter->redirectUrl( $presenter->createRequest($this, $destination, $args, 'redirect'), diff --git a/src/Application/UI/ComponentReflection.php b/src/Application/UI/ComponentReflection.php index e0a2d19e0..df74becfc 100644 --- a/src/Application/UI/ComponentReflection.php +++ b/src/Application/UI/ComponentReflection.php @@ -38,7 +38,7 @@ final class ComponentReflection extends \ReflectionClass */ public function getPersistentParams(string $class = null): array { - $class = $class === null ? $this->getName() : $class; + $class = $class ?? $this->getName(); $params = &self::$ppCache[$class]; if ($params !== null) { return $params; @@ -71,7 +71,7 @@ public function getPersistentParams(string $class = null): array public function getPersistentComponents(string $class = null): array { - $class = $class === null ? $this->getName() : $class; + $class = $class ?? $this->getName(); $components = &self::$pcCache[$class]; if ($components !== null) { return $components; @@ -242,7 +242,9 @@ public static function parseAnnotation(\Reflector $ref, string $name): ?array $res = []; foreach ($m[1] as $s) { foreach (preg_split('#\s*,\s*#', $s, -1, PREG_SPLIT_NO_EMPTY) ?: ['true'] as $item) { - $res[] = array_key_exists($tmp = strtolower($item), $tokens) ? $tokens[$tmp] : $item; + $res[] = array_key_exists($tmp = strtolower($item), $tokens) + ? $tokens[$tmp] + : $item; } } return $res; diff --git a/src/Application/UI/Control.php b/src/Application/UI/Control.php index da20357ee..e955f5e41 100644 --- a/src/Application/UI/Control.php +++ b/src/Application/UI/Control.php @@ -92,7 +92,7 @@ public function flashMessage($message, string $type = 'info'): \stdClass public function redrawControl(string $snippet = null, bool $redraw = true): void { if ($redraw) { - $this->invalidSnippets[$snippet === null ? "\0" : $snippet] = true; + $this->invalidSnippets[$snippet ?? "\0"] = true; } elseif ($snippet === null) { $this->invalidSnippets = []; @@ -108,32 +108,29 @@ public function redrawControl(string $snippet = null, bool $redraw = true): void */ public function isControlInvalid(string $snippet = null): bool { - if ($snippet === null) { - if (count($this->invalidSnippets) > 0) { - return true; - - } else { - $queue = [$this]; - do { - foreach (array_shift($queue)->getComponents() as $component) { - if ($component instanceof IRenderable) { - if ($component->isControlInvalid()) { - // $this->invalidSnippets['__child'] = true; // as cache - return true; - } - - } elseif ($component instanceof Nette\ComponentModel\IContainer) { - $queue[] = $component; - } + if ($snippet !== null) { + return $this->invalidSnippets[$snippet] ?? isset($this->invalidSnippets["\0"]); + + } elseif (count($this->invalidSnippets) > 0) { + return true; + } + + $queue = [$this]; + do { + foreach (array_shift($queue)->getComponents() as $component) { + if ($component instanceof IRenderable) { + if ($component->isControlInvalid()) { + // $this->invalidSnippets['__child'] = true; // as cache + return true; } - } while ($queue); - return false; + } elseif ($component instanceof Nette\ComponentModel\IContainer) { + $queue[] = $component; + } } + } while ($queue); - } else { - return $this->invalidSnippets[$snippet] ?? isset($this->invalidSnippets["\0"]); - } + return false; } diff --git a/src/Application/UI/Form.php b/src/Application/UI/Form.php index 9c821062c..2e7ceca03 100644 --- a/src/Application/UI/Form.php +++ b/src/Application/UI/Form.php @@ -124,11 +124,9 @@ protected function receiveHttpData(): ?array return null; } - if ($this->isMethod('post')) { - return Nette\Utils\Arrays::mergeTree($request->getPost(), $request->getFiles()); - } else { - return $request->getParameters(); - } + return $this->isMethod('post') + ? Nette\Utils\Arrays::mergeTree($request->getPost(), $request->getFiles()) + : $request->getParameters(); } @@ -152,7 +150,7 @@ protected function beforeRender() public function signalReceived(string $signal): void { if ($signal !== 'submit') { - $class = get_class($this); + $class = static::class; throw new BadSignalException("Missing handler for signal '$signal' in $class."); } elseif ($this->sameSiteProtection && !$this->getPresenter()->getHttpRequest()->isSameSite()) { diff --git a/src/Application/UI/Presenter.php b/src/Application/UI/Presenter.php index a5702b810..48c043fa4 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -349,7 +349,9 @@ public function processSignal(): void return; } - $component = $this->signalReceiver === '' ? $this : $this->getComponent($this->signalReceiver, false); + $component = $this->signalReceiver === '' + ? $this + : $this->getComponent($this->signalReceiver, false); if ($component === null) { throw new BadSignalException("The signal receiver component '$this->signalReceiver' is not found."); @@ -378,7 +380,9 @@ final public function getSignal(): ?array final public function isSignalReceiver($component, $signal = null): bool { if ($component instanceof Nette\ComponentModel\Component) { - $component = $component === $this ? '' : $component->lookupPath(__CLASS__, true); + $component = $component === $this + ? '' + : $component->lookupPath(self::class, true); } if ($this->signal === null) { @@ -390,10 +394,8 @@ final public function isSignalReceiver($component, $signal = null): bool } elseif ($signal === null) { return $this->signalReceiver === $component; - - } else { - return $this->signalReceiver === $component && strcasecmp($signal, $this->signal) === 0; } + return $this->signalReceiver === $component && strcasecmp($signal, $this->signal) === 0; } @@ -405,7 +407,9 @@ final public function isSignalReceiver($component, $signal = null): bool */ final public function getAction(bool $fullyQualified = false): string { - return $fullyQualified ? ':' . $this->getName() . ':' . $this->action : $this->action; + return $fullyQualified + ? ':' . $this->getName() . ':' . $this->action + : $this->action; } @@ -651,7 +655,9 @@ public function forward($destination, $args = []): void $this->sendResponse(new Responses\ForwardResponse($destination)); } - $args = func_num_args() < 3 && is_array($args) ? $args : array_slice(func_get_args(), 1); + $args = func_num_args() < 3 && is_array($args) + ? $args + : array_slice(func_get_args(), 1); $this->createRequest($this, $destination, $args, 'forward'); $this->sendResponse(new Responses\ForwardResponse($this->lastCreatedRequest)); } @@ -703,21 +709,27 @@ final public function getLastCreatedRequestFlag(string $flag): bool public function canonicalize(string $destination = null, array $args = []): void { $request = $this->request; - if (!$this->isAjax() && ($request->isMethod('get') || $request->isMethod('head'))) { - try { - $url = $this->createRequest( - $this, - $destination ?: $this->action, - $args + $this->getGlobalState() + $request->getParameters(), - 'redirectX' - ); - } catch (InvalidLinkException $e) { - } - if (isset($url) && !$this->httpRequest->getUrl()->withoutUserInfo()->isEqual($url)) { - $code = $request->hasFlag($request::VARYING) ? Http\IResponse::S302_FOUND : Http\IResponse::S301_MOVED_PERMANENTLY; - $this->sendResponse(new Responses\RedirectResponse($url, $code)); - } + if ($this->isAjax() || (!$request->isMethod('get') && !$request->isMethod('head'))) { + return; } + + try { + $url = $this->createRequest( + $this, + $destination ?: $this->action, + $args + $this->getGlobalState() + $request->getParameters(), + 'redirectX' + ); + } catch (InvalidLinkException $e) { + } + if (!isset($url) || $this->httpRequest->getUrl()->withoutUserInfo()->isEqual($url)) { + return; + } + + $code = $request->hasFlag($request::VARYING) + ? Http\IResponse::S302_FOUND + : Http\IResponse::S301_MOVED_PERMANENTLY; + $this->sendResponse(new Responses\RedirectResponse($url, $code)); } @@ -748,8 +760,12 @@ public function lastModified($lastModified, string $etag = null, string $expire * @throws InvalidLinkException * @internal */ - final protected function createRequest(Component $component, string $destination, array $args, string $mode): ?string - { + final protected function createRequest( + Component $component, + string $destination, + array $args, + string $mode + ): ?string { // note: createRequest supposes that saveState(), run() & tryCall() behaviour is final $this->lastCreatedRequest = $this->lastCreatedRequestFlag = null; @@ -774,7 +790,7 @@ final protected function createRequest(Component $component, string $destination if ($presenter === '') { $action = $path === 'this' ? $this->action : $action; $presenter = $this->getName(); - $presenterClass = get_class($this); + $presenterClass = static::class; } else { if ($presenter[0] === ':') { // absolute @@ -830,12 +846,12 @@ final protected function createRequest(Component $component, string $destination } // PROCESS ARGUMENTS - if (is_subclass_of($presenterClass, __CLASS__)) { + if (is_subclass_of($presenterClass, self::class)) { if ($action === '') { $action = self::DEFAULT_ACTION; } - $current = ($action === '*' || strcasecmp($action, (string) $this->action) === 0) && $presenterClass === get_class($this); + $current = ($action === '*' || strcasecmp($action, (string) $this->action) === 0) && $presenterClass === static::class; $reflection = new ComponentReflection($presenterClass); @@ -953,7 +969,7 @@ protected function requestToUrl(Application\Request $request, bool $relative = n throw new InvalidLinkException("No route for {$request->getPresenterName()}:{$request->getParameter('action')}($params)"); } - if ($relative === null ? !$this->absoluteUrls : $relative) { + if ($relative ?? !$this->absoluteUrls) { $hostUrl = $this->refUrlCache->getHostUrl() . '/'; if (strncmp($url, $hostUrl, strlen($hostUrl)) === 0) { $url = substr($url, strlen($hostUrl) - 1); @@ -970,8 +986,13 @@ protected function requestToUrl(Application\Request $request, bool $relative = n * @throws InvalidLinkException * @internal */ - public static function argsToParams(string $class, string $method, array &$args, array $supplemental = [], array &$missing = null): void - { + public static function argsToParams( + string $class, + string $method, + array &$args, + array $supplemental = [], + array &$missing = null + ): void { $i = 0; $rm = new \ReflectionMethod($class, $method); foreach ($rm->getParameters() as $param) { @@ -991,7 +1012,13 @@ public static function argsToParams(string $class, string $method, array &$args, } if (!isset($args[$name])) { - if (!$param->isDefaultValueAvailable() && !$param->allowsNull() && $type !== 'NULL' && $type !== 'array' && $type !== 'iterable') { + if ( + !$param->isDefaultValueAvailable() + && !$param->allowsNull() + && $type !== 'NULL' + && $type !== 'array' + && $type !== 'iterable' + ) { $missing[] = $param; unset($args[$name]); } @@ -1008,7 +1035,9 @@ public static function argsToParams(string $class, string $method, array &$args, )); } - $def = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null; + $def = $param->isDefaultValueAvailable() + ? $param->getDefaultValue() + : null; if ($args[$name] === $def || ($def === null && $args[$name] === '')) { $args[$name] = null; // value transmit is unnecessary } @@ -1295,9 +1324,16 @@ public function getFlashSession(): Http\SessionSection /********************* services ****************d*g**/ - final public function injectPrimary(?Nette\DI\Container $context, ?Application\IPresenterFactory $presenterFactory, ?Nette\Routing\Router $router, - Http\IRequest $httpRequest, Http\IResponse $httpResponse, ?Http\Session $session = null, ?Nette\Security\User $user = null, ?ITemplateFactory $templateFactory = null) - { + final public function injectPrimary( + ?Nette\DI\Container $context, + ?Application\IPresenterFactory $presenterFactory, + ?Nette\Routing\Router $router, + Http\IRequest $httpRequest, + Http\IResponse $httpResponse, + ?Http\Session $session = null, + ?Nette\Security\User $user = null, + ?ITemplateFactory $templateFactory = null + ) { if ($this->presenterFactory !== null) { throw new Nette\InvalidStateException('Method ' . __METHOD__ . ' is intended for initialization and should not be called more than once.'); } @@ -1346,7 +1382,9 @@ final public function getSession(string $namespace = null) if (!$this->session) { throw new Nette\InvalidStateException('Service Session has not been set.'); } - return $namespace === null ? $this->session : $this->session->getSection($namespace); + return $namespace === null + ? $this->session + : $this->session->getSection($namespace); } diff --git a/src/Bridges/ApplicationDI/ApplicationExtension.php b/src/Bridges/ApplicationDI/ApplicationExtension.php index 7fa000764..f48e348d6 100644 --- a/src/Bridges/ApplicationDI/ApplicationExtension.php +++ b/src/Bridges/ApplicationDI/ApplicationExtension.php @@ -38,8 +38,12 @@ final class ApplicationExtension extends Nette\DI\CompilerExtension private $tempDir; - public function __construct(bool $debugMode = false, array $scanDirs = null, string $tempDir = null, Nette\Loaders\RobotLoader $robotLoader = null) - { + public function __construct( + bool $debugMode = false, + array $scanDirs = null, + string $tempDir = null, + Nette\Loaders\RobotLoader $robotLoader = null + ) { $this->debugMode = $debugMode; $this->scanDirs = (array) $scanDirs; $this->tempDir = $tempDir; @@ -87,7 +91,8 @@ public function loadConfiguration() $presenterFactory = $builder->addDefinition($this->prefix('presenterFactory')) ->setType(Nette\Application\IPresenterFactory::class) ->setFactory(Nette\Application\PresenterFactory::class, [new Definitions\Statement( - Nette\Bridges\ApplicationDI\PresenterFactoryCallback::class, [1 => $this->invalidLinkMode, $touch ?? null] + Nette\Bridges\ApplicationDI\PresenterFactoryCallback::class, + [1 => $this->invalidLinkMode, $touch ?? null] )]); if ($config->mapping) { diff --git a/src/Bridges/ApplicationDI/LatteExtension.php b/src/Bridges/ApplicationDI/LatteExtension.php index 892d432a9..8be00173b 100644 --- a/src/Bridges/ApplicationDI/LatteExtension.php +++ b/src/Bridges/ApplicationDI/LatteExtension.php @@ -33,10 +33,13 @@ public function __construct(string $tempDir, bool $debugMode = false) $this->config = new class { /** @var bool */ public $xhtml = false; + /** @var string[] */ public $macros = []; + /** @var ?string */ public $templateClass; + /** @var bool */ public $strictTypes = false; }; diff --git a/src/Bridges/ApplicationDI/RoutingExtension.php b/src/Bridges/ApplicationDI/RoutingExtension.php index 597c841d2..033a2ce9e 100644 --- a/src/Bridges/ApplicationDI/RoutingExtension.php +++ b/src/Bridges/ApplicationDI/RoutingExtension.php @@ -30,10 +30,13 @@ public function __construct(bool $debugMode = false) $this->config = new class { /** @var ?bool */ public $debugger; + /** @var string[] */ public $routes = []; + /** @var ?string */ public $routeClass = Nette\Application\Routers\Route::class; + /** @var bool */ public $cache = false; }; diff --git a/src/Bridges/ApplicationLatte/Template.php b/src/Bridges/ApplicationLatte/Template.php index 76d586523..67e53d28e 100644 --- a/src/Bridges/ApplicationLatte/Template.php +++ b/src/Bridges/ApplicationLatte/Template.php @@ -110,7 +110,9 @@ public function addFunction(string $name, callable $callback) public function setTranslator(?Nette\Localization\ITranslator $translator) { $this->latte->addFilter('translate', function (Latte\Runtime\FilterInfo $fi, ...$args) use ($translator): string { - return $translator === null ? $args[0] : $translator->translate(...$args); + return $translator === null + ? $args[0] + : $translator->translate(...$args); }); return $this; } diff --git a/src/Bridges/ApplicationLatte/TemplateFactory.php b/src/Bridges/ApplicationLatte/TemplateFactory.php index 5f9b25be9..d8c0646bd 100644 --- a/src/Bridges/ApplicationLatte/TemplateFactory.php +++ b/src/Bridges/ApplicationLatte/TemplateFactory.php @@ -40,9 +40,13 @@ class TemplateFactory implements UI\ITemplateFactory private $templateClass; - public function __construct(ILatteFactory $latteFactory, Nette\Http\IRequest $httpRequest = null, - Nette\Security\User $user = null, Nette\Caching\IStorage $cacheStorage = null, $templateClass = null) - { + public function __construct( + ILatteFactory $latteFactory, + Nette\Http\IRequest $httpRequest = null, + Nette\Security\User $user = null, + Nette\Caching\IStorage $cacheStorage = null, + $templateClass = null + ) { $this->latteFactory = $latteFactory; $this->httpRequest = $httpRequest; $this->user = $user; @@ -91,7 +95,9 @@ public function createTemplate(UI\Control $control = null): UI\ITemplate trigger_error('Filter |null is deprecated.', E_USER_DEPRECATED); }); $latte->addFilter('modifyDate', function ($time, $delta, $unit = null) { - return $time == null ? null : Nette\Utils\DateTime::from($time)->modify($delta . $unit); // intentionally == + return $time == null // intentionally == + ? null + : Nette\Utils\DateTime::from($time)->modify($delta . $unit); }); if (!isset($latte->getFilters()['translate'])) { @@ -107,8 +113,12 @@ public function createTemplate(UI\Control $control = null): UI\ITemplate // default parameters $template->user = $this->user; - $template->baseUrl = $this->httpRequest ? rtrim($this->httpRequest->getUrl()->withoutUserInfo()->getBaseUrl(), '/') : null; - $template->basePath = $this->httpRequest ? preg_replace('#https?://[^/]+#A', '', $template->baseUrl) : null; + $template->baseUrl = $this->httpRequest + ? rtrim($this->httpRequest->getUrl()->withoutUserInfo()->getBaseUrl(), '/') + : null; + $template->basePath = $this->httpRequest + ? preg_replace('#https?://[^/]+#A', '', $template->baseUrl) + : null; $template->flashes = []; if ($control) { $template->control = $control; diff --git a/src/Bridges/ApplicationLatte/UIMacros.php b/src/Bridges/ApplicationLatte/UIMacros.php index c0222de3d..1e82b3f29 100644 --- a/src/Bridges/ApplicationLatte/UIMacros.php +++ b/src/Bridges/ApplicationLatte/UIMacros.php @@ -80,7 +80,9 @@ public function macroControl(MacroNode $node, PhpWriter $writer) } $name = $writer->formatWord($words[0]); $method = ucfirst($words[1] ?? ''); - $method = Strings::match($method, '#^\w*$#D') ? "render$method" : "{\"render$method\"}"; + $method = Strings::match($method, '#^\w*$#D') + ? "render$method" + : "{\"render$method\"}"; $tokens = $node->tokenizer; $pos = $tokens->position; @@ -115,7 +117,8 @@ public function macroLink(MacroNode $node, PhpWriter $writer) { $node->modifiers = preg_replace('#\|safeurl\s*(?=\||$)#Di', '', $node->modifiers); return $writer->using($node, $this->getCompiler()) - ->write('echo %escape(%modify(' + ->write( + 'echo %escape(%modify(' . ($node->name === 'plink' ? '$this->global->uiPresenter' : '$this->global->uiControl') . '->link(%node.word, %node.array?)))' ); @@ -130,9 +133,10 @@ public function macroIfCurrent(MacroNode $node, PhpWriter $writer) if ($node->modifiers) { throw new CompileException('Modifiers are not allowed in ' . $node->getNotation()); } - return $writer->write($node->args - ? 'if ($this->global->uiPresenter->isLinkCurrent(%node.word, %node.array?)) {' - : 'if ($this->global->uiPresenter->getLastCreatedRequestFlag("current")) {' + return $writer->write( + $node->args + ? 'if ($this->global->uiPresenter->isLinkCurrent(%node.word, %node.array?)) {' + : 'if ($this->global->uiPresenter->getLastCreatedRequestFlag("current")) {' ); } diff --git a/src/Bridges/ApplicationTracy/RoutingPanel.php b/src/Bridges/ApplicationTracy/RoutingPanel.php index 6518b00c0..f46a327d5 100644 --- a/src/Bridges/ApplicationTracy/RoutingPanel.php +++ b/src/Bridges/ApplicationTracy/RoutingPanel.php @@ -55,8 +55,11 @@ public static function initializePanel(Nette\Application\Application $applicatio } - public function __construct(Routing\Router $router, Nette\Http\IRequest $httpRequest, Nette\Application\IPresenterFactory $presenterFactory) - { + public function __construct( + Routing\Router $router, + Nette\Http\IRequest $httpRequest, + Nette\Application\IPresenterFactory $presenterFactory + ) { $this->router = $router; $this->httpRequest = $httpRequest; $this->presenterFactory = $presenterFactory; @@ -96,12 +99,16 @@ public function getPanel(): string /** * Analyses simple route. */ - private function analyse(Routing\Router $router, string $module = '', bool $parentMatches = true, int $level = -1): void - { + private function analyse( + Routing\Router $router, + string $module = '', + bool $parentMatches = true, + int $level = -1 + ): void { if ($router instanceof Routing\RouteList) { try { $parentMatches = $parentMatches && $router->match($this->httpRequest) !== null; - } catch (\Exception $e) { + } catch (\Throwable $e) { } $next = count($this->routers); $parentModule = $module . ($router instanceof Nette\Application\Routers\RouteList ? $router->getModule() : ''); @@ -121,8 +128,10 @@ private function analyse(Routing\Router $router, string $module = '', bool $pare $matched = 'no'; $params = $e = null; try { - $params = $parentMatches ? $router->match($this->httpRequest) : null; - } catch (\Exception $e) { + $params = $parentMatches + ? $router->match($this->httpRequest) + : null; + } catch (\Throwable $e) { $matched = 'error'; } if ($params !== null) { @@ -174,6 +183,8 @@ private function findSource(): void } } - $this->source = isset($method) && $rc->hasMethod($method) ? $rc->getMethod($method) : $rc; + $this->source = isset($method) && $rc->hasMethod($method) + ? $rc->getMethod($method) + : $rc; } } diff --git a/tests/Bridges.DI/LatteExtension.basic.phpt b/tests/Bridges.DI/LatteExtension.basic.phpt index 6adc602c0..8a8092bc0 100644 --- a/tests/Bridges.DI/LatteExtension.basic.phpt +++ b/tests/Bridges.DI/LatteExtension.basic.phpt @@ -62,7 +62,7 @@ class NonStaticMacrosFactory { $macros = new Latte\Macros\MacroSet($compiler); $macros->addMacro('foo', 'foo ' . $this->parameter); - Notes::add(get_class($this) . '::install'); + Notes::add(static::class . '::install'); } @@ -70,7 +70,7 @@ class NonStaticMacrosFactory { $macros = new Latte\Macros\MacroSet($compiler); $macros->addMacro('foo2', 'foo ' . $this->parameter); - Notes::add(get_class($this) . '::create'); + Notes::add(static::class . '::create'); } } diff --git a/tests/Bridges.Latte/UIMacros.isLinkCurrent.phpt b/tests/Bridges.Latte/UIMacros.isLinkCurrent.phpt index 23bc3c157..eb3b85e40 100644 --- a/tests/Bridges.Latte/UIMacros.isLinkCurrent.phpt +++ b/tests/Bridges.Latte/UIMacros.isLinkCurrent.phpt @@ -30,7 +30,7 @@ $factory->createTemplate($presenter); $latte->setLoader(new Latte\Loaders\StringLoader); Assert::matchFile(__DIR__ . '/expected/UIMacros.isLinkCurrent.phtml', $latte->compile( -'n:href before n:class + 'n:href before n:class n:href after n:class @@ -43,4 +43,5 @@ Assert::matchFile(__DIR__ . '/expected/UIMacros.isLinkCurrent.phtml', $latte->co {ifCurrent default}default{/ifCurrent} custom function -')); +' +)); diff --git a/tests/Routers/Route.php b/tests/Routers/Route.php index 8420cc3dc..58e883d39 100644 --- a/tests/Routers/Route.php +++ b/tests/Routers/Route.php @@ -32,7 +32,9 @@ function testRouteIn(Nette\Routing\Router $route, string $url, array $expectedPa unset($params['extra']); $result = $route->constructUrl($params, $url); - $result = $result && !strncmp($result, 'http://example.com', 18) ? substr($result, 18) : $result; + $result = $result && !strncmp($result, 'http://example.com', 18) + ? substr($result, 18) + : $result; Assert::same($expectedUrl, $result); } } diff --git a/tests/Routers/RouteList.addRoute.phpt b/tests/Routers/RouteList.addRoute.phpt index 538921ea1..57703b365 100644 --- a/tests/Routers/RouteList.addRoute.phpt +++ b/tests/Routers/RouteList.addRoute.phpt @@ -20,7 +20,9 @@ testRouteIn($list, '/foo', ['presenter' => 'foo', 'test' => 'testvalue']); testRouteIn($list, '/bar', ['presenter' => 'bar', 'test' => 'testvalue']); -testRouteIn($list, '/hello', +testRouteIn( + $list, + '/hello', ['presenter' => 'hello', 'test' => 'testvalue'], '/hello?test=testvalue' ); diff --git a/tests/Routers/RouteList.modules.phpt b/tests/Routers/RouteList.modules.phpt index 185e9121f..d6086a333 100644 --- a/tests/Routers/RouteList.modules.phpt +++ b/tests/Routers/RouteList.modules.phpt @@ -27,7 +27,9 @@ $list[] = new Route('[/]', [ 'action' => 'default', ]); -testRouteIn($list, '/auth/', +testRouteIn( + $list, + '/auth/', [ 'presenter' => 'Auth:Homepage', 'action' => 'default', diff --git a/tests/Routers/RouteList.withModule.phpt b/tests/Routers/RouteList.withModule.phpt index dce158f48..f9344cce5 100644 --- a/tests/Routers/RouteList.withModule.phpt +++ b/tests/Routers/RouteList.withModule.phpt @@ -22,17 +22,23 @@ $list ->addRoute('hello', ['presenter' => 'hello']); -testRouteIn($list, '/foo', +testRouteIn( + $list, + '/foo', ['presenter' => 'A:foo', 'test' => 'testvalue'], '/foo?test=testvalue' ); -testRouteIn($list, '/bar', +testRouteIn( + $list, + '/bar', ['presenter' => 'A:B:bar', 'test' => 'testvalue'], '/bar?test=testvalue' ); -testRouteIn($list, '/hello', +testRouteIn( + $list, + '/hello', ['presenter' => 'C:hello', 'test' => 'testvalue'], '/hello?test=testvalue' ); diff --git a/tests/UI/ComponentReflection.combineArgs.phpt b/tests/UI/ComponentReflection.combineArgs.phpt index d972d3151..b0e79704e 100644 --- a/tests/UI/ComponentReflection.combineArgs.phpt +++ b/tests/UI/ComponentReflection.combineArgs.phpt @@ -24,8 +24,13 @@ class MyPresenter } - public function hintsNulls(int $int = null, bool $bool = null, string $str = null, array $arr = null, iterable $iter = null) - { + public function hintsNulls( + int $int = null, + bool $bool = null, + string $str = null, + array $arr = null, + iterable $iter = null + ) { } @@ -34,8 +39,13 @@ class MyPresenter } - public function hintsDefaults(int $int = 0, bool $bool = false, string $str = '', array $arr = [], iterable $iter = []) - { + public function hintsDefaults( + int $int = 0, + bool $bool = false, + string $str = '', + array $arr = [], + iterable $iter = [] + ) { } diff --git a/tests/UI/Presenter.formatLayoutTemplateFiles.phpt b/tests/UI/Presenter.formatLayoutTemplateFiles.phpt index 261147500..fcde28dc4 100644 --- a/tests/UI/Presenter.formatLayoutTemplateFiles.phpt +++ b/tests/UI/Presenter.formatLayoutTemplateFiles.phpt @@ -62,7 +62,7 @@ test('with module & without subdir templates', function () { __DIR__ . '/templates/Two.@layout.latte', __DIR__ . '/templates/@layout.latte', dirname(__DIR__) . '/templates/@layout.latte', - dirname(dirname(__DIR__)) . '/templates/@layout.latte', + dirname(__DIR__, 2) . '/templates/@layout.latte', ], $presenter->formatLayoutTemplateFiles()); }); diff --git a/tests/UI/Presenter.isModuleCurrent().phpt b/tests/UI/Presenter.isModuleCurrent().phpt index 1ac2f4295..59ac53d57 100644 --- a/tests/UI/Presenter.isModuleCurrent().phpt +++ b/tests/UI/Presenter.isModuleCurrent().phpt @@ -4,7 +4,7 @@ * Test: Presenter::isModuleCurrent. */ -declare(strict_types = 1); +declare(strict_types=1); use Tester\Assert; diff --git a/tests/UI/Presenter.link().74.phptx b/tests/UI/Presenter.link().php74 similarity index 100% rename from tests/UI/Presenter.link().74.phptx rename to tests/UI/Presenter.link().php74 diff --git a/tests/UI/Presenter.storeRequest().phpt b/tests/UI/Presenter.storeRequest().phpt index b7cc73e41..d32861228 100644 --- a/tests/UI/Presenter.storeRequest().phpt +++ b/tests/UI/Presenter.storeRequest().phpt @@ -33,8 +33,10 @@ class MockSession extends Http\Session } - public function getSection(string $section, string $class = Nette\Http\SessionSection::class): Nette\Http\SessionSection - { + public function getSection( + string $section, + string $class = Nette\Http\SessionSection::class + ): Nette\Http\SessionSection { return $this->testSection; } } From d2471134ed909210de8a3e8559931902b1bee67b Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 23 Oct 2020 14:54:16 +0200 Subject: [PATCH 10/10] added PHP 8 attributes Persistent & CrossOrigin --- src/Application/Attributes/CrossOrigin.php | 18 ++++++++ src/Application/Attributes/Persistent.php | 18 ++++++++ src/Application/UI/Component.php | 1 + src/Application/UI/ComponentReflection.php | 5 ++- src/Application/UI/Presenter.php | 8 +++- .../UI/Presenter.getPersistentComponents.phpt | 41 +++++++++++++++++++ tests/UI/Presenter.link().persistent.phpt | 13 ++++++ 7 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 src/Application/Attributes/CrossOrigin.php create mode 100644 src/Application/Attributes/Persistent.php create mode 100644 tests/UI/Presenter.getPersistentComponents.phpt diff --git a/src/Application/Attributes/CrossOrigin.php b/src/Application/Attributes/CrossOrigin.php new file mode 100644 index 000000000..6ddf9e4dd --- /dev/null +++ b/src/Application/Attributes/CrossOrigin.php @@ -0,0 +1,18 @@ +getName(), 0, 6) === 'handle' && !ComponentReflection::parseAnnotation($element, 'crossOrigin') + && (PHP_VERSION_ID < 80000 || !$element->getAttributes(Nette\Application\Attributes\CrossOrigin::class)) && !$this->getPresenter()->getHttpRequest()->isSameSite() ) { $this->getPresenter()->detectedCsrf(); diff --git a/src/Application/UI/ComponentReflection.php b/src/Application/UI/ComponentReflection.php index df74becfc..8a7270f26 100644 --- a/src/Application/UI/ComponentReflection.php +++ b/src/Application/UI/ComponentReflection.php @@ -49,7 +49,10 @@ public function getPersistentParams(string $class = null): array $defaults = get_class_vars($class); foreach ($defaults as $name => $default) { $rp = new \ReflectionProperty($class, $name); - if (!$rp->isStatic() && self::parseAnnotation($rp, 'persistent')) { + if (!$rp->isStatic() + && ((PHP_VERSION_ID >= 80000 && $rp->getAttributes(Nette\Application\Attributes\Persistent::class)) + || self::parseAnnotation($rp, 'persistent')) + ) { $params[$name] = [ 'def' => $default, 'type' => Nette\Utils\Reflection::getPropertyType($rp) ?: gettype($default), diff --git a/src/Application/UI/Presenter.php b/src/Application/UI/Presenter.php index 48c043fa4..f3a12f4dc 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -1118,7 +1118,13 @@ public function restoreRequest(string $key): void */ public static function getPersistentComponents(): array { - return (array) ComponentReflection::parseAnnotation(new \ReflectionClass(static::class), 'persistent'); + $rc = new \ReflectionClass(static::class); + $attrs = PHP_VERSION_ID >= 80000 + ? $rc->getAttributes(Application\Attributes\Persistent::class) + : null; + return $attrs + ? $attrs[0]->getArguments() + : (array) ComponentReflection::parseAnnotation($rc, 'persistent'); } diff --git a/tests/UI/Presenter.getPersistentComponents.phpt b/tests/UI/Presenter.getPersistentComponents.phpt new file mode 100644 index 000000000..773e680f4 --- /dev/null +++ b/tests/UI/Presenter.getPersistentComponents.phpt @@ -0,0 +1,41 @@ += 80000) { + Assert::same(['a', 'b'], ThreePresenter::getPersistentComponents()); +} diff --git a/tests/UI/Presenter.link().persistent.phpt b/tests/UI/Presenter.link().persistent.phpt index 7e25ebb12..bc2fb9515 100644 --- a/tests/UI/Presenter.link().persistent.phpt +++ b/tests/UI/Presenter.link().persistent.phpt @@ -95,6 +95,13 @@ class ThirdPresenter extends BasePresenter } +class FourthPresenter extends BasePresenter +{ + #[Application\Attributes\Persistent] + public $p1; +} + + Assert::same([ 'p1' => ['def' => null, 'type' => 'NULL', 'since' => 'BasePresenter'], 't1' => ['def' => null, 'type' => 'NULL', 'since' => 'PersistentParam1'], @@ -120,6 +127,12 @@ Assert::same([ 't2' => ['def' => null, 'type' => 'NULL', 'since' => 'PersistentParam2A'], ], ThirdPresenter::getReflection()->getPersistentParams()); +if (PHP_VERSION_ID >= 80000) { + Assert::same([ + 'p1' => ['def' => null, 'type' => 'NULL', 'since' => 'BasePresenter'], + 't1' => ['def' => null, 'type' => 'NULL', 'since' => 'PersistentParam1'], + ], FourthPresenter::getReflection()->getPersistentParams()); +} $url = new Http\UrlScript('http://localhost/index.php', '/index.php');