Skip to content

Commit

Permalink
add phpstan library to dev dependencies
Browse files Browse the repository at this point in the history
add phpdocs with type properties
  • Loading branch information
smajti1 committed Mar 3, 2022
1 parent 4fa28d5 commit 2b83390
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 26 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.7.0] - 2022-03-03
### Added
- phpstan library to dev dependencies
- phpdocs with type properties

## [1.6.1] - 2022-02-28
### Added
- support `laravel` version `^9.0`
Expand Down Expand Up @@ -68,3 +73,4 @@ And will be changed to non-static
[1.4.0]: https://github.com/smajti1/laravel-wizard/compare/v1.3.0...v1.4.0
[1.5.0]: https://github.com/smajti1/laravel-wizard/compare/v1.4.0...v1.5.0
[1.6.0]: https://github.com/smajti1/laravel-wizard/compare/v1.5.0...v1.6.0
[1.7.0]: https://github.com/smajti1/laravel-wizard/compare/v1.6.0...v1.7.0
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ RUN apk update && apk add --no-cache \
RUN pecl install xdebug-3.1.3 && docker-php-ext-enable xdebug

RUN usermod -u 1000 www-data
RUN chown www-data:www-data /var/www/
RUN chown www-data:www-data /var/www/html/
RUN chown www-data:www-data /var/www/ /var/www/html/

RUN curl --insecure https://getcomposer.org/composer-stable.phar -o /usr/bin/composer && chmod +x /usr/bin/composer
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"illuminate/http": "^7.0 || ^8.0 || ^9.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^1.4"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# vendor/bin/phpstan analyse --configuration=phpstan.neon
parameters:
level: 8
paths:
- src
- tests
22 changes: 11 additions & 11 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
processIsolation="false"
stopOnFailure="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
</phpunit>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
22 changes: 21 additions & 1 deletion src/Step.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,33 @@ abstract class Step

/**
* @deprecated since 1.1.0 $label will be no more static
* @var string
*/
public static $label;

/**
* @deprecated from 1.1.0 $slug will be no more static
* @var string
*/
public static $slug;

/**
* @deprecated from 1.1.0 $view will be no more static
* @var string
*/
public static $view;
/** @var int */
public $number;
/** @var int|string */
public $key;
/** @var int */
public $index;
/** @var Wizard */
protected $wizard;

/**
* @param int|string $key
*/
public function __construct(int $number, $key, int $index, Wizard $wizard)
{
$this->number = $number;
Expand All @@ -34,13 +44,23 @@ public function __construct(int $number, $key, int $index, Wizard $wizard)
$this->wizard = $wizard;
}

/**
* @return void
*/
abstract public function process(Request $request);

/**
* @return array{string, string}|array{}
*/
public function rules(Request $request = null): array
{
return [];
}

/**
* @param array<mixed> $additionalData
* @return void
*/
public function saveProgress(Request $request, array $additionalData = [])
{
$wizardData = $this->wizard->data();
Expand All @@ -56,4 +76,4 @@ public function clearData(): void
unset($data[$this::$slug]);
$this->wizard->data($data);
}
}
}
54 changes: 45 additions & 9 deletions src/Wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ class Wizard
{

const SESSION_NAME = 'smajti1.wizard';
/** @var array<int, Step> */
protected $steps = [];
/** @var int */
protected $currentIndex = -1;
/** @var string */
protected $sessionKeyName = '';

/**
* @param array<int|string, class-string<Step>>|array{} $steps
* @throws StepNotFoundException
*/
public function __construct(array $steps, string $sessionKeyName = '')
Expand All @@ -37,10 +41,13 @@ public function __construct(array $steps, string $sessionKeyName = '')
}
}

/**
* @param class-string<Step> $stepClassName
* @param string|int $key
*/
protected function createStepClass($stepClassName, int $naturalNumber, $key, int $index): Step
{
$step = new $stepClassName($naturalNumber, $key, $index, $this);
return $step;
return new $stepClassName($naturalNumber, $key, $index, $this);
}

public function prevStep(): ?Step
Expand Down Expand Up @@ -140,19 +147,23 @@ public function firstOrLastProcessed(int $moveSteps = 0): Step
*/
public function lastProcessed(): ?bool
{
return $this->lastProcessedIndex();
$last_processed_index = $this->lastProcessedIndex();
return $last_processed_index === null ? null : (bool) $last_processed_index;
}

