From 41935796a9b51b953101d694f36fdc118cddd474 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Sat, 27 Dec 2025 09:03:21 +0900 Subject: [PATCH 01/13] =?UTF-8?q?refactor:=20=E3=82=A2=E3=83=8E=E3=83=86?= =?UTF-8?q?=E3=83=BC=E3=82=B7=E3=83=A7=E3=83=B3=E3=82=92=20PHP=208=20Attri?= =?UTF-8?q?butes=20=E3=81=AB=E7=A7=BB=E8=A1=8C=E3=81=97=E4=BE=9D=E5=AD=98?= =?UTF-8?q?=E9=96=A2=E4=BF=82=E3=82=92=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 12 +++++++++--- src/AbstractForm.php | 7 ++----- src/SetAntiCsrfTrait.php | 7 ++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/composer.json b/composer.json index 37ad5fd..e36bd59 100644 --- a/composer.json +++ b/composer.json @@ -6,15 +6,16 @@ "Ray.Di module" ], "require": { - "php": ">=7.0.0", - "ray/di": "^2.7", + "php": ">=8.0.0", + "ray/di": "^2.16", + "ray/aop": "^2.14", "aura/input": "^1.2", "aura/filter": "^2.3|3.x-dev", "aura/html": "^2.5", "ray/aura-session-module": "^1.1" }, "require-dev": { - "phpunit/phpunit": "^5.7.13" + "phpunit/phpunit": "^9.5" }, "license": "MIT", "autoload":{ @@ -32,5 +33,10 @@ "coverage": ["php -dzend_extension=xdebug.so ./vendor/bin/phpunit --coverage-text --coverage-html=build/coverage"], "cs": ["php-cs-fixer fix -v --dry-run", "phpcs --standard=./phpcs.xml src"], "cs-fix": ["php-cs-fixer fix -v", "phpcbf src"] + }, + "config": { + "allow-plugins": { + "aura/installer-default": true + } } } diff --git a/src/AbstractForm.php b/src/AbstractForm.php index 1c109ab..71a7734 100644 --- a/src/AbstractForm.php +++ b/src/AbstractForm.php @@ -72,9 +72,8 @@ public function __toString() * @param BuilderInterface $builder * @param FilterFactory $filterFactory * @param HelperLocatorFactory $helperFactory - * - * @\Ray\Di\Di\Inject */ + #[\Ray\Di\Di\Inject] public function setBaseDependencies( BuilderInterface $builder, FilterFactory $filterFactory, @@ -90,9 +89,7 @@ public function setAntiCsrf(AntiCsrfInterface $antiCsrf) $this->antiCsrf = $antiCsrf; } - /** - * @\Ray\Di\Di\PostConstruct - */ + #[\Ray\Di\Di\PostConstruct] public function postConstruct() { $this->init(); diff --git a/src/SetAntiCsrfTrait.php b/src/SetAntiCsrfTrait.php index 2d7b289..bd256f5 100644 --- a/src/SetAntiCsrfTrait.php +++ b/src/SetAntiCsrfTrait.php @@ -10,11 +10,8 @@ trait SetAntiCsrfTrait { - /** - * @param AntiCsrfInterface $antiCsrf - * - * @\Ray\Di\Di\Inject - */ + /** @param AntiCsrfInterface $antiCsrf */ + #[\Ray\Di\Di\Inject] public function setAntiCsrf(AntiCsrfInterface $antiCsrf) { $this->antiCsrf = $antiCsrf; From d90abb98f97f3f033da98e5b58911002d7fde5a4 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Wed, 31 Dec 2025 10:11:50 +0900 Subject: [PATCH 02/13] =?UTF-8?q?docs:=20README=20=E3=81=AE=E3=82=B5?= =?UTF-8?q?=E3=83=B3=E3=83=97=E3=83=AB=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92?= =?UTF-8?q?=20PHP=208=20Attributes=20=E5=BD=A2=E5=BC=8F=E3=81=AB=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=20migrate-attribute?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.JA.md | 24 ++++++------------------ README.md | 20 +++++--------------- src/Annotation/AbstractValidation.php | 7 +++---- src/Annotation/FormValidation.php | 7 +++---- src/Annotation/InputValidation.php | 7 +++---- src/Annotation/VndError.php | 7 +++---- 6 files changed, 23 insertions(+), 49 deletions(-) diff --git a/README.JA.md b/README.JA.md index b3acce9..29d83ef 100644 --- a/README.JA.md +++ b/README.JA.md @@ -95,18 +95,14 @@ class MyController */ protected $contactForm; - /** - * @Inject - * @Named("contact_form") - */ + #[Inject] + #[Named("contact_form")] public function setForm(FormInterface $form) { $this->contactForm = $form; } - /** - * @FormValidation(form="contactForm", onFailure="badRequestAction") - */ + #[FormValidation(form: "contactForm", onFailure: "badRequestAction")] public function createAction() { // validation success @@ -151,9 +147,7 @@ use Ray\WebFormModule\Annotation\InputValidation; class Foo { - /** - * @InputValidation(form="form1") - */ + #[InputValidation(form: "form1")] public function createAction($name) { // ... @@ -193,14 +187,8 @@ echo $e->error; `@VndError`アノテーションで`vnd.error+json`に必要な情報を加えることができます。 ```php - /** - * @FormValidation(form="contactForm") - * @VndError( - * message="foo validation failed", - * logref="a1000", path="/path/to/error", - * href={"_self"="/path/to/error", "help"="/path/to/help"} - * ) - */ + #[FormValidation(form: "contactForm")] + #[VndError(message: "foo validation failed", logref: "a1000", path: "/path/to/error", href: ["_self" => "/path/to/error", "help" => "/path/to/help"])] ``` このオプションのモジュールはAPIアプリケーションの時に有用です。 diff --git a/README.md b/README.md index 2265909..49c7aa1 100644 --- a/README.md +++ b/README.md @@ -103,18 +103,14 @@ class MyController */ protected $contactForm; - /** - * @Inject - * @Named("contact_form") - */ + #[Inject] + #[Named("contact_form")] public function setForm(FormInterface $form) { $this->contactForm = $form; } - /** - * @FormValidation(form="contactForm", onFailure="badRequestAction") - */ + #[FormValidation(form: "contactForm", onFailure: "badRequestAction")] public function createAction() { // validation success @@ -186,14 +182,8 @@ echo $e->error; More detail for `vnd.error+json`can be add with `@VndError` annotation. ```php - /** - * @FormValidation(form="contactForm") - * @VndError( - * message="foo validation failed", - * logref="a1000", path="/path/to/error", - * href={"_self"="/path/to/error", "help"="/path/to/help"} - * ) - */ + #[FormValidation(form: "contactForm")] + #[VndError(message: "foo validation failed", logref: "a1000", path: "/path/to/error", href: ["_self" => "/path/to/error", "help" => "/path/to/help"])] ``` This optional module is handy for API application. diff --git a/src/Annotation/AbstractValidation.php b/src/Annotation/AbstractValidation.php index 3cf5e2f..75c5de3 100644 --- a/src/Annotation/AbstractValidation.php +++ b/src/Annotation/AbstractValidation.php @@ -6,10 +6,9 @@ */ namespace Ray\WebFormModule\Annotation; -/** - * @Annotation - * @Target("METHOD") - */ +use Attribute; + +#[Attribute(Attribute::TARGET_METHOD)] class AbstractValidation { /** diff --git a/src/Annotation/FormValidation.php b/src/Annotation/FormValidation.php index 2640582..1700c70 100644 --- a/src/Annotation/FormValidation.php +++ b/src/Annotation/FormValidation.php @@ -6,10 +6,9 @@ */ namespace Ray\WebFormModule\Annotation; -/** - * @Annotation - * @Target("METHOD") - */ +use Attribute; + +#[Attribute(Attribute::TARGET_METHOD)] final class FormValidation extends AbstractValidation { /** diff --git a/src/Annotation/InputValidation.php b/src/Annotation/InputValidation.php index 424e84d..d10948a 100644 --- a/src/Annotation/InputValidation.php +++ b/src/Annotation/InputValidation.php @@ -6,10 +6,9 @@ */ namespace Ray\WebFormModule\Annotation; -/** - * @Annotation - * @Target("METHOD") - */ +use Attribute; + +#[Attribute(Attribute::TARGET_METHOD)] final class InputValidation extends AbstractValidation { } diff --git a/src/Annotation/VndError.php b/src/Annotation/VndError.php index 8acdcde..fd9a5a9 100644 --- a/src/Annotation/VndError.php +++ b/src/Annotation/VndError.php @@ -6,10 +6,9 @@ */ namespace Ray\WebFormModule\Annotation; -/** - * @Annotation - * @Target("METHOD") - */ +use Attribute; + +#[Attribute(Attribute::TARGET_METHOD)] final class VndError { /** From e192dc415f6738967307492ff63b62b88b866537 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Tue, 6 Jan 2026 14:15:10 +0900 Subject: [PATCH 03/13] =?UTF-8?q?test:=20PHPUnit=20=E8=A8=AD=E5=AE=9A?= =?UTF-8?q?=E3=82=92=E6=9C=80=E6=96=B0=E5=BD=A2=E5=BC=8F=E3=81=AB=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E3=81=97=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=83=89=E3=82=92=20PHP=208=20=E5=AF=BE=E5=BF=9C=E3=81=AB?= =?UTF-8?q?=E6=94=B9=E5=96=84=20migrate-attribute?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++ phpunit.xml.dist | 22 ++++++----- src/Annotation/AbstractValidation.php | 8 ++-- src/Annotation/FormValidation.php | 19 ++++------ src/Annotation/VndError.php | 40 +++++--------------- src/AuraInputModule.php | 4 +- tests/AbstractAuraFormTest.php | 6 ++- tests/AbstractFormTest.php | 16 ++++---- tests/AntiCsrfTest.php | 5 ++- tests/AuraInputInterceptorTest.php | 27 ++++++------- tests/AuraInputModuleTest.php | 5 ++- tests/Fake/FakeController.php | 12 ++---- tests/Fake/FakeControllerVndError.php | 17 ++------- tests/Fake/FakeInputValidationController.php | 10 ++--- tests/Fake/FakeInvalidController1.php | 4 +- tests/Fake/FakeInvalidController2.php | 4 +- tests/Fake/FakeInvalidController3.php | 4 +- tests/Fake/FakeInvalidInstanceController.php | 4 +- tests/FormFactoryTest.php | 6 ++- tests/VndErrorHandlerTest.php | 7 ++-- tests/bootstrap.php | 6 +-- 21 files changed, 94 insertions(+), 135 deletions(-) diff --git a/.gitignore b/.gitignore index 59370d4..b47a5dd 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,6 @@ build vendor/ composer.phar composer.lock +tests/tmp/* +!tests/tmp/.placefolder +.phpunit.result.cache diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0c37fb9..ca6b708 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,19 +1,21 @@ - + tests + + + src + + + + + + + - - - + - - - - src - - diff --git a/src/Annotation/AbstractValidation.php b/src/Annotation/AbstractValidation.php index 75c5de3..18743a5 100644 --- a/src/Annotation/AbstractValidation.php +++ b/src/Annotation/AbstractValidation.php @@ -4,6 +4,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule\Annotation; use Attribute; @@ -11,8 +12,7 @@ #[Attribute(Attribute::TARGET_METHOD)] class AbstractValidation { - /** - * @var string - */ - public $form = 'form'; + public function __construct(public string $form = 'form') + { + } } diff --git a/src/Annotation/FormValidation.php b/src/Annotation/FormValidation.php index 1700c70..8497a1a 100644 --- a/src/Annotation/FormValidation.php +++ b/src/Annotation/FormValidation.php @@ -4,6 +4,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule\Annotation; use Attribute; @@ -11,15 +12,11 @@ #[Attribute(Attribute::TARGET_METHOD)] final class FormValidation extends AbstractValidation { - /** - * @var bool - */ - public $antiCsrf = false; - - /** - * Method name on validation failed. - * - * @var string - */ - public $onFailure; + public function __construct( + string $form = 'form', + public bool $antiCsrf = false, + public string|null $onFailure = null + ) { + parent::__construct($form); + } } diff --git a/src/Annotation/VndError.php b/src/Annotation/VndError.php index fd9a5a9..12a85f2 100644 --- a/src/Annotation/VndError.php +++ b/src/Annotation/VndError.php @@ -4,6 +4,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule\Annotation; use Attribute; @@ -12,40 +13,17 @@ final class VndError { /** - * @var string - * - * REQUIRED - */ - public $message; - - /** - * @var array - * - * REQUIRED - */ - public $href; - - /** - * @var string - * - * OPTIONAL - */ - public $logref; - - /** - * @var string - * - * OPTIONAL - * - * help + * @param array $href * * @see http://www.w3.org/TR/html5/links.html#link-type-help - * - * about * @see http://tools.ietf.org/html/rfc6903#section-2 - * - * describes * @see http://tools.ietf.org/html/rfc6892 */ - public $path; + public function __construct( + public string $message, + public array $href, + public string|null $logref = null, + public string|null $path = null + ) { + } } diff --git a/src/AuraInputModule.php b/src/AuraInputModule.php index 738c346..c1e0cd7 100644 --- a/src/AuraInputModule.php +++ b/src/AuraInputModule.php @@ -13,8 +13,8 @@ use Aura\Input\BuilderInterface; use Aura\Input\Filter; use Aura\Input\FilterInterface; -use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\Reader; +use Koriym\Attributes\AttributeReader; use Ray\AuraSessionModule\AuraSessionModule; use Ray\Di\AbstractModule; use Ray\Di\Scope; @@ -29,7 +29,7 @@ class AuraInputModule extends AbstractModule protected function configure() { $this->install(new AuraSessionModule); - $this->bind(Reader::class)->to(AnnotationReader::class)->in(Scope::SINGLETON); + $this->bind(Reader::class)->to(AttributeReader::class)->in(Scope::SINGLETON); $this->bind(BuilderInterface::class)->to(Builder::class); $this->bind(FilterInterface::class)->to(Filter::class); $this->bind(AntiCsrfInterface::class)->to(AntiCsrf::class)->in(Scope::SINGLETON); diff --git a/tests/AbstractAuraFormTest.php b/tests/AbstractAuraFormTest.php index d11bdd7..16862dd 100644 --- a/tests/AbstractAuraFormTest.php +++ b/tests/AbstractAuraFormTest.php @@ -6,14 +6,16 @@ */ namespace Ray\WebFormModule; -class AbstractAuraFormTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class AbstractAuraFormTest extends TestCase { /** * @var AbstractForm */ private $form; - public function setUp() + public function setUp(): void { parent::setUp(); $this->form = (new FormFactory)->newInstance(FakeForm::class); diff --git a/tests/AbstractFormTest.php b/tests/AbstractFormTest.php index d87ad54..973f553 100644 --- a/tests/AbstractFormTest.php +++ b/tests/AbstractFormTest.php @@ -11,18 +11,20 @@ use Aura\Session\Randval; use Aura\Session\SegmentFactory; use Aura\Session\Session; -use Doctrine\Common\Annotations\AnnotationReader; +use Koriym\Attributes\AttributeReader; +use PHPUnit\Framework\TestCase; use Ray\Aop\ReflectiveMethodInvocation; use Ray\WebFormModule\Exception\CsrfViolationException; +use Ray\WebFormModule\Exception\ValidationException; -class AbstractFormTest extends \PHPUnit_Framework_TestCase +class AbstractFormTest extends TestCase { /** * @var AbstractForm */ private $form; - public function setUp() + public function setUp(): void { parent::setUp(); $this->form = (new FormFactory)->newInstance(FakeMiniForm::class); @@ -39,12 +41,12 @@ public function getMethodInvocation(array $arguments) $controller = new FakeController; $controller->setForm($fakeForm); // interceptor - $reader = new AnnotationReader; + $reader = new AttributeReader; $interceptor = new AuraInputInterceptor($reader, new VndErrorHandler($reader)); return new ReflectiveMethodInvocation( $controller, - new \ReflectionMethod($controller, 'createAction'), + 'createAction', $arguments, [ $interceptor @@ -61,7 +63,7 @@ public function testApply() public function testSubmit() { - $this->expectException(\ReflectionException::class); + $this->expectException(ValidationException::class); $invocation = $this->getMethodInvocation(['na']); $invocation->proceed(); } @@ -89,7 +91,7 @@ public function testGetItelator() public function testAntiCsrfViolation() { - $this->setExpectedException(CsrfViolationException::class); + $this->expectException(CsrfViolationException::class); $session = new Session( new SegmentFactory, new CsrfTokenFactory(new Randval(new Phpfunc)), diff --git a/tests/AntiCsrfTest.php b/tests/AntiCsrfTest.php index a20bc94..ebb4297 100644 --- a/tests/AntiCsrfTest.php +++ b/tests/AntiCsrfTest.php @@ -14,8 +14,9 @@ use Aura\Session\Randval; use Aura\Session\SegmentFactory; use Aura\Session\Session; +use PHPUnit\Framework\TestCase; -class AntiCsrfTest extends \PHPUnit_Framework_TestCase +class AntiCsrfTest extends TestCase { private $phpfunc; @@ -29,7 +30,7 @@ class AntiCsrfTest extends \PHPUnit_Framework_TestCase */ private $session; - protected function setUp() + protected function setUp(): void { $this->phpfunc = new FakePhpfunc; $this->session = $this->newSession(); diff --git a/tests/AuraInputInterceptorTest.php b/tests/AuraInputInterceptorTest.php index f153f5b..16e58f6 100644 --- a/tests/AuraInputInterceptorTest.php +++ b/tests/AuraInputInterceptorTest.php @@ -6,15 +6,14 @@ */ namespace Ray\WebFormModule; -use Doctrine\Common\Annotations\AnnotationReader; -use Ray\Aop\ReflectiveMethodInvocation; +use PHPUnit\Framework\TestCase; use Ray\Di\AbstractModule; use Ray\Di\Injector; use Ray\Di\InjectorInterface; use Ray\WebFormModule\Exception\InvalidFormPropertyException; use Ray\WebFormModule\Exception\ValidationException; -class AuraInputInterceptorTest extends \PHPUnit_Framework_TestCase +class AuraInputInterceptorTest extends TestCase { /** * @var InjectorInterface @@ -26,7 +25,7 @@ class AuraInputInterceptorTest extends \PHPUnit_Framework_TestCase */ private $controller; - public function setUp() + public function setUp(): void { $this->injector = new Injector(new class() extends AbstractModule { protected function configure() @@ -104,21 +103,21 @@ public function invalidControllerProvider() public function testInvalidFormPropertyByMissingProperty() { - $this->setExpectedException(InvalidFormPropertyException::class); + $this->expectException(InvalidFormPropertyException::class); $controller = $this->injector->getInstance(FakeInvalidController1::class); $controller->createAction(); } public function testInvalidFormPropertyByMissingProperty2() { - $this->setExpectedException(InvalidFormPropertyException::class); + $this->expectException(InvalidFormPropertyException::class); $controller = $this->injector->getInstance(FakeInvalidController2::class); $controller->createAction(); } public function testInvalidFormPropertyException() { - $this->setExpectedException(InvalidFormPropertyException::class); + $this->expectException(InvalidFormPropertyException::class); /** @var FakeInvalidController3 $controller */ $controller = $this->injector->getInstance(FakeInvalidController3::class); $controller->createAction(''); @@ -126,24 +125,26 @@ public function testInvalidFormPropertyException() public function testInvalidFormPropertyByInvalidInstance() { - $this->setExpectedException(InvalidFormPropertyException::class); - $this->setExpectedException(InvalidFormPropertyException::class); + $this->expectException(InvalidFormPropertyException::class); + $this->expectException(InvalidFormPropertyException::class); $controller = $this->injector->getInstance(FakeInvalidController1::class); $controller->createAction(''); } public function testProceedWithVndErrorHandler() { - /** @var FakeController $controller */ - $controller = $this->injector->getInstance(FakeController::class); + /** @var FakeControllerVndError $controller */ + $controller = $this->injector->getInstance(FakeControllerVndError::class); try { $controller->createAction(''); + $this->fail('ValidationException should be thrown'); } catch (ValidationException $e) { $this->assertInstanceOf(FormValidationError::class, $e->error); $json = (string) $e->error; $this->assertSame('{ - "message": "Validation failed", - "path": "", + "message": "foo validation failed", + "path": "/path/to/error", + "logref": "a1000", "validation_messages": { "name": [ "Name must be alphabetic only." diff --git a/tests/AuraInputModuleTest.php b/tests/AuraInputModuleTest.php index 2d51b2b..47add84 100644 --- a/tests/AuraInputModuleTest.php +++ b/tests/AuraInputModuleTest.php @@ -6,11 +6,12 @@ */ namespace Ray\WebFormModule; +use PHPUnit\Framework\TestCase; use Ray\Aop\WeavedInterface; use Ray\Di\Injector; use Ray\WebFormModule\Exception\ValidationException; -class AuraInputModuleTest extends \PHPUnit_Framework_TestCase +class AuraInputModuleTest extends TestCase { public function testAuraInputModule() { @@ -21,7 +22,7 @@ public function testAuraInputModule() public function testExceptionOnFailure() { - $this->setExpectedException(ValidationException::class); + $this->expectException(ValidationException::class); $injector = new Injector(new FakeModule, __DIR__ . '/tmp'); /** @var $controller FakeInputValidationController */ $controller = $injector->getInstance(FakeInputValidationController::class); diff --git a/tests/Fake/FakeController.php b/tests/Fake/FakeController.php index d642e52..9484301 100644 --- a/tests/Fake/FakeController.php +++ b/tests/Fake/FakeController.php @@ -13,20 +13,14 @@ class FakeController */ protected $form; - /** - * @Inject - * @Named("contact_form") - */ + #[Inject] + #[Named('contact_form')] public function setForm(FormInterface $form) { $this->form = $form; } - /** - * @FormValidation - * - * = is same as @ FormValidation(form="form", onFailure="createActionValidationFailed") - */ + #[FormValidation] public function createAction($name) { return '201'; diff --git a/tests/Fake/FakeControllerVndError.php b/tests/Fake/FakeControllerVndError.php index 3f44ab5..d952c87 100644 --- a/tests/Fake/FakeControllerVndError.php +++ b/tests/Fake/FakeControllerVndError.php @@ -14,24 +14,15 @@ class FakeControllerVndError */ protected $form1; - /** - * @Inject - * @Named("contact_form") - */ + #[Inject] + #[Named('contact_form')] public function setForm(FormInterface $form) { $this->form1 = $form; } - /** - * @InputValidation(form="form1") - * @VndError( - * message="foo validation failed", - * logref="a1000", - * path="/path/to/error", - * href={"_self"="/path/to/error", "help"="/path/to/help"} - * ) - */ + #[InputValidation(form: 'form1')] + #[VndError(message: 'foo validation failed', href: ['_self' => '/path/to/error', 'help' => '/path/to/help'], logref: 'a1000', path: '/path/to/error')] public function createAction($name) { } diff --git a/tests/Fake/FakeInputValidationController.php b/tests/Fake/FakeInputValidationController.php index 9619276..162ad83 100644 --- a/tests/Fake/FakeInputValidationController.php +++ b/tests/Fake/FakeInputValidationController.php @@ -13,18 +13,14 @@ class FakeInputValidationController */ protected $form; - /** - * @Inject - * @Named("contact_form") - */ + #[Inject] + #[Named('contact_form')] public function setForm(FormInterface $form) { $this->form = $form; } - /** - * @InputValidation - */ + #[InputValidation] public function createAction($name) { } diff --git a/tests/Fake/FakeInvalidController1.php b/tests/Fake/FakeInvalidController1.php index 32aa3dc..122fb70 100644 --- a/tests/Fake/FakeInvalidController1.php +++ b/tests/Fake/FakeInvalidController1.php @@ -6,9 +6,7 @@ class FakeInvalidController1 { - /** - * @FormValidation(form="missing") - */ + #[FormValidation(form: "missing")] public function createAction() { } diff --git a/tests/Fake/FakeInvalidController2.php b/tests/Fake/FakeInvalidController2.php index 9f6bb47..711449c 100644 --- a/tests/Fake/FakeInvalidController2.php +++ b/tests/Fake/FakeInvalidController2.php @@ -8,9 +8,7 @@ class FakeInvalidController2 { private $form = null; - /** - * @FormValidation - */ + #[FormValidation] public function createAction() { } diff --git a/tests/Fake/FakeInvalidController3.php b/tests/Fake/FakeInvalidController3.php index 8322619..be70fac 100644 --- a/tests/Fake/FakeInvalidController3.php +++ b/tests/Fake/FakeInvalidController3.php @@ -13,9 +13,7 @@ public function setForm(FormInterface $form) $this->form = $form; } - /** - * @FormValidation(onFailure="missing_method") - */ + #[FormValidation(onFailure: "missing_method")] public function createAction($name) { } diff --git a/tests/Fake/FakeInvalidInstanceController.php b/tests/Fake/FakeInvalidInstanceController.php index d64b9c6..5f93b9d 100644 --- a/tests/Fake/FakeInvalidInstanceController.php +++ b/tests/Fake/FakeInvalidInstanceController.php @@ -8,9 +8,7 @@ class FakeInvalidInstanceController { private $form; - /** - * @FormValidation - */ + #[FormValidation] public function createAction() { } diff --git a/tests/FormFactoryTest.php b/tests/FormFactoryTest.php index 07451e3..c8ae411 100644 --- a/tests/FormFactoryTest.php +++ b/tests/FormFactoryTest.php @@ -6,14 +6,16 @@ */ namespace Ray\WebFormModule; -class FormFactoryTest extends \PHPUnit_Framework_TestCase +use PHPUnit\Framework\TestCase; + +class FormFactoryTest extends TestCase { /** * @var FormFactory */ private $factory; - public function setUp() + public function setUp(): void { parent::setUp(); $this->factory = new FormFactory; diff --git a/tests/VndErrorHandlerTest.php b/tests/VndErrorHandlerTest.php index 80519d7..2b96fb8 100644 --- a/tests/VndErrorHandlerTest.php +++ b/tests/VndErrorHandlerTest.php @@ -6,17 +6,18 @@ */ namespace Ray\WebFormModule; +use PHPUnit\Framework\TestCase; use Ray\Di\Injector; use Ray\WebFormModule\Exception\ValidationException; -class VndErrorHandlerTest extends \PHPUnit_Framework_TestCase +class VndErrorHandlerTest extends TestCase { /** * @var FakeController */ private $controller; - public function setUp() + public function setUp(): void { parent::setUp(); $this->controller = (new Injector(new FakeVndErrorModule, __DIR__ . '/tmp'))->getInstance(FakeController::class); @@ -24,7 +25,7 @@ public function setUp() public function testValidationException() { - $this->setExpectedException(ValidationException::class); + $this->expectException(ValidationException::class); $this->controller->createAction(''); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 29ca915..adacf3f 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -4,11 +4,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ -use Doctrine\Common\Annotations\AnnotationRegistry; - -/* @var $loader \Composer\Autoload\ClassLoader */ -$loader = require dirname(__DIR__) . '/vendor/autoload.php'; -AnnotationRegistry::registerLoader([$loader, 'loadClass']); +require dirname(__DIR__) . '/vendor/autoload.php'; $handler = new \Ray\WebFormModule\FakeSessionHandler(); session_set_save_handler( From d6e5bba386493f4bbbdaeb10989026d239a11b79 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Tue, 6 Jan 2026 14:19:08 +0900 Subject: [PATCH 04/13] =?UTF-8?q?ci:=20Scrutinizer=20=E8=A8=AD=E5=AE=9A?= =?UTF-8?q?=E3=81=8B=E3=82=89=20`php=5Fmess=5Fdetector`=20=E3=82=92?= =?UTF-8?q?=E7=84=A1=E5=8A=B9=E5=8C=96=20migrate-attribute?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .scrutinizer.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 0ca2b93..39fbeb5 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -5,7 +5,6 @@ tools: php_code_coverage: timeout: 1200 php_sim: true - php_mess_detector: true php_pdepend: true php_analyzer: true php_cpd: true From f4e605affb3489d65822eb6f83b373577ba7cab9 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Tue, 6 Jan 2026 14:38:57 +0900 Subject: [PATCH 05/13] =?UTF-8?q?docs:=20README=20=E3=81=AE=20`VndError`?= =?UTF-8?q?=20=E8=A1=A8=E8=A8=98=E3=82=92=20Attributes=20=E5=BD=A2?= =?UTF-8?q?=E5=BC=8F=E3=81=AB=E6=9B=B4=E6=96=B0=20migrate-attribute?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.JA.md | 2 +- README.md | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.JA.md b/README.JA.md index 29d83ef..c96343d 100644 --- a/README.JA.md +++ b/README.JA.md @@ -184,7 +184,7 @@ echo $e->error; //} ``` -`@VndError`アノテーションで`vnd.error+json`に必要な情報を加えることができます。 +`#[VndError]`属性で`vnd.error+json`に必要な情報を加えることができます。 ```php #[FormValidation(form: "contactForm")] diff --git a/README.md b/README.md index 49c7aa1..b07cc87 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ class MyController public function createAction() { // validation success + // More detail for `vnd.error+json` can be added with `#[VndError]`. } public function badRequestAction() From 7bcb17db9cfbc475a1c6246ecc0e17baa17fc10d Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Wed, 7 Jan 2026 12:01:08 +0900 Subject: [PATCH 06/13] =?UTF-8?q?ci:=20Scrutinizer=20=E8=A8=AD=E5=AE=9A?= =?UTF-8?q?=E3=82=92=E7=B0=A1=E7=B4=A0=E5=8C=96=E3=81=97=20PHP=208.0=20?= =?UTF-8?q?=E3=83=93=E3=83=AB=E3=83=89=E7=92=B0=E5=A2=83=E3=81=AB=E7=A7=BB?= =?UTF-8?q?=E8=A1=8C=20migrate-attribute?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .scrutinizer.yml | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 39fbeb5..db26de6 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -1,18 +1,12 @@ +build: + image: default-jammy + environment: + php: 8.0 + nodes: + analysis: + tests: + override: + - php-scrutinizer-run + filter: paths: ["src/*"] -tools: - external_code_coverage: true - php_code_coverage: - timeout: 1200 - php_sim: true - php_pdepend: true - php_analyzer: true - php_cpd: true - php_mess_detector: - enabled: true - config: - ruleset: ./phpmd.xml - php_code_sniffer: - enabled: true - config: - ruleset: ./phpcs.xml From 67df167606bf0ebb453b32791b5470a5469124b9 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Wed, 7 Jan 2026 12:06:53 +0900 Subject: [PATCH 07/13] =?UTF-8?q?test:=20`AuraInputInterceptorTest`=20?= =?UTF-8?q?=E3=81=AE=E9=87=8D=E8=A4=87=E3=81=97=E3=81=9F=20`expectExceptio?= =?UTF-8?q?n`=20=E5=91=BC=E3=81=B3=E5=87=BA=E3=81=97=E3=82=92=E5=89=8A?= =?UTF-8?q?=E9=99=A4=20migrate-attribute?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/AuraInputInterceptorTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/AuraInputInterceptorTest.php b/tests/AuraInputInterceptorTest.php index 16e58f6..8b2df09 100644 --- a/tests/AuraInputInterceptorTest.php +++ b/tests/AuraInputInterceptorTest.php @@ -125,7 +125,6 @@ public function testInvalidFormPropertyException() public function testInvalidFormPropertyByInvalidInstance() { - $this->expectException(InvalidFormPropertyException::class); $this->expectException(InvalidFormPropertyException::class); $controller = $this->injector->getInstance(FakeInvalidController1::class); $controller->createAction(''); From 47c4b506cf4db4617e0ecc927c8817255c60c781 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Wed, 7 Jan 2026 12:07:52 +0900 Subject: [PATCH 08/13] =?UTF-8?q?ci:=20Scrutinizer=20=E3=81=AE=20PHP=20?= =?UTF-8?q?=E3=83=90=E3=83=BC=E3=82=B8=E3=83=A7=E3=83=B3=E3=82=92=208.2=20?= =?UTF-8?q?=E3=81=AB=E6=9B=B4=E6=96=B0=E3=81=97=E8=A8=AD=E5=AE=9A=E6=A7=8B?= =?UTF-8?q?=E9=80=A0=E3=82=92=E6=9C=80=E9=81=A9=E5=8C=96=20migrate-attribu?= =?UTF-8?q?te?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .scrutinizer.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index db26de6..9c4042c 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -1,12 +1,12 @@ build: - image: default-jammy - environment: - php: 8.0 nodes: analysis: tests: override: - php-scrutinizer-run + environment: + php: + version: 8.2 filter: paths: ["src/*"] From 280117b58e35b96bb7a55a7b5fa18ae0ddf7fb67 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Wed, 7 Jan 2026 13:33:06 +0900 Subject: [PATCH 09/13] =?UTF-8?q?test:=20=E6=9C=AA=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E3=81=AE=20`invalidControllerProvider`=20=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89=E3=82=92=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E3=82=A2=E3=82=A6=E3=83=88=20migrate-attribute?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/AuraInputInterceptorTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/AuraInputInterceptorTest.php b/tests/AuraInputInterceptorTest.php index 8b2df09..18ad270 100644 --- a/tests/AuraInputInterceptorTest.php +++ b/tests/AuraInputInterceptorTest.php @@ -93,13 +93,13 @@ public function testProceed() $this->assertSame('201', $result); } - public function invalidControllerProvider() - { - return [ - [$this->injector->getInstance(FakeInvalidController1::class)], - [$this->injector->getInstance(FakeInvalidController2::class)] - ]; - } +// public function invalidControllerProvider() +// { +// return [ +// [$this->injector->getInstance(FakeInvalidController1::class)], +// [$this->injector->getInstance(FakeInvalidController2::class)] +// ]; +// } public function testInvalidFormPropertyByMissingProperty() { From 9f408e2d78fa19952eab04de9b4d7b28a7719467 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Sat, 7 Feb 2026 17:00:41 +0900 Subject: [PATCH 10/13] =?UTF-8?q?style:=20=E5=85=A8=E3=82=BD=E3=83=BC?= =?UTF-8?q?=E3=82=B9=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=AB=20`decla?= =?UTF-8?q?re(strict=5Ftypes=3D1)`=20=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97?= =?UTF-8?q?=E5=8E=B3=E5=AF=86=E3=81=AA=E5=9E=8B=E3=83=81=E3=82=A7=E3=83=83?= =?UTF-8?q?=E3=82=AF=E3=82=92=E6=9C=89=E5=8A=B9=E5=8C=96=20migrate-attribu?= =?UTF-8?q?te?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AbstractForm.php | 3 +++ src/Annotation/AbstractValidation.php | 3 +++ src/Annotation/FormValidation.php | 3 +++ src/Annotation/InputValidation.php | 3 +++ src/Annotation/VndError.php | 3 +++ src/AntiCsrf.php | 3 +++ src/AuraInputInterceptor.php | 3 +++ src/AuraInputModule.php | 3 +++ src/Exception/CsrfViolationException.php | 3 +++ src/Exception/ExceptionInterface.php | 3 +++ src/Exception/InvalidArgumentException.php | 3 +++ src/Exception/InvalidFormPropertyException.php | 3 +++ src/Exception/InvalidOnFailureMethod.php | 3 +++ src/Exception/LogicException.php | 3 +++ src/Exception/RuntimeException.php | 3 +++ src/Exception/ValidationException.php | 3 +++ src/FailureHandlerInterface.php | 3 +++ src/FormFactory.php | 3 +++ src/FormInterface.php | 3 +++ src/FormValidationError.php | 3 +++ src/FormVndErrorModule.php | 3 +++ src/InputValidationInterceptor.php | 3 +++ src/OnFailureMethodHandler.php | 3 +++ src/SetAntiCsrfTrait.php | 3 +++ src/SubmitInterface.php | 3 +++ src/ToStringInterface.php | 3 +++ src/VndErrorHandler.php | 4 ++++ 27 files changed, 82 insertions(+) diff --git a/src/AbstractForm.php b/src/AbstractForm.php index 71a7734..b81c26f 100644 --- a/src/AbstractForm.php +++ b/src/AbstractForm.php @@ -1,4 +1,7 @@ Date: Sat, 14 Feb 2026 11:47:04 +0900 Subject: [PATCH 11/13] =?UTF-8?q?style:=20namespace=20=E5=BE=8C=E3=81=AB?= =?UTF-8?q?=E7=A9=BA=E8=A1=8C=E8=BF=BD=E5=8A=A0=E3=80=81use=20=E6=96=87?= =?UTF-8?q?=E3=82=92=E6=95=B4=E7=90=86=E3=81=97=20FQCN=20=E3=82=92?= =?UTF-8?q?=E5=89=8A=E6=B8=9B=20migrate-attribute?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AbstractForm.php | 43 ++++++++----------- src/Annotation/InputValidation.php | 1 + src/AntiCsrf.php | 17 +++----- src/AuraInputInterceptor.php | 15 +++---- src/AuraInputModule.php | 9 ++-- src/Exception/CsrfViolationException.php | 1 + src/Exception/ExceptionInterface.php | 1 + src/Exception/InvalidArgumentException.php | 1 + .../InvalidFormPropertyException.php | 5 ++- src/Exception/InvalidOnFailureMethod.php | 5 ++- src/Exception/LogicException.php | 1 + src/Exception/RuntimeException.php | 1 + src/Exception/ValidationException.php | 6 ++- src/FailureHandlerInterface.php | 1 + src/FormFactory.php | 1 + src/FormInterface.php | 3 +- src/FormValidationError.php | 5 +-- src/FormVndErrorModule.php | 5 +-- src/InputValidationInterceptor.php | 1 + src/OnFailureMethodHandler.php | 5 +-- src/SetAntiCsrfTrait.php | 1 + src/SubmitInterface.php | 1 + src/ToStringInterface.php | 7 ++- src/VndErrorHandler.php | 8 +--- 24 files changed, 69 insertions(+), 75 deletions(-) diff --git a/src/AbstractForm.php b/src/AbstractForm.php index b81c26f..810a3e2 100644 --- a/src/AbstractForm.php +++ b/src/AbstractForm.php @@ -7,8 +7,10 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule; +use ArrayIterator; use Aura\Filter\FilterFactory; use Aura\Filter\SubjectFilter; use Aura\Html\HelperLocator; @@ -16,29 +18,23 @@ use Aura\Input\AntiCsrfInterface; use Aura\Input\BuilderInterface; use Aura\Input\Fieldset; +use Ray\Di\Di\Inject; +use Ray\Di\Di\PostConstruct; use Ray\WebFormModule\Exception\CsrfViolationException; use Ray\WebFormModule\Exception\LogicException; abstract class AbstractForm extends Fieldset implements FormInterface { - /** - * @var SubjectFilter - */ + /** @var SubjectFilter */ protected $filter; - /** - * @var null | array - */ + /** @var null|array */ protected $errorMessages; - /** - * @var HelperLocator - */ + /** @var HelperLocator */ protected $helper; - /** - * @var AntiCsrfInterface - */ + /** @var AntiCsrfInterface */ protected $antiCsrf; public function __construct() @@ -76,7 +72,7 @@ public function __toString() * @param FilterFactory $filterFactory * @param HelperLocatorFactory $helperFactory */ - #[\Ray\Di\Di\Inject] + #[Inject] public function setBaseDependencies( BuilderInterface $builder, FilterFactory $filterFactory, @@ -92,7 +88,7 @@ public function setAntiCsrf(AntiCsrfInterface $antiCsrf) $this->antiCsrf = $antiCsrf; } - #[\Ray\Di\Di\PostConstruct] + #[PostConstruct] public function postConstruct() { $this->init(); @@ -101,17 +97,13 @@ public function postConstruct() } } - /** - * {@inheritdoc} - */ + /** {@inheritdoc} */ public function input($input) { return $this->helper->input($this->get($input)); } - /** - * {@inheritdoc} - */ + /** {@inheritdoc} */ public function error($input) { if (! $this->errorMessages) { @@ -131,10 +123,10 @@ public function error($input) /** * @param array $attr attributes for the form tag * - * @throws \Aura\Html\Exception\HelperNotFound + * @return string * @throws \Aura\Input\Exception\NoSuchInput * - * @return string + * @throws \Aura\Html\Exception\HelperNotFound */ public function form($attr = []) { @@ -151,9 +143,8 @@ public function form($attr = []) * * @param array $data * - * @throws CsrfViolationException - * * @return bool + * @throws CsrfViolationException */ public function apply(array $data) { @@ -178,10 +169,10 @@ public function getFailureMessages() /** * Returns all the fields collection * - * @return \ArrayIterator + * @return ArrayIterator */ public function getIterator() { - return new \ArrayIterator($this->inputs); + return new ArrayIterator($this->inputs); } } diff --git a/src/Annotation/InputValidation.php b/src/Annotation/InputValidation.php index d8dbb8f..71ef33c 100644 --- a/src/Annotation/InputValidation.php +++ b/src/Annotation/InputValidation.php @@ -7,6 +7,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule\Annotation; use Attribute; diff --git a/src/AntiCsrf.php b/src/AntiCsrf.php index 6ac280e..6397971 100644 --- a/src/AntiCsrf.php +++ b/src/AntiCsrf.php @@ -7,6 +7,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule; use Aura\Input\AntiCsrfInterface; @@ -19,20 +20,16 @@ final class AntiCsrf implements AntiCsrfInterface const TOKEN_KEY = '__csrf_token'; - /** - * @var bool - */ + /** @var bool */ private $isCli; - /** - * @var Session - */ + /** @var Session */ private $session; /** * @param Session $session * @param bool|null $isCli - s */ + * s */ public function __construct(Session $session, $isCli = null) { $this->session = $session; @@ -42,7 +39,7 @@ public function __construct(Session $session, $isCli = null) public function setField(Fieldset $fieldset) { $fieldset->setField(self::TOKEN_KEY, 'hidden') - ->setAttribs(['value' => $this->getToken()]); + ->setAttribs(['value' => $this->getToken()]); } /** @@ -59,9 +56,7 @@ public function isValid(array $data) return isset($data[self::TOKEN_KEY]) && $data[self::TOKEN_KEY] == $this->getToken(); } - /** - * @return string - */ + /** @return string */ private function getToken() { $value = $this->isCli ? self::TEST_TOKEN : $this->session->getCsrfToken()->getValue(); diff --git a/src/AuraInputInterceptor.php b/src/AuraInputInterceptor.php index 274bb76..25505b5 100644 --- a/src/AuraInputInterceptor.php +++ b/src/AuraInputInterceptor.php @@ -7,6 +7,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule; use Doctrine\Common\Annotations\Reader; @@ -19,14 +20,10 @@ class AuraInputInterceptor implements MethodInterceptor { - /** - * @var Reader - */ + /** @var Reader */ protected $reader; - /** - * @var FailureHandlerInterface - */ + /** @var FailureHandlerInterface */ protected $failureHandler; /** @@ -65,15 +62,13 @@ public function invoke(MethodInvocation $invocation) * @param array $submit * @param AbstractForm $form * + * @return bool * @throws Exception\CsrfViolationException * - * @return bool */ public function isValid(array $submit, AbstractForm $form) { - $isValid = $form->apply($submit); - - return $isValid; + return $form->apply($submit); } /** diff --git a/src/AuraInputModule.php b/src/AuraInputModule.php index a46d581..2e3dd73 100644 --- a/src/AuraInputModule.php +++ b/src/AuraInputModule.php @@ -7,6 +7,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule; use Aura\Filter\FilterFactory; @@ -26,9 +27,7 @@ class AuraInputModule extends AbstractModule { - /** - * {@inheritdoc} - */ + /** {@inheritdoc} */ protected function configure() { $this->install(new AuraSessionModule); @@ -37,7 +36,9 @@ protected function configure() $this->bind(FilterInterface::class)->to(Filter::class); $this->bind(AntiCsrfInterface::class)->to(AntiCsrf::class)->in(Scope::SINGLETON); $this->bind(FailureHandlerInterface::class)->to(OnFailureMethodHandler::class); - $this->bind(FailureHandlerInterface::class)->annotatedWith('vnd_error')->to(VndErrorHandler::class)->in(Scope::SINGLETON); + $this->bind(FailureHandlerInterface::class)->annotatedWith('vnd_error')->to(VndErrorHandler::class)->in( + Scope::SINGLETON + ); $this->bind(HelperLocatorFactory::class); $this->bind(FilterFactory::class); $this->bindInterceptor( diff --git a/src/Exception/CsrfViolationException.php b/src/Exception/CsrfViolationException.php index 816cc28..c97b0b3 100644 --- a/src/Exception/CsrfViolationException.php +++ b/src/Exception/CsrfViolationException.php @@ -7,6 +7,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule\Exception; use Aura\Input\Exception\CsrfViolation; diff --git a/src/Exception/ExceptionInterface.php b/src/Exception/ExceptionInterface.php index dd435dc..237ca9d 100644 --- a/src/Exception/ExceptionInterface.php +++ b/src/Exception/ExceptionInterface.php @@ -7,6 +7,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule\Exception; interface ExceptionInterface diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index 47323d4..5afa83d 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -7,6 +7,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule\Exception; class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface diff --git a/src/Exception/InvalidFormPropertyException.php b/src/Exception/InvalidFormPropertyException.php index 73a98ff..e4c9b77 100644 --- a/src/Exception/InvalidFormPropertyException.php +++ b/src/Exception/InvalidFormPropertyException.php @@ -7,8 +7,11 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule\Exception; -class InvalidFormPropertyException extends \LogicException +use LogicException; + +class InvalidFormPropertyException extends LogicException { } diff --git a/src/Exception/InvalidOnFailureMethod.php b/src/Exception/InvalidOnFailureMethod.php index f48c830..5b3ccdc 100644 --- a/src/Exception/InvalidOnFailureMethod.php +++ b/src/Exception/InvalidOnFailureMethod.php @@ -7,8 +7,11 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule\Exception; -class InvalidOnFailureMethod extends \LogicException +use LogicException; + +class InvalidOnFailureMethod extends LogicException { } diff --git a/src/Exception/LogicException.php b/src/Exception/LogicException.php index da9cee0..d5ec12c 100644 --- a/src/Exception/LogicException.php +++ b/src/Exception/LogicException.php @@ -7,6 +7,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule\Exception; class LogicException extends \LogicException implements ExceptionInterface diff --git a/src/Exception/RuntimeException.php b/src/Exception/RuntimeException.php index 247a9ad..46953e3 100644 --- a/src/Exception/RuntimeException.php +++ b/src/Exception/RuntimeException.php @@ -7,6 +7,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule\Exception; class RuntimeException extends \LogicException implements ExceptionInterface diff --git a/src/Exception/ValidationException.php b/src/Exception/ValidationException.php index 94bb6b9..bbf583c 100644 --- a/src/Exception/ValidationException.php +++ b/src/Exception/ValidationException.php @@ -7,15 +7,17 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule\Exception; +use Exception; use Ray\WebFormModule\FormValidationError; -class ValidationException extends \Exception +class ValidationException extends Exception { public $error; - public function __construct($message = '', $code = 0, \Exception $e = null, FormValidationError $error = null) + public function __construct($message = '', $code = 0, Exception $e = null, FormValidationError $error = null) { parent::__construct($message, $code, $e); $this->error = $error; diff --git a/src/FailureHandlerInterface.php b/src/FailureHandlerInterface.php index 113f32b..948becf 100644 --- a/src/FailureHandlerInterface.php +++ b/src/FailureHandlerInterface.php @@ -7,6 +7,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule; use Ray\Aop\MethodInvocation; diff --git a/src/FormFactory.php b/src/FormFactory.php index 7136d69..b9b07f5 100644 --- a/src/FormFactory.php +++ b/src/FormFactory.php @@ -7,6 +7,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule; use Aura\Filter\FilterFactory; diff --git a/src/FormInterface.php b/src/FormInterface.php index ef49d75..f02348d 100644 --- a/src/FormInterface.php +++ b/src/FormInterface.php @@ -7,6 +7,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule; interface FormInterface @@ -16,9 +17,9 @@ interface FormInterface * * @param string $input * + * @return string * @throws \Aura\Input\Exception\NoSuchInput * - * @return string */ public function input($input); diff --git a/src/FormValidationError.php b/src/FormValidationError.php index 5f16121..cf93a02 100644 --- a/src/FormValidationError.php +++ b/src/FormValidationError.php @@ -7,13 +7,12 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule; class FormValidationError { - /** - * @var array - */ + /** @var array */ private $value; public function __construct(array $value) diff --git a/src/FormVndErrorModule.php b/src/FormVndErrorModule.php index cbd2063..a4064b2 100644 --- a/src/FormVndErrorModule.php +++ b/src/FormVndErrorModule.php @@ -7,15 +7,14 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule; use Ray\Di\AbstractModule; class FormVndErrorModule extends AbstractModule { - /** - * {@inheritdoc} - */ + /** {@inheritdoc} */ protected function configure() { $this->bind(FailureHandlerInterface::class)->to(VndErrorHandler::class); diff --git a/src/InputValidationInterceptor.php b/src/InputValidationInterceptor.php index 79df033..b262ae6 100644 --- a/src/InputValidationInterceptor.php +++ b/src/InputValidationInterceptor.php @@ -7,6 +7,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule; use Doctrine\Common\Annotations\Reader; diff --git a/src/OnFailureMethodHandler.php b/src/OnFailureMethodHandler.php index e4b1b7d..17567bf 100644 --- a/src/OnFailureMethodHandler.php +++ b/src/OnFailureMethodHandler.php @@ -7,6 +7,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule; use Ray\Aop\MethodInvocation; @@ -18,9 +19,7 @@ final class OnFailureMethodHandler implements FailureHandlerInterface { const FAILURE_SUFFIX = 'ValidationFailed'; - /** - * {@inheritdoc} - */ + /** {@inheritdoc} */ public function handle(AbstractValidation $formValidation, MethodInvocation $invocation, AbstractForm $form) { unset($form); diff --git a/src/SetAntiCsrfTrait.php b/src/SetAntiCsrfTrait.php index 32d448e..f314312 100644 --- a/src/SetAntiCsrfTrait.php +++ b/src/SetAntiCsrfTrait.php @@ -7,6 +7,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule; use Aura\Input\AntiCsrfInterface; diff --git a/src/SubmitInterface.php b/src/SubmitInterface.php index aba0cdf..10db621 100644 --- a/src/SubmitInterface.php +++ b/src/SubmitInterface.php @@ -7,6 +7,7 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule; interface SubmitInterface diff --git a/src/ToStringInterface.php b/src/ToStringInterface.php index 0385411..22ed248 100644 --- a/src/ToStringInterface.php +++ b/src/ToStringInterface.php @@ -7,12 +7,11 @@ * * @license http://opensource.org/licenses/MIT MIT */ + namespace Ray\WebFormModule; -/** - * Return form markup string - */ +/** Return form markup string */ interface ToStringInterface { - public function toString() : string; + public function toString(): string; } diff --git a/src/VndErrorHandler.php b/src/VndErrorHandler.php index 3c1bee4..d3bf03b 100644 --- a/src/VndErrorHandler.php +++ b/src/VndErrorHandler.php @@ -18,9 +18,7 @@ final class VndErrorHandler implements FailureHandlerInterface { - /** - * @var Reader - */ + /** @var Reader */ private $reader; public function __construct(Reader $reader) @@ -28,9 +26,7 @@ public function __construct(Reader $reader) $this->reader = $reader; } - /** - * {@inheritdoc} - */ + /** {@inheritdoc} */ public function handle(AbstractValidation $formValidation, MethodInvocation $invocation, AbstractForm $form) { unset($formValidation); From 46414e8d77293a30306fe7b3d44bee957587114d Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Sat, 14 Feb 2026 16:31:52 +0900 Subject: [PATCH 12/13] =?UTF-8?q?refactor:=20=E3=83=97=E3=83=AD=E3=83=91?= =?UTF-8?q?=E3=83=86=E3=82=A3=E3=81=A8=E8=BF=94=E3=82=8A=E5=80=A4=E3=81=AB?= =?UTF-8?q?=E5=9E=8B=E5=AE=A3=E8=A8=80=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=97?= =?UTF-8?q?=20PHP=208=20=E5=9E=8B=E3=82=B7=E3=82=B9=E3=83=86=E3=83=A0?= =?UTF-8?q?=E3=81=AB=E5=AF=BE=E5=BF=9C=20migrate-attribute?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AbstractForm.php | 42 ++++++++++++++---------------- src/AntiCsrf.php | 39 +++++++++++---------------- src/AuraInputInterceptor.php | 26 +++++++++--------- src/AuraInputModule.php | 5 ++-- src/FormFactory.php | 7 +---- src/FormInterface.php | 15 +++-------- src/FormValidationError.php | 8 ++++-- src/InputValidationInterceptor.php | 13 ++++----- src/OnFailureMethodHandler.php | 8 ++++-- src/SetAntiCsrfTrait.php | 6 ++--- src/VndErrorHandler.php | 7 ++--- 11 files changed, 77 insertions(+), 99 deletions(-) diff --git a/src/AbstractForm.php b/src/AbstractForm.php index 810a3e2..6d9e5b2 100644 --- a/src/AbstractForm.php +++ b/src/AbstractForm.php @@ -18,21 +18,23 @@ use Aura\Input\AntiCsrfInterface; use Aura\Input\BuilderInterface; use Aura\Input\Fieldset; +use Exception; use Ray\Di\Di\Inject; use Ray\Di\Di\PostConstruct; use Ray\WebFormModule\Exception\CsrfViolationException; use Ray\WebFormModule\Exception\LogicException; +use function trigger_error; + abstract class AbstractForm extends Fieldset implements FormInterface { /** @var SubjectFilter */ protected $filter; - /** @var null|array */ + /** @var array|null */ protected $errorMessages; - /** @var HelperLocator */ - protected $helper; + protected HelperLocator $helper; /** @var AntiCsrfInterface */ protected $antiCsrf; @@ -60,11 +62,11 @@ public function __toString() } return $this->toString(); - } catch (\Exception $e) { + } catch (Exception $e) { trigger_error($e->getMessage() . PHP_EOL . $e->getTraceAsString(), E_USER_ERROR); - - return ''; } + + return ''; } /** @@ -77,19 +79,19 @@ public function setBaseDependencies( BuilderInterface $builder, FilterFactory $filterFactory, HelperLocatorFactory $helperFactory - ) { + ): void { $this->builder = $builder; $this->filter = $filterFactory->newSubjectFilter(); $this->helper = $helperFactory->newInstance(); } - public function setAntiCsrf(AntiCsrfInterface $antiCsrf) + public function setAntiCsrf(AntiCsrfInterface $antiCsrf): void { $this->antiCsrf = $antiCsrf; } #[PostConstruct] - public function postConstruct() + public function postConstruct(): void { $this->init(); if ($this->antiCsrf instanceof AntiCsrfInterface) { @@ -104,7 +106,7 @@ public function input($input) } /** {@inheritdoc} */ - public function error($input) + public function error(string $input): string { if (! $this->errorMessages) { $failure = $this->filter->getFailures(); @@ -123,12 +125,10 @@ public function error($input) /** * @param array $attr attributes for the form tag * - * @return string * @throws \Aura\Input\Exception\NoSuchInput - * * @throws \Aura\Html\Exception\HelperNotFound */ - public function form($attr = []) + public function form(array $attr = []): string { $form = $this->helper->form($attr); if (isset($this->inputs['__csrf_token'])) { @@ -143,14 +143,14 @@ public function form($attr = []) * * @param array $data * - * @return bool * @throws CsrfViolationException */ - public function apply(array $data) + public function apply(array $data): bool { if ($this->antiCsrf && ! $this->antiCsrf->isValid($data)) { throw new CsrfViolationException; } + $this->fill($data); return $this->filter->apply($data); @@ -159,19 +159,15 @@ public function apply(array $data) /** * Returns all failure messages for all fields. * - * @return array + * @return list */ - public function getFailureMessages() + public function getFailureMessages(): array { return $this->filter->getFailures()->getMessages(); } - /** - * Returns all the fields collection - * - * @return ArrayIterator - */ - public function getIterator() + /** Returns all the fields collection */ + public function getIterator(): ArrayIterator { return new ArrayIterator($this->inputs); } diff --git a/src/AntiCsrf.php b/src/AntiCsrf.php index 6397971..bec9e6a 100644 --- a/src/AntiCsrf.php +++ b/src/AntiCsrf.php @@ -14,53 +14,44 @@ use Aura\Input\Fieldset; use Aura\Session\Session; +use function is_bool; + +use const PHP_SAPI; + final class AntiCsrf implements AntiCsrfInterface { - const TEST_TOKEN = '1234'; + public const TEST_TOKEN = '1234'; - const TOKEN_KEY = '__csrf_token'; + public const TOKEN_KEY = '__csrf_token'; - /** @var bool */ - private $isCli; + private bool $isCli; - /** @var Session */ - private $session; + private Session $session; - /** - * @param Session $session - * @param bool|null $isCli - * s */ - public function __construct(Session $session, $isCli = null) + public function __construct(Session $session, bool|null $isCli = null) { $this->session = $session; $this->isCli = is_bool($isCli) ? $isCli : PHP_SAPI === 'cli'; } - public function setField(Fieldset $fieldset) + public function setField(Fieldset $fieldset): void { $fieldset->setField(self::TOKEN_KEY, 'hidden') ->setAttribs(['value' => $this->getToken()]); } - /** - * @param array $data - * - * @return bool - */ - public function isValid(array $data) + /** @param array $data */ + public function isValid(array $data): bool { if ($this->isCli) { return true; } - return isset($data[self::TOKEN_KEY]) && $data[self::TOKEN_KEY] == $this->getToken(); + return isset($data[self::TOKEN_KEY]) && $data[self::TOKEN_KEY] === $this->getToken(); } - /** @return string */ - private function getToken() + private function getToken(): string { - $value = $this->isCli ? self::TEST_TOKEN : $this->session->getCsrfToken()->getValue(); - - return $value; + return $this->isCli ? self::TEST_TOKEN : $this->session->getCsrfToken()->getValue(); } } diff --git a/src/AuraInputInterceptor.php b/src/AuraInputInterceptor.php index 25505b5..2d951e8 100644 --- a/src/AuraInputInterceptor.php +++ b/src/AuraInputInterceptor.php @@ -17,19 +17,17 @@ use Ray\WebFormModule\Annotation\FormValidation; use Ray\WebFormModule\Exception\InvalidArgumentException; use Ray\WebFormModule\Exception\InvalidFormPropertyException; +use ReflectionClass; + +use function array_shift; +use function property_exists; class AuraInputInterceptor implements MethodInterceptor { - /** @var Reader */ - protected $reader; + protected Reader $reader; - /** @var FailureHandlerInterface */ - protected $failureHandler; + protected FailureHandlerInterface $failureHandler; - /** - * @param Reader $reader - * @param FailureHandlerInterface $handler - */ public function __construct(Reader $reader, FailureHandlerInterface $handler) { $this->reader = $reader; @@ -51,7 +49,7 @@ public function invoke(MethodInvocation $invocation) $data = $form instanceof SubmitInterface ? $form->submit() : $this->getNamedArguments($invocation); $isValid = $this->isValid($data, $form); if ($isValid === true) { - // validation success + // validation success return $invocation->proceed(); } @@ -66,7 +64,7 @@ public function invoke(MethodInvocation $invocation) * @throws Exception\CsrfViolationException * */ - public function isValid(array $submit, AbstractForm $form) + public function isValid(array $submit, AbstractForm $form): bool { return $form->apply($submit); } @@ -78,7 +76,7 @@ public function isValid(array $submit, AbstractForm $form) * * @return array */ - private function getNamedArguments(MethodInvocation $invocation) + private function getNamedArguments(MethodInvocation $invocation): array { $submit = []; $params = $invocation->getMethod()->getParameters(); @@ -87,7 +85,8 @@ private function getNamedArguments(MethodInvocation $invocation) $arg = array_shift($args); $submit[$param->getName()] = $arg; } - // has token ? + + // has token? if (isset($_POST[AntiCsrf::TOKEN_KEY])) { $submit[AntiCsrf::TOKEN_KEY] = $_POST[AntiCsrf::TOKEN_KEY]; } @@ -108,7 +107,8 @@ private function getFormProperty(AbstractValidation $formValidation, $object) if (! property_exists($object, $formValidation->form)) { throw new InvalidFormPropertyException($formValidation->form); } - $prop = (new \ReflectionClass($object))->getProperty($formValidation->form); + + $prop = (new ReflectionClass($object))->getProperty($formValidation->form); $prop->setAccessible(true); $form = $prop->getValue($object); if (! $form instanceof AbstractForm) { diff --git a/src/AuraInputModule.php b/src/AuraInputModule.php index 2e3dd73..2e39a54 100644 --- a/src/AuraInputModule.php +++ b/src/AuraInputModule.php @@ -36,9 +36,8 @@ protected function configure() $this->bind(FilterInterface::class)->to(Filter::class); $this->bind(AntiCsrfInterface::class)->to(AntiCsrf::class)->in(Scope::SINGLETON); $this->bind(FailureHandlerInterface::class)->to(OnFailureMethodHandler::class); - $this->bind(FailureHandlerInterface::class)->annotatedWith('vnd_error')->to(VndErrorHandler::class)->in( - Scope::SINGLETON - ); + $this->bind(FailureHandlerInterface::class) + ->annotatedWith('vnd_error')->to(VndErrorHandler::class)->in(Scope::SINGLETON); $this->bind(HelperLocatorFactory::class); $this->bind(FilterFactory::class); $this->bindInterceptor( diff --git a/src/FormFactory.php b/src/FormFactory.php index b9b07f5..ed12a80 100644 --- a/src/FormFactory.php +++ b/src/FormFactory.php @@ -16,12 +16,7 @@ final class FormFactory { - /** - * @param string $class - * - * @return AbstractForm - */ - public function newInstance($class) + public function newInstance(string $class): AbstractForm { /** @var $form AbstractForm */ $form = new $class; diff --git a/src/FormInterface.php b/src/FormInterface.php index f02348d..5e36178 100644 --- a/src/FormInterface.php +++ b/src/FormInterface.php @@ -15,20 +15,11 @@ interface FormInterface /** * Return input element html * - * @param string $input - * * @return string * @throws \Aura\Input\Exception\NoSuchInput - * */ - public function input($input); + public function input(string $input); - /** - * Return error message - * - * @param string $input - * - * @return string - */ - public function error($input); + /** Return error message */ + public function error(string $input): string; } diff --git a/src/FormValidationError.php b/src/FormValidationError.php index cf93a02..6cdec60 100644 --- a/src/FormValidationError.php +++ b/src/FormValidationError.php @@ -10,10 +10,14 @@ namespace Ray\WebFormModule; +use function json_encode; + +use const JSON_PRETTY_PRINT; +use const JSON_UNESCAPED_SLASHES; + class FormValidationError { - /** @var array */ - private $value; + private array $value; public function __construct(array $value) { diff --git a/src/InputValidationInterceptor.php b/src/InputValidationInterceptor.php index b262ae6..c3584ca 100644 --- a/src/InputValidationInterceptor.php +++ b/src/InputValidationInterceptor.php @@ -15,14 +15,11 @@ class InputValidationInterceptor extends AuraInputInterceptor { - /** - * @param Reader $reader - * @param FailureHandlerInterface $handler - * - * @Named("handler=vnd_error") - */ - public function __construct(Reader $reader, FailureHandlerInterface $handler) - { + public function __construct( + Reader $reader, + #[Named("vnd_error")] + FailureHandlerInterface $handler, + ) { parent::__construct($reader, $handler); } } diff --git a/src/OnFailureMethodHandler.php b/src/OnFailureMethodHandler.php index 17567bf..77ff764 100644 --- a/src/OnFailureMethodHandler.php +++ b/src/OnFailureMethodHandler.php @@ -15,9 +15,12 @@ use Ray\WebFormModule\Annotation\FormValidation; use Ray\WebFormModule\Exception\InvalidOnFailureMethod; +use function call_user_func_array; +use function method_exists; + final class OnFailureMethodHandler implements FailureHandlerInterface { - const FAILURE_SUFFIX = 'ValidationFailed'; + public const FAILURE_SUFFIX = 'ValidationFailed'; /** {@inheritdoc} */ public function handle(AbstractValidation $formValidation, MethodInvocation $invocation, AbstractForm $form) @@ -28,8 +31,9 @@ public function handle(AbstractValidation $formValidation, MethodInvocation $inv if (! $formValidation instanceof FormValidation) { throw new InvalidOnFailureMethod(get_class($invocation->getThis())); } + $onFailureMethod = $formValidation->onFailure ?: $invocation->getMethod()->getName() . self::FAILURE_SUFFIX; - if (! $formValidation instanceof FormValidation || ! method_exists($object, $onFailureMethod)) { + if (! method_exists($object, $onFailureMethod)) { throw new InvalidOnFailureMethod(get_class($invocation->getThis())); } diff --git a/src/SetAntiCsrfTrait.php b/src/SetAntiCsrfTrait.php index f314312..e3fb26b 100644 --- a/src/SetAntiCsrfTrait.php +++ b/src/SetAntiCsrfTrait.php @@ -11,12 +11,12 @@ namespace Ray\WebFormModule; use Aura\Input\AntiCsrfInterface; +use Ray\Di\Di\Inject; trait SetAntiCsrfTrait { - /** @param AntiCsrfInterface $antiCsrf */ - #[\Ray\Di\Di\Inject] - public function setAntiCsrf(AntiCsrfInterface $antiCsrf) + #[Inject] + public function setAntiCsrf(AntiCsrfInterface $antiCsrf): void { $this->antiCsrf = $antiCsrf; } diff --git a/src/VndErrorHandler.php b/src/VndErrorHandler.php index d3bf03b..3d82f2d 100644 --- a/src/VndErrorHandler.php +++ b/src/VndErrorHandler.php @@ -18,8 +18,7 @@ final class VndErrorHandler implements FailureHandlerInterface { - /** @var Reader */ - private $reader; + private Reader $reader; public function __construct(Reader $reader) { @@ -39,7 +38,7 @@ public function handle(AbstractValidation $formValidation, MethodInvocation $inv private function makeVndError(AbstractForm $form, VndError $vndError = null) { $body = ['message' => 'Validation failed']; - $body['path'] = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : ''; + $body['path'] = $_SERVER['PATH_INFO'] ?? ''; $body['validation_messages'] = $form->getFailureMessages(); $body = $vndError ? $this->optionalAttribute($vndError) + $body : $body; @@ -52,9 +51,11 @@ private function optionalAttribute(VndError $vndError) if ($vndError->message) { $body['message'] = $vndError->message; } + if ($vndError->path) { $body['path'] = $vndError->path; } + if ($vndError->logref) { $body['logref'] = $vndError->logref; } From dfafacbeb2cd5c3d0e0078010fc4be4d4aa4edfb Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Sat, 14 Feb 2026 16:46:08 +0900 Subject: [PATCH 13/13] =?UTF-8?q?refactor:=20Doctrine=20Annotations=20?= =?UTF-8?q?=E3=82=92=E5=89=8A=E9=99=A4=E3=81=97=20PHP=208=20Attributes=20?= =?UTF-8?q?=E3=81=AB=E5=AE=8C=E5=85=A8=E7=A7=BB=E8=A1=8C=20migrate-attribu?= =?UTF-8?q?te?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AuraInputInterceptor.php | 31 ++++++++++++++++++++---------- src/AuraInputModule.php | 3 --- src/InputValidationInterceptor.php | 4 +--- src/VndErrorHandler.php | 23 ++++++++++++++-------- tests/AbstractFormTest.php | 4 +--- 5 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/AuraInputInterceptor.php b/src/AuraInputInterceptor.php index 2d951e8..697693a 100644 --- a/src/AuraInputInterceptor.php +++ b/src/AuraInputInterceptor.php @@ -10,27 +10,24 @@ namespace Ray\WebFormModule; -use Doctrine\Common\Annotations\Reader; use Ray\Aop\MethodInterceptor; use Ray\Aop\MethodInvocation; use Ray\WebFormModule\Annotation\AbstractValidation; -use Ray\WebFormModule\Annotation\FormValidation; use Ray\WebFormModule\Exception\InvalidArgumentException; use Ray\WebFormModule\Exception\InvalidFormPropertyException; +use ReflectionAttribute; use ReflectionClass; +use ReflectionMethod; use function array_shift; use function property_exists; class AuraInputInterceptor implements MethodInterceptor { - protected Reader $reader; - protected FailureHandlerInterface $failureHandler; - public function __construct(Reader $reader, FailureHandlerInterface $handler) + public function __construct(FailureHandlerInterface $handler) { - $this->reader = $reader; $this->failureHandler = $handler; } @@ -42,20 +39,34 @@ public function __construct(Reader $reader, FailureHandlerInterface $handler) public function invoke(MethodInvocation $invocation) { $object = $invocation->getThis(); - /* @var $formValidation FormValidation */ - $method = $invocation->getMethod(); - $formValidation = $this->reader->getMethodAnnotation($method, AbstractValidation::class); + $formValidation = $this->getValidationAttribute($invocation->getMethod()); + if ($formValidation === null) { + throw new InvalidArgumentException('The method must be attributed with #[FormValidation] or #[InputValidation]'); + } + $form = $this->getFormProperty($formValidation, $object); $data = $form instanceof SubmitInterface ? $form->submit() : $this->getNamedArguments($invocation); $isValid = $this->isValid($data, $form); if ($isValid === true) { - // validation success return $invocation->proceed(); } return $this->failureHandler->handle($formValidation, $invocation, $form); } + private function getValidationAttribute(ReflectionMethod $method): AbstractValidation|null + { + $attributes = $method->getAttributes(AbstractValidation::class, ReflectionAttribute::IS_INSTANCEOF); + if ($attributes === []) { + return null; + } + + $instance = $attributes[0]->newInstance(); + assert($instance instanceof AbstractValidation); + + return $instance; + } + /** * @param array $submit * @param AbstractForm $form diff --git a/src/AuraInputModule.php b/src/AuraInputModule.php index 2e39a54..d2f56d6 100644 --- a/src/AuraInputModule.php +++ b/src/AuraInputModule.php @@ -17,8 +17,6 @@ use Aura\Input\BuilderInterface; use Aura\Input\Filter; use Aura\Input\FilterInterface; -use Doctrine\Common\Annotations\Reader; -use Koriym\Attributes\AttributeReader; use Ray\AuraSessionModule\AuraSessionModule; use Ray\Di\AbstractModule; use Ray\Di\Scope; @@ -31,7 +29,6 @@ class AuraInputModule extends AbstractModule protected function configure() { $this->install(new AuraSessionModule); - $this->bind(Reader::class)->to(AttributeReader::class)->in(Scope::SINGLETON); $this->bind(BuilderInterface::class)->to(Builder::class); $this->bind(FilterInterface::class)->to(Filter::class); $this->bind(AntiCsrfInterface::class)->to(AntiCsrf::class)->in(Scope::SINGLETON); diff --git a/src/InputValidationInterceptor.php b/src/InputValidationInterceptor.php index c3584ca..e59098e 100644 --- a/src/InputValidationInterceptor.php +++ b/src/InputValidationInterceptor.php @@ -10,16 +10,14 @@ namespace Ray\WebFormModule; -use Doctrine\Common\Annotations\Reader; use Ray\Di\Di\Named; class InputValidationInterceptor extends AuraInputInterceptor { public function __construct( - Reader $reader, #[Named("vnd_error")] FailureHandlerInterface $handler, ) { - parent::__construct($reader, $handler); + parent::__construct($handler); } } diff --git a/src/VndErrorHandler.php b/src/VndErrorHandler.php index 3d82f2d..9ddc582 100644 --- a/src/VndErrorHandler.php +++ b/src/VndErrorHandler.php @@ -12,29 +12,36 @@ use Doctrine\Common\Annotations\Reader; use Ray\Aop\MethodInvocation; +use Ray\Aop\ReflectionMethod; use Ray\WebFormModule\Annotation\AbstractValidation; use Ray\WebFormModule\Annotation\VndError; use Ray\WebFormModule\Exception\ValidationException; final class VndErrorHandler implements FailureHandlerInterface { - private Reader $reader; - - public function __construct(Reader $reader) - { - $this->reader = $reader; - } - /** {@inheritdoc} */ public function handle(AbstractValidation $formValidation, MethodInvocation $invocation, AbstractForm $form) { unset($formValidation); - $vndError = $this->reader->getMethodAnnotation($invocation->getMethod(), VndError::class); + $vndError = $this->getVndErrorAttribute($invocation->getMethod()); $error = new FormValidationError($this->makeVndError($form, $vndError)); throw new ValidationException('Validation failed.', 400, null, $error); } + private function getVndErrorAttribute(ReflectionMethod $method): VndError|null + { + $attributes = $method->getAttributes(VndError::class); + if ($attributes === []) { + return null; + } + + $instance = $attributes[0]->newInstance(); + assert($instance instanceof VndError); + + return $instance; + } + private function makeVndError(AbstractForm $form, VndError $vndError = null) { $body = ['message' => 'Validation failed']; diff --git a/tests/AbstractFormTest.php b/tests/AbstractFormTest.php index 973f553..86e8263 100644 --- a/tests/AbstractFormTest.php +++ b/tests/AbstractFormTest.php @@ -11,7 +11,6 @@ use Aura\Session\Randval; use Aura\Session\SegmentFactory; use Aura\Session\Session; -use Koriym\Attributes\AttributeReader; use PHPUnit\Framework\TestCase; use Ray\Aop\ReflectiveMethodInvocation; use Ray\WebFormModule\Exception\CsrfViolationException; @@ -41,8 +40,7 @@ public function getMethodInvocation(array $arguments) $controller = new FakeController; $controller->setForm($fakeForm); // interceptor - $reader = new AttributeReader; - $interceptor = new AuraInputInterceptor($reader, new VndErrorHandler($reader)); + $interceptor = new AuraInputInterceptor(new VndErrorHandler()); return new ReflectiveMethodInvocation( $controller,