Skip to content

Commit ecc0940

Browse files
committed
Address PR review feedback
1 parent 4cfa0db commit ecc0940

30 files changed

Lines changed: 165 additions & 178 deletions

.github/workflows/static-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ jobs:
99
sa:
1010
uses: ray-di/.github/.github/workflows/static-analysis.yml@v1
1111
with:
12-
php_version: 8.3
12+
php_version: 8.4

.travis.yml

Lines changed: 0 additions & 23 deletions
This file was deleted.

README.JA.md

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function createAction($id, $name, $body)
7979

8080
### Controller
8181

82-
コントローラークラスにフォームをインジェクトします。フォームのバリデーションを行うメソッドを`@FormValidation`
82+
コントローラークラスにフォームをインジェクトします。フォームのバリデーションを行うメソッドを`#[FormValidation]`
8383
アノテートします。この時フォームのプロパティ名を`form`で、バリデーションが失敗したときのメソッドを`onFailure`で指定します。
8484

8585
```php
@@ -95,18 +95,13 @@ class MyController
9595
*/
9696
protected $contactForm;
9797

98-
/**
99-
* @Inject
100-
* @Named("contact_form")
101-
*/
102-
public function setForm(FormInterface $form)
98+
#[Inject]
99+
public function setForm(#[Named("contact_form")] FormInterface $form)
103100
{
104101
$this->contactForm = $form;
105102
}
106103

107-
/**
108-
* @FormValidation(form="contactForm", onFailure="badRequestAction")
109-
*/
104+
#[FormValidation(form: "contactForm", onFailure: "badRequestAction")]
110105
public function createAction()
111106
{
112107
// validation success
@@ -144,16 +139,14 @@ class MyForm extends AbstractAuraForm
144139

145140
## Validation Exception
146141

147-
`@FormValidation`の代わりに`@InputValidation`とアノテートするとバリデーションが失敗したときに`Ray\WebFormModule\Exception\ValidationException`が投げられるよになります。この場合はHTML表現は使われません。Web APIアプリケーションなどに便利です。
142+
`#[FormValidation]`の代わりに`#[InputValidation]`とアノテートするとバリデーションが失敗したときに`Ray\WebFormModule\Exception\ValidationException`が投げられるよになります。この場合はHTML表現は使われません。Web APIアプリケーションなどに便利です。
148143

149144
```php
150145
use Ray\WebFormModule\Annotation\InputValidation;
151146

152147
class Foo
153148
{
154-
/**
155-
* @InputValidation(form="form1")
156-
*/
149+
#[InputValidation(form: "form1")]
157150
public function createAction($name)
158151
{
159152
// ...
@@ -190,17 +183,11 @@ echo $e->error;
190183
//}
191184
```
192185

193-
`@VndError`アノテーションで`vnd.error+json`に必要な情報を加えることができます。
186+
`#[VndError]`属性で`vnd.error+json`に必要な情報を加えることができます。
194187

195188
```php
196-
/**
197-
* @FormValidation(form="contactForm")
198-
* @VndError(
199-
* message="foo validation failed",
200-
* logref="a1000", path="/path/to/error",
201-
* href={"_self"="/path/to/error", "help"="/path/to/help"}
202-
* )
203-
*/
189+
#[FormValidation(form: "contactForm")]
190+
#[VndError(message: "foo validation failed", logref: "a1000", path: "/path/to/error", href: ["_self" => "/path/to/error", "help" => "/path/to/help"])]
204191
```
205192

206193
このオプションのモジュールはAPIアプリケーションの時に有用です。

README.md

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class MyForm extends AbstractForm
8888
```
8989
### Controller
9090

91-
We annotate the methods which web form validation is required with `@FormValidation`. We can specify form object property name with `name` and failiure method name with `@onFailure`.
91+
We annotate the methods which web form validation is required with `#[FormValidation]`. We can specify form object property name with `form` and failure method name with `onFailure`.
9292

9393
```php
9494
use Ray\Di\Di\Inject;
@@ -103,18 +103,13 @@ class MyController
103103
*/
104104
protected $contactForm;
105105

106-
/**
107-
* @Inject
108-
* @Named("contact_form")
109-
*/
110-
public function setForm(FormInterface $form)
106+
#[Inject]
107+
public function setForm(#[Named("contact_form")] FormInterface $form)
111108
{
112109
$this->contactForm = $form;
113110
}
114111

115-
/**
116-
* @FormValidation(form="contactForm", onFailure="badRequestAction")
117-
*/
112+
#[FormValidation(form: "contactForm", onFailure: "badRequestAction")]
118113
public function createAction()
119114
{
120115
// validation success
@@ -183,17 +178,11 @@ echo $e->error;
183178
//}
184179
```
185180

