Skip to content

Commit 66be1e5

Browse files
authored
QA: fix phpstan (#31)
1 parent a54da82 commit 66be1e5

5 files changed

Lines changed: 29 additions & 23 deletions

File tree

src/Facade.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Contributte\FormWizard;
44

5+
use Closure;
56
use Nette\Forms\Form;
67
use Nette\SmartObject;
78
use Nette\Utils\ArrayHash;
8-
use Nette\Utils\ObjectHelpers;
99

1010
/**
1111
* @property-read mixed[]|ArrayHash $values
@@ -30,7 +30,7 @@ public function __construct(IWizard $wizard)
3030
}
3131

3232
/**
33-
* @return mixed[]|ArrayHash<string|int,mixed>
33+
* @return array<mixed>|ArrayHash<string|int,mixed>
3434
*/
3535
public function getValues(bool $asArray = false)
3636
{
@@ -114,13 +114,13 @@ public function isDisabled(int $step): bool
114114
public function __get(string $name)
115115
{
116116
$getters = ['get' . ucfirst($name), 'is' . ucfirst($name)];
117+
117118
foreach ($getters as $getter) {
118-
if (method_exists($this, $getter)) {
119-
return $this->$getter();
120-
}
119+
$callable = [$this, $getter];
120+
assert(is_callable($callable));
121+
$method = Closure::fromCallable($callable);
122+
return $method($getter);
121123
}
122-
123-
ObjectHelpers::strictGet(static::class, $name);
124124
}
125125

126126
}

src/IWizard.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function getStepData(int $step): array;
4343
public function getTotalSteps(): int;
4444

4545
/**
46-
* @return mixed[]|ArrayHash<string|int,mixed>
46+
* @return array<mixed>|ArrayHash<mixed>
4747
*/
4848
public function getValues(bool $asArray = false);
4949

src/Latte/WizardMacros.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static function createFacade(IComponent $component): Facade
3838
public function wizardStart(MacroNode $node, PhpWriter $writer): string
3939
{
4040
$word = $node->tokenizer->fetchWord();
41-
if (!$word) {
41+
if ($word === null || $word === '') {
4242
throw new CompileException('Missing control name in {wizard}');
4343
}
4444

@@ -54,7 +54,7 @@ public function wizardStart(MacroNode $node, PhpWriter $writer): string
5454
public function stepStart(MacroNode $node, PhpWriter $writer): string
5555
{
5656
$word = $node->tokenizer->fetchWord();
57-
if (!is_numeric($word) && !in_array($word, ['success', '"success"', "'success'"])) {
57+
if (!is_numeric($word) && !in_array($word, ['success', '"success"', "'success'"], true)) {
5858
throw new CompileException('First parameter in {step} must be a numeric.');
5959
}
6060

src/Steps/StepCounter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ public function getTotalSteps(): int
2929

3030
public function getCurrentStep(): int
3131
{
32-
return $this->minmax($this->section->getCurrentStep() ?: 1);
32+
return $this->minmax($this->section->getCurrentStep() ?? 1);
3333
}
3434

3535
public function getLastStep(): int
3636
{
37-
return $this->minmax($this->section->getLastStep() ?: 1);
37+
return $this->minmax($this->section->getLastStep() ?? 1);
3838
}
3939

4040
public function setLastStep(int $step): void
@@ -70,7 +70,7 @@ public function previousStep(): void
7070

7171
public function canFinish(): bool
7272
{
73-
return $this->section->getValues() && $this->getLastStep() === $this->totalSteps;
73+
return $this->section->getValues() !== [] && $this->getLastStep() === $this->totalSteps;
7474
}
7575

7676
protected function minmax(int $value, int $min = 1, ?int $max = null): int

src/Wizard.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Contributte\FormWizard\Session\WizardSessionSection;
66
use Contributte\FormWizard\Steps\StepCounter;
7+
use Closure;
78
use InvalidArgumentException;
89
use LogicException;
910
use Nette\Application\UI\Component;
@@ -107,7 +108,7 @@ public function isSuccess(): bool
107108

108109
protected function getSection(): WizardSessionSection
109110
{
110-
if (!$this->section) {
111+
if ($this->section === null) {
111112
$section = $this->session->getSection('wizard' . $this->getName())
112113
->setExpiration($this->expiration);
113114

@@ -119,7 +120,8 @@ protected function getSection(): WizardSessionSection
119120

120121
public function getStepCounter(): StepCounter
121122
{
122-
if (!$this->stepCounter) {
123+
$counter = 1;
124+
if ($this->stepCounter === null) {
123125
for ($counter = 1; $counter < 1000; $counter++) {
124126
if (!method_exists($this, 'createStep' . $counter) && !$this->getComponent('step' . $counter, false)) {
125127
$counter--;
@@ -182,7 +184,7 @@ public function getTotalSteps(): int
182184
}
183185

184186
/**
185-
* @return mixed[]|ArrayHash<string|int,mixed>
187+
* @return array<mixed>|ArrayHash<mixed>
186188
*/
187189
public function getValues(bool $asArray = false)
188190
{
@@ -222,7 +224,7 @@ public function setStep(int $step): IWizard
222224

223225
protected function createForm(): Form
224226
{
225-
return $this->factory ? $this->factory->create() : new Form();
227+
return $this->factory !== null ? $this->factory->create() : new Form();
226228
}
227229

228230
public function submitStep(SubmitButton $button): void
@@ -236,7 +238,7 @@ public function submitStep(SubmitButton $button): void
236238
$submitName = $button->getName();
237239
$step = $this->extractStepFromName($form->getName());
238240

239-
if (!$step || $step !== $this->getCurrentStep()) {
241+
if ($step === null || $step !== $this->getCurrentStep()) {
240242
return;
241243
}
242244

@@ -299,7 +301,7 @@ public function create(?string $step = null): Form
299301
*/
300302
protected function extractStepFromName($name): ?int
301303
{
302-
if ($name === null || !preg_match('#^step(\d+)$#', $name, $matches)) {
304+
if ($name === null || preg_match('#^step(\d+)$#', $name, $matches) === false) {
303305
return null;
304306
}
305307

@@ -326,11 +328,15 @@ public function addComponent(IComponent $component, ?string $name, ?string $inse
326328

327329
protected function createComponent(string $name): ?IComponent
328330
{
329-
if (preg_match('#^step\d+$#', $name)) {
331+
if (preg_match('#^step\d+$#', $name) > 0) {
330332
$ucname = ucfirst($name);
331333
$method = 'create' . $ucname;
332334
if ($ucname !== $name && method_exists($this, $method) && (new ReflectionMethod($this, $method))->getName() === $method) {
333-
$component = $this->$method($name);
335+
$callable = [$this, $method];
336+
assert(is_callable($callable));
337+
$callableMethod = Closure::fromCallable($callable);
338+
$component = $callableMethod($name);
339+
334340
if (!$component instanceof IComponent && $this->getComponent($name) === null) {
335341
throw new UnexpectedValueException(
336342
sprintf('Method %s::%s() did not return or create the desired component.', static::class, $method)
@@ -350,7 +356,7 @@ private function applyCallbacksToButtons(Forms\Form $form): void
350356
{
351357
/** @var SubmitButton $control */
352358
foreach ($form->getComponents(false, SubmitButton::class) as $control) {
353-
if (!in_array($control->getName(), [self::FINISH_SUBMIT_NAME, self::NEXT_SUBMIT_NAME, self::PREV_SUBMIT_NAME])) {
359+
if (!in_array($control->getName(), [self::FINISH_SUBMIT_NAME, self::NEXT_SUBMIT_NAME, self::PREV_SUBMIT_NAME], true)) {
354360
continue;
355361
}
356362

@@ -367,7 +373,7 @@ private function applyCallbacksToButtons(Forms\Form $form): void
367373
*/
368374
public function getPresenter(): ?Presenter
369375
{
370-
if (!$this->presenter) {
376+
if ($this->presenter === null) {
371377
$this->presenter = parent::getPresenter();
372378
}
373379

0 commit comments

Comments
 (0)