public function lastProcessedIndex(): ?int
{
$data = $this->data();
if ($data) {
$lastProcessed = isset($data['lastProcessed']) ? $data['lastProcessed'] : null;
return $lastProcessed;
return $data['lastProcessed'] ?? null;
}
return null;
}

/**
* @param array<mixed>|array{}|null $data
* @return array<string|int, mixed>|array{}
*/
public function data($data = null): array
{
$default = [];
Expand All @@ -166,30 +177,47 @@ public function data($data = null): array
return session($this->sessionKeyName, $default);
}

/**
* @param string|int $key
*/
public function dataHas($key): bool
{
$data = $this->data();
return isset($data[$key]);
}

/**
* @param string|int $key
* @return array<mixed>
*/
public function dataGet($key)
{
$data = $this->data();
return $data[$key];
}

/**
* @param string|int $key
* @return array<mixed>
*/
public function dataStep(Step $step, $key): array
{
$data = $this->data();
$stepData = $data[$step::$slug][$key] ?? [];
return $stepData;
return $data[$step::$slug][$key] ?? [];
}

/**
* @return array<int, Step>
*/
public function all(): array
{
return $this->steps;
}

/**
* @param class-string<Step> $newStepClass
* @param string|int $key
*/
public function replaceStep(int $index, string $newStepClass, $key): Step
{
$step = $this->steps[$index];
Expand All @@ -200,6 +228,10 @@ public function replaceStep(int $index, string $newStepClass, $key): Step
return $this->steps[$index];
}

/**
* @param class-string<Step> $stepClass
* @param string|int $key
*/
public function appendStep(string $stepClass, $key): Step
{
$newIndex = count($this->steps);
Expand All @@ -208,13 +240,17 @@ public function appendStep(string $stepClass, $key): Step
return $this->steps[$newIndex];
}

/**
* @param string|int $key
* @param class-string<Step> $stepClass
*/
public function insertStep(int $index, string $stepClass, $key): Step
{
if ($index < 0) {
throw new InvalidArgumentException('Cannot set index below zero!');
}
$stepsCount = count($this->steps);
if ($index >= $stepsCount || $stepsCount === 0) {
if ($index >= $stepsCount) {
return $this->appendStep($stepClass, $key);
}

Expand Down Expand Up @@ -254,4 +290,4 @@ public function clearData(): void
{
$this->data([]);
}
}
}
14 changes: 12 additions & 2 deletions tests/WizardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Smajti1\LaravelWizard\Test;

use InvalidArgumentException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Smajti1\Laravel\Exceptions\StepNotFoundException;
Expand All @@ -17,11 +18,17 @@

class WizardTest extends TestCase
{
/** @var string */
protected $sessionKeyName;
/** @var string */
protected $wizardFirstStepKey;
/** @var array<int|string, class-string<Step>> */
protected $steps;
/** @var MockObject&Wizard */
protected $wizard;
/** @var ReflectionClass<Wizard> */
protected $wizard_reflection;
/** @var string */
protected $wizardThirdStepKey;

public function __construct()
Expand Down Expand Up @@ -77,6 +84,7 @@ public function testCreateStepClass(): void
$method->setAccessible(true);

$result = $method->invoke($this->wizard, $testStepClassName, 1, 'test_key', 2);
/** @phpstan-ignore-next-line */
self::assertInstanceOf($testStepClassName, $result);
self::assertInstanceOf(Step::class, $result);
}
Expand Down Expand Up @@ -180,11 +188,13 @@ public function testLastProcessedIndexWithoutData(): void
public function testWizardTestSteps(): void
{
$this->wizard->__construct($this->steps);
/** @var Step $nextStep */
$nextStep = $this->wizard->nextStep();
self::assertEquals($nextStep::$slug, SecondDumpStep::$slug);

$goBackToPrevStep = $this->wizard->prevStep();
self::assertEquals($goBackToPrevStep::$slug, FirstDumpStep::$slug);
/** @var Step $goBackToPrevStep */
$goBackToPrevStep = $this->wizard->prevStep();
self::assertEquals($goBackToPrevStep::$slug, FirstDumpStep::$slug);

$stepBySlug = $this->wizard->getBySlug(SecondDumpStep::$slug);
self::assertEquals($stepBySlug::$slug, SecondDumpStep::$slug);
Expand Down

0 comments on commit 2b83390

Please sign in to comment.