186-
More detail for `vnd.error+json`can be add with `@VndError` annotation.
181+
More detail for `vnd.error+json` can be added with the `#[VndError]` attribute.
187182

188183
```php
189-
/**
190-
* @FormValidation(form="contactForm")
191-
* @VndError(
192-
* message="foo validation failed",
193-
* logref="a1000", path="/path/to/error",
194-
* href={"_self"="/path/to/error", "help"="/path/to/help"}
195-
* )
196-
*/
184+
#[FormValidation(form: "contactForm")]
185+
#[VndError(message: "foo validation failed", logref: "a1000", path: "/path/to/error", href: ["_self" => "/path/to/error", "help" => "/path/to/help"])]
197186
```
198187

199188
This optional module is handy for API application.

src/AbstractForm.php

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,22 @@
2525
use const E_USER_ERROR;
2626
use const PHP_EOL;
2727

28+
/** @psalm-suppress PropertyNotSetInConstructor */
2829
abstract class AbstractForm extends Fieldset implements FormInterface
2930
{
30-
/** @var SubjectFilter */
31-
protected $filter;
31+
/**
32+
* @var SubjectFilter
33+
* @psalm-suppress NonInvariantDocblockPropertyType
34+
*/
35+
protected $filter; // @phpstan-ignore-line
3236

3337
/** @var array<string, array<string>>|null */
3438
protected $errorMessages;
3539

3640
/** @var HelperLocator */
3741
protected $helper;
3842

39-
/** @var AntiCsrfInterface */
43+
/** @var AntiCsrfInterface|null */
4044
protected $antiCsrf;
4145

4246
public function __construct()
@@ -65,7 +69,7 @@ public function __toString()
6569
} catch (Throwable $e) {
6670
trigger_error($e->getMessage() . PHP_EOL . $e->getTraceAsString(), E_USER_ERROR);
6771

68-
return '';
72+
return ''; // @codeCoverageIgnore @phpstan-ignore deadCode.unreachable
6973
}
7074
}
7175

@@ -74,22 +78,23 @@ public function setBaseDependencies(
7478
BuilderInterface $builder,
7579
FilterFactory $filterFactory,
7680
HelperLocatorFactory $helperFactory,
77-
) {
78-
$this->builder = $builder;
81+
): void {
82+
/** @psalm-suppress PropertyTypeCoercion */
83+
$this->builder = $builder; // @phpstan-ignore-line
7984
$this->filter = $filterFactory->newSubjectFilter();
8085
$this->helper = $helperFactory->newInstance();
8186
}
8287

83-
public function setAntiCsrf(AntiCsrfInterface $antiCsrf)
88+
public function setAntiCsrf(AntiCsrfInterface $antiCsrf): void
8489
{
8590
$this->antiCsrf = $antiCsrf;
8691
}
8792

