Skip to content

Commit

Permalink
feat: add strict types and return types across Dusk package classes
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Feb 22, 2025
1 parent ea3c632 commit 7e4a10f
Show file tree
Hide file tree
Showing 28 changed files with 165 additions and 97 deletions.
18 changes: 13 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
"tastyigniter/core": "^v4.0@beta"
},
"require-dev": {
"laravel/pint": "^1.2",
"larastan/larastan": "^2.4.0",
"sampoyigi/testbench": "dev-main as 1.0",
"pestphp/pest-plugin-laravel": "^2.0"
"laravel/pint": "^1.2",
"pestphp/pest-plugin-laravel": "^2.0",
"rector/rector": "^1.2",
"sampoyigi/testbench": "dev-main as 1.0"
},
"autoload": {
"psr-4": {
Expand All @@ -42,9 +43,16 @@
},
"scripts": {
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage",
"test-coverage": "vendor/bin/pest --coverage --exactly=100 --compact",
"type-coverage": "vendor/bin/pest --type-coverage --min=100",
"format": "vendor/bin/pint",
"static": "vendor/bin/phpstan analyse --ansi --memory-limit 1056M"
"refactor": "vendor/bin/rector process --dry-run",
"static": "vendor/bin/phpstan analyse --ansi --memory-limit 1056M",
"test-suite": [
"@refactor",
"@static",
"@test"
]
},
"config": {
"allow-plugins": {
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ parameters:
count: 1
path: src/Classes/DuskTestCase.php

-
message: "#^Method Igniter\\\\System\\\\Classes\\\\ExtensionManager\\:\\:loadExtension\\(\\) invoked with 2 parameters, 1 required\\.$#"
count: 1
path: src/Classes/DuskTestCase.php

-
message: "#^Call to method getUser\\(\\) on an unknown class Igniter\\\\User\\\\Auth\\\\Manager\\.$#"
count: 1
Expand Down
23 changes: 23 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;

return RectorConfig::configure()
->withImportNames(removeUnusedImports: true)
->withPaths([__DIR__.'/src', __DIR__.'/tests'])
->withRules([
DeclareStrictTypesRector::class,
])
->withSkip([
ReturnTypeFromStrictNewArrayRector::class,
])
->withPreparedSets(
deadCode: true,
codeQuality: true,
codingStyle: true,
typeDeclarations: true,
);
6 changes: 5 additions & 1 deletion src/Classes/AdminPage.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php

declare(strict_types=1);

namespace Igniter\Dusk\Classes;

abstract class AdminPage extends \Laravel\Dusk\Page
use Laravel\Dusk\Page;

abstract class AdminPage extends Page
{
/**
* Get the global element shortcuts for the site.
Expand Down
24 changes: 13 additions & 11 deletions src/Classes/DuskTestCase.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Igniter\Dusk\Classes;

use Facebook\WebDriver\Chrome\ChromeOptions;
Expand All @@ -16,23 +18,23 @@

abstract class DuskTestCase extends BaseTestCase
{
use CreatesApplication, RunsMigrations, TestsExtensions;

use CreatesApplication;
use RunsMigrations;
use TestsExtensions;
/**
* Prepare for Dusk test execution.
*
* @beforeClass
* @return void
*/
public static function prepare()
public static function prepare(): void
{
static::startChromeDriver();
}

/**
* Create the RemoteWebDriver instance.
*
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
* @return RemoteWebDriver
*/
protected function driver()
{
Expand All @@ -53,7 +55,7 @@ protected function driver()
/**
* Register the base URL with Dusk.
*/
public function setUp(): void
protected function setUp(): void
{
$this->resetManagers();

Expand Down Expand Up @@ -88,9 +90,9 @@ public function setUp(): void
$this->registerBrowserMacros();
}

public function tearDown(): void
protected function tearDown(): void
{
if ($this->usingTestDatabase && isset($this->testDatabasePath)) {
if ($this->usingTestDatabase && $this->testDatabasePath !== null) {
unlink($this->testDatabasePath);
}

Expand All @@ -100,7 +102,7 @@ public function tearDown(): void
/**
* Return the default user to authenticate.
*
* @return \Igniter\User\Models\User|int|null
* @return User|int|null
*/
protected function user()
{
Expand All @@ -114,10 +116,10 @@ protected function user()
*/
protected function registerBrowserMacros()
{
Browser::macro('hasClass', function(string $selector, string $class) {
Browser::macro('hasClass', function(string $selector, string $class): bool {
$classes = preg_split('/\s+/', $this->attribute($selector, 'class'), -1, PREG_SPLIT_NO_EMPTY);

if (empty($classes)) {
if ($classes === [] || $classes === false) {
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Classes/Page.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Igniter\Dusk\Classes;

abstract class Page extends \Laravel\Dusk\Page
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/ComponentCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Igniter\Dusk\Commands;

class ComponentCommand extends GeneratorCommand
Expand Down
16 changes: 9 additions & 7 deletions src/Commands/DuskCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Igniter\Dusk\Commands;

use Igniter\System\Classes\ExtensionManager;
Expand Down Expand Up @@ -50,17 +52,17 @@ public function handle()

try {
$process->setTty(!$this->option('without-tty'));
} catch (RuntimeException $e) {
$this->output->writeln('Warning: '.$e->getMessage());
} catch (RuntimeException $runtimeException) {
$this->output->writeln('Warning: '.$runtimeException->getMessage());
}

try {
return $process->run(function($type, $line) {
return $process->run(function($type, $line): void {
$this->output->write($line);
});
} catch (ProcessSignaledException $e) {
if (extension_loaded('pcntl') && $e->getSignal() !== SIGINT) {
throw $e;
} catch (ProcessSignaledException $processSignaledException) {
if (extension_loaded('pcntl') && $processSignaledException->getSignal() !== SIGINT) {
throw $processSignaledException;
}
}
});
Expand Down Expand Up @@ -244,7 +246,7 @@ protected function duskFile()
return 'extensions/igniter/dusk/stubs/.env.dusk';
}

protected function duskPhpUnitXmlFile()
protected function duskPhpUnitXmlFile(): string
{
return extension_path('igniter/dusk/stubs/.phpunit.dusk.xml.stub');
}
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/DuskFailsCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Igniter\Dusk\Commands;

class DuskFailsCommand extends \Laravel\Dusk\Console\DuskFailsCommand {}
4 changes: 3 additions & 1 deletion src/Commands/GeneratorCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Igniter\Dusk\Commands;

use Illuminate\Support\Str;
Expand All @@ -14,7 +16,7 @@ public function handle()
if (count($array = explode('.', $code)) != 2) {
$this->error('Invalid extension name, Example name: AuthorName.ExtensionName');

return;
return null;
}

$this->extensionNamespace = $array;
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/MakeCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Igniter\Dusk\Commands;

class MakeCommand extends GeneratorCommand
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/PageCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Igniter\Dusk\Commands;

class PageCommand extends GeneratorCommand
Expand Down
9 changes: 7 additions & 2 deletions src/Concerns/CreatesApplication.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?php

declare(strict_types=1);

namespace Igniter\Dusk\Concerns;

use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Application;

trait CreatesApplication
{
/**
Expand All @@ -21,13 +26,13 @@ trait CreatesApplication
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
* @return Application
*/
public function createApplication()
{
$app = require __DIR__.'/../../../../bootstrap/app.php';

$app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();
$app->make(Kernel::class)->bootstrap();

$app['cache']->setDefaultDriver('array');
$app->setLocale('en');
Expand Down
2 changes: 2 additions & 0 deletions src/Concerns/RunsMigrations.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Igniter\Dusk\Concerns;

use Illuminate\Support\Facades\Artisan;
Expand Down
20 changes: 12 additions & 8 deletions src/Concerns/TestsExtensions.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<?php

declare(strict_types=1);

namespace Igniter\Dusk\Concerns;

use ReflectionClass;
use InvalidArgumentException;
use RuntimeException;
use Artisan;
use Igniter\System\Classes\ExtensionManager;
use Igniter\System\Classes\UpdateManager;

Expand Down Expand Up @@ -49,12 +55,10 @@ protected function detectExtensionDependencies($extension): void

/**
* Locates the extension code based on the test file location.
*
* @return string|bool
*/
protected function guessExtensionCodeFromTest()
protected function guessExtensionCodeFromTest(): string|false
{
$reflect = new \ReflectionClass($this);
$reflect = new ReflectionClass($this);
$path = $reflect->getFilename();
$basePath = $this->app->extensionsPath();

Expand All @@ -80,7 +84,7 @@ protected function runExtensionRefreshCommand($code, $throwException = true): vo
return;
}

throw new \InvalidArgumentException(sprintf('Invalid extension code: "%s"', $code));
throw new InvalidArgumentException(sprintf('Invalid extension code: "%s"', $code));
}

$extensionManager = resolve(ExtensionManager::class);
Expand All @@ -94,17 +98,17 @@ protected function runExtensionRefreshCommand($code, $throwException = true): vo
return;
}

throw new \RuntimeException(sprintf('Unable to find extension with code: "%s"', $code));
throw new RuntimeException(sprintf('Unable to find extension with code: "%s"', $code));
}

$extension = $extensionManager->loadExtension($namespace, $path);
$extension = $extensionManager->loadExtension($namespace);
}

$this->testCaseLoadedExtensions[$code] = $extension;

$this->detectExtensionDependencies($extension);

// Execute the command
\Artisan::call('extension:refresh', ['name' => $code]);
Artisan::call('extension:refresh', ['name' => $code]);
}
}
Loading

0 comments on commit 7e4a10f

Please sign in to comment.