Skip to content

Commit

Permalink
QA: fix phpstan (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
petrparolek committed Jul 6, 2021
1 parent a54da82 commit 66be1e5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
14 changes: 7 additions & 7 deletions src/Facade.php
Expand Up @@ -2,10 +2,10 @@

namespace Contributte\FormWizard;

use Closure;
use Nette\Forms\Form;
use Nette\SmartObject;
use Nette\Utils\ArrayHash;
use Nette\Utils\ObjectHelpers;

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

/**
* @return mixed[]|ArrayHash<string|int,mixed>
* @return array<mixed>|ArrayHash<string|int,mixed>
*/
public function getValues(bool $asArray = false)
{
Expand Down Expand Up @@ -114,13 +114,13 @@ public function isDisabled(int $step): bool
public function __get(string $name)
{
$getters = ['get' . ucfirst($name), 'is' . ucfirst($name)];

foreach ($getters as $getter) {
if (method_exists($this, $getter)) {
return $this->$getter();
}
$callable = [$this, $getter];
assert(is_callable($callable));
$method = Closure::fromCallable($callable);
return $method($getter);
}

ObjectHelpers::strictGet(static::class, $name);
}

}
2 changes: 1 addition & 1 deletion src/IWizard.php
Expand Up @@ -43,7 +43,7 @@ public function getStepData(int $step): array;
public function getTotalSteps(): int;

/**
* @return mixed[]|ArrayHash<string|int,mixed>
* @return array<mixed>|ArrayHash<mixed>
*/
public function getValues(bool $asArray = false);

Expand Down
4 changes: 2 additions & 2 deletions src/Latte/WizardMacros.php
Expand Up @@ -38,7 +38,7 @@ public static function createFacade(IComponent $component): Facade
public function wizardStart(MacroNode $node, PhpWriter $writer): string
{
$word = $node->tokenizer->fetchWord();
if (!$word) {
if ($word === null || $word === '') {
throw new CompileException('Missing control name in {wizard}');
}

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

Expand Down
6 changes: 3 additions & 3 deletions src/Steps/StepCounter.php
Expand Up @@ -29,12 +29,12 @@ public function getTotalSteps(): int

public function getCurrentStep(): int
{
return $this->minmax($this->section->getCurrentStep() ?: 1);
return $this->minmax($this->section->getCurrentStep() ?? 1);
}

public function getLastStep(): int
{
return $this->minmax($this->section->getLastStep() ?: 1);
return $this->minmax($this->section->getLastStep() ?? 1);
}

public function setLastStep(int $step): void
Expand Down Expand Up @@ -70,7 +70,7 @@ public function previousStep(): void

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

protected function minmax(int $value, int $min = 1, ?int $max = null): int
Expand Down
26 changes: 16 additions & 10 deletions src/Wizard.php
Expand Up @@ -4,6 +4,7 @@

use Contributte\FormWizard\Session\WizardSessionSection;
use Contributte\FormWizard\Steps\StepCounter;
use Closure;
use InvalidArgumentException;
use LogicException;
use Nette\Application\UI\Component;
Expand Down Expand Up @@ -107,7 +108,7 @@ public function isSuccess(): bool

protected function getSection(): WizardSessionSection
{
if (!$this->section) {
if ($this->section === null) {
$section = $this->session->getSection('wizard' . $this->getName())
->setExpiration($this->expiration);

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

public function getStepCounter(): StepCounter
{
if (!$this->stepCounter) {
$counter = 1;
if ($this->stepCounter === null) {
for ($counter = 1; $counter < 1000; $counter++) {
if (!method_exists($this, 'createStep' . $counter) && !$this->getComponent('step' . $counter, false)) {
$counter--;
Expand Down Expand Up @@ -182,7 +184,7 @@ public function getTotalSteps(): int
}

/**
* @return mixed[]|ArrayHash<string|int,mixed>
* @return array<mixed>|ArrayHash<mixed>
*/
public function getValues(bool $asArray = false)
{
Expand Down Expand Up @@ -222,7 +224,7 @@ public function setStep(int $step): IWizard

protected function createForm(): Form
{
return $this->factory ? $this->factory->create() : new Form();
return $this->factory !== null ? $this->factory->create() : new Form();
}

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

if (!$step || $step !== $this->getCurrentStep()) {
if ($step === null || $step !== $this->getCurrentStep()) {
return;
}

Expand Down Expand Up @@ -299,7 +301,7 @@ public function create(?string $step = null): Form
*/
protected function extractStepFromName($name): ?int
{
if ($name === null || !preg_match('#^step(\d+)$#', $name, $matches)) {
if ($name === null || preg_match('#^step(\d+)$#', $name, $matches) === false) {
return null;
}

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

protected function createComponent(string $name): ?IComponent
{
if (preg_match('#^step\d+$#', $name)) {
if (preg_match('#^step\d+$#', $name) > 0) {
$ucname = ucfirst($name);
$method = 'create' . $ucname;
if ($ucname !== $name && method_exists($this, $method) && (new ReflectionMethod($this, $method))->getName() === $method) {
$component = $this->$method($name);
$callable = [$this, $method];
assert(is_callable($callable));
$callableMethod = Closure::fromCallable($callable);
$component = $callableMethod($name);

if (!$component instanceof IComponent && $this->getComponent($name) === null) {
throw new UnexpectedValueException(
sprintf('Method %s::%s() did not return or create the desired component.', static::class, $method)
Expand All @@ -350,7 +356,7 @@ private function applyCallbacksToButtons(Forms\Form $form): void
{
/** @var SubmitButton $control */
foreach ($form->getComponents(false, SubmitButton::class) as $control) {
if (!in_array($control->getName(), [self::FINISH_SUBMIT_NAME, self::NEXT_SUBMIT_NAME, self::PREV_SUBMIT_NAME])) {
if (!in_array($control->getName(), [self::FINISH_SUBMIT_NAME, self::NEXT_SUBMIT_NAME, self::PREV_SUBMIT_NAME], true)) {
continue;
}

Expand All @@ -367,7 +373,7 @@ private function applyCallbacksToButtons(Forms\Form $form): void
*/
public function getPresenter(): ?Presenter
{
if (!$this->presenter) {
if ($this->presenter === null) {
$this->presenter = parent::getPresenter();
}

Expand Down

0 comments on commit 66be1e5

Please sign in to comment.