8893
#[PostConstruct]
89-
public function postConstruct()
94+
public function postConstruct(): void
9095
{
9196
$this->init();
92-
if (! ($this->antiCsrf instanceof AntiCsrfInterface)) {
97+
if ($this->antiCsrf === null) {
9398
return;
9499
}
95100

@@ -101,18 +106,32 @@ public function postConstruct()
101106
*/
102107
public function input($input)
103108
{
104-
return $this->helper->input($this->get($input));
109+
/**
110+
* @var string $result
111+
* @psalm-suppress UndefinedMagicMethod
112+
*/
113+
$result = $this->helper->input($this->get($input)); // @phpstan-ignore-line
114+
115+
return $result;
105116
}
106117

107118
/**
108119
* {@inheritDoc}
109120
*/
110121
public function error($input)
111122
{
112-
if (! $this->errorMessages) {
123+
if ($this->errorMessages === null) {
113124
$failure = $this->filter->getFailures();
114-
if ($failure) {
115-
$this->errorMessages = $failure->getMessages();
125+
/**
126+
* @psalm-suppress RedundantConditionGivenDocblockType - getFailures() can return null at runtime
127+
* @phpstan-ignore notIdentical.alwaysTrue
128+
*/
129+
if ($failure !== null) {
130+
/** @var array<string, array<string>> $messages */
131+
$messages = $failure->getMessages();
132+
$this->errorMessages = $messages;
133+
} else {
134+
$this->errorMessages = [];
116135
}
117136
}
118137

@@ -133,9 +152,14 @@ public function error($input)
133152
*/
134153
public function form($attr = [])
135154
{
136-
$form = $this->helper->form($attr);
155+
/**
156+
* @var string $form
157+
* @psalm-suppress UndefinedMagicMethod
158+
*/
159+
$form = $this->helper->form($attr); // @phpstan-ignore-line
137160
if (isset($this->inputs['__csrf_token'])) {
138-
$form .= $this->helper->input($this->get('__csrf_token'));
161+
/** @psalm-suppress UndefinedMagicMethod */
162+
$form .= $this->helper->input($this->get('__csrf_token')); // @phpstan-ignore-line
139163
}
140164

141165
return $form;
@@ -152,7 +176,7 @@ public function form($attr = [])
152176
*/
153177
public function apply(array $data)
154178
{
155-
if ($this->antiCsrf && ! $this->antiCsrf->isValid($data)) {
179+
if ($this->antiCsrf !== null && ! $this->antiCsrf->isValid($data)) {
156180
throw new CsrfViolationException();
157181
}
158182

@@ -168,15 +192,18 @@ public function apply(array $data)
168192
*/
169193
public function getFailureMessages()
170194
{
171-
return $this->filter->getFailures()->getMessages();
195+
/** @var array<string, array<string>> $messages */
196+
$messages = $this->filter->getFailures()->getMessages();
197+
198+
return $messages;
172199
}
173200

174201
/**
175202
* Returns all the fields collection
176203
*
177-
* @return ArrayIterator
204+
* @return ArrayIterator<array-key, mixed>
178205
*/
179-
public function getIterator()
206+
public function getIterator(): ArrayIterator
180207
{
181208
return new ArrayIterator($this->inputs);
182209
}

src/Annotation/AbstractValidation.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
namespace Ray\WebFormModule\Annotation;
66

7-
use Attribute;
8-
9-
#[Attribute(Attribute::TARGET_METHOD)]
10-
class AbstractValidation
7+
abstract class AbstractValidation
118
{
129
public function __construct(public string $form = 'form')
1310
{

src/AntiCsrf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function setField(Fieldset $fieldset)
3838
}
3939

4040
/**
41-
* @param array<string, mixed> $data
41+
* @param array<array-key, mixed> $data
4242
*
4343
* @return bool
4444
*/

src/AuraInputInterceptor.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use ReflectionMethod;
1616

1717
use function array_shift;
18+
use function is_array;
1819
use function property_exists;
1920

2021
class AuraInputInterceptor implements MethodInterceptor
@@ -35,11 +36,12 @@ public function __construct(FailureHandlerInterface $handler)
3536
public function invoke(MethodInvocation $invocation)
3637
{
3738
$object = $invocation->getThis();
38-
/** @var FormValidation $formValidation */
3939
$method = $invocation->getMethod();
4040
$formValidation = $this->getValidationAttribute($method);
4141
$form = $this->getFormProperty($formValidation, $object);
42-
$data = $form instanceof SubmitInterface ? $form->submit() : $this->getNamedArguments($invocation);
42+
$submit = $form instanceof SubmitInterface ? $form->submit() : $this->getNamedArguments($invocation);
43+
/** @var array<string, mixed> $data */
44+
$data = is_array($submit) ? $submit : (array) $submit;
4345
$isValid = $this->isValid($data, $form);
4446
if ($isValid === true) {
4547
// validation success
@@ -64,6 +66,8 @@ public function isValid(array $submit, AbstractForm $form)
6466
/**
6567
* Return arguments as named arguments.
6668
*
69+
* @param MethodInvocation<object> $invocation
70+
*
6771
* @return array<string, mixed>
6872
*/
6973
private function getNamedArguments(MethodInvocation $invocation)
@@ -72,7 +76,9 @@ private function getNamedArguments(MethodInvocation $invocation)
7276
$params = $invocation->getMethod()->getParameters();
7377
$args = $invocation->getArguments()->getArrayCopy();
7478
foreach ($params as $param) {
79+
/** @var mixed $arg */
7580
$arg = array_shift($args);
81+
/** @psalm-suppress MixedAssignment */
7682
$submit[$param->getName()] = $arg;
7783
}
7884

src/AuraInputModule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Ray\WebFormModule\Annotation\FormValidation;
1818
use Ray\WebFormModule\Annotation\InputValidation;
1919

20-
class AuraInputModule extends AbstractModule
20+
final class AuraInputModule extends AbstractModule
2121
{
2222
/**
2323
* {@inheritDoc}

src/Exception/CsrfViolationException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
use Aura\Input\Exception\CsrfViolation;
88

9-
class CsrfViolationException extends CsrfViolation
9+
final class CsrfViolationException extends CsrfViolation
1010
{
1111
}

0 commit comments

Comments
 (0)