Skip to content

Commit

Permalink
fix: lint with php-cs-fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceAmstoutz committed Oct 8, 2024
1 parent 31f814c commit 5d20b78
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 24 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
->in(__DIR__)
->exclude([
'src/Core/Bridge/Symfony/Maker/Resources/skeleton',
'src/Laravel/Console/Maker/Resources/skeleton',
'src/Laravel/config',
'tests/Fixtures/app/var',
'docs/guides',
Expand Down
23 changes: 18 additions & 5 deletions src/Laravel/Console/Maker/MakeStateProviderCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Laravel\Console\Maker;

use ApiPlatform\Laravel\Console\Maker\Utils\AppServiceProviderTagger;
Expand All @@ -22,9 +33,8 @@ public function __construct(
private readonly Filesystem $filesystem,
private readonly StateProviderGenerator $stateProviderGenerator,
private readonly AppServiceProviderTagger $appServiceProviderTagger,
private readonly StateDirectoryManager $stateDirectoryManager
)
{
private readonly StateDirectoryManager $stateDirectoryManager,
) {
parent::__construct();
}

Expand All @@ -38,19 +48,22 @@ public function handle(): int

$filePath = $this->stateProviderGenerator->getFilePath($directoryPath, $providerName);
if ($this->stateProviderGenerator->isFileExists($filePath)) {
$this->error(sprintf('[ERROR] The file "%s" can\'t be generated because it already exists.', $filePath));
$this->error(\sprintf('[ERROR] The file "%s" can\'t be generated because it already exists.', $filePath));

return self::FAILURE;
}

$this->stateProviderGenerator->generate($filePath, $providerName);
if (!$this->filesystem->exists($filePath)) {
$this->error(sprintf('[ERROR] The file "%s" could not be created.', $filePath));
$this->error(\sprintf('[ERROR] The file "%s" could not be created.', $filePath));

return self::FAILURE;
}

$this->appServiceProviderTagger->addTagToServiceProvider($providerName);

$this->writeSuccessMessage($filePath);

return self::SUCCESS;
}

Expand Down
25 changes: 19 additions & 6 deletions src/Laravel/Console/Maker/Utils/AppServiceProviderTagger.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Laravel\Console\Maker\Utils;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\Filesystem;

final readonly class AppServiceProviderTagger
{
/** @var string */
/** @var string */
private const SERVICE_PROVIDER_PATH = 'Providers/AppServiceProvider.php';

/** @var string */
/** @var string */
private const ITEM_PROVIDER_USE_STATEMENT = 'use ApiPlatform\Laravel\Eloquent\State\ItemProvider;';

public function __construct(private Filesystem $filesystem) {}
public function __construct(private Filesystem $filesystem)
{
}

/**
* @throws FileNotFoundException
Expand All @@ -34,7 +47,7 @@ public function addTagToServiceProvider(string $providerName): void
private function ensureServiceProviderExists(string $path): void
{
if (!$this->filesystem->exists($path)) {
throw new \RuntimeException("The AppServiceProvider is missing!");
throw new \RuntimeException('The AppServiceProvider is missing!');
}
}

Expand All @@ -52,7 +65,7 @@ private function addUseStatement(string &$content, string $useStatement): void
private function addTag(string &$content, string $providerName, string $serviceProviderPath): void
{
$tagStatement = sprintf("\n\n\t\t\$this->app->tag(%s::class, ItemProvider::class);", $providerName);
$tagStatement = \sprintf("\n\n\t\t\$this->app->tag(%s::class, ItemProvider::class);", $providerName);
if (!str_contains($content, $tagStatement)) {
$content = preg_replace(
'/(public function register\(\)[^{]*{)(.*?)(\s*}\s*})/s',
Expand All @@ -66,6 +79,6 @@ private function addTag(string &$content, string $providerName, string $serviceP
public function getProviderNamespace(string $providerName): string
{
return sprintf('use App\\State\\%s;', $providerName);
return \sprintf('use App\\State\\%s;', $providerName);
}
}
15 changes: 14 additions & 1 deletion src/Laravel/Console/Maker/Utils/StateDirectoryManager.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Laravel\Console\Maker\Utils;

use Illuminate\Filesystem\Filesystem;

final readonly class StateDirectoryManager
{
public function __construct(private Filesystem $filesystem) {}
public function __construct(private Filesystem $filesystem)
{
}

public function ensureStateDirectoryExists(): string
{
Expand Down
19 changes: 16 additions & 3 deletions src/Laravel/Console/Maker/Utils/StateProviderGenerator.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Laravel\Console\Maker\Utils;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\Filesystem;

final readonly class StateProviderGenerator
{
public function __construct(private Filesystem $filesystem) {}
public function __construct(private Filesystem $filesystem)
{
}

public function getFilePath(string $directoryPath, string $providerName): string
{
return $directoryPath . $providerName . '.php';
return $directoryPath.$providerName.'.php';
}

public function isFileExists(string $filePath): bool
Expand Down Expand Up @@ -40,7 +53,7 @@ public function generate(string $pathLink, string $providerName): void
*/
private function loadTemplate(): string
{
$templatePath = dirname(__DIR__) . '/Resources/skeleton/StateProvider.tpl.php';
$templatePath = \dirname(__DIR__).'/Resources/skeleton/StateProvider.tpl.php';

return $this->filesystem->get($templatePath);
}
Expand Down
13 changes: 12 additions & 1 deletion src/Laravel/Console/Maker/Utils/SuccessMessageTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Laravel\Console\Maker\Utils;

trait SuccessMessageTrait
Expand All @@ -11,7 +22,7 @@ private function writeSuccessMessage(string $filePath): void
$this->line(' <bg=green;fg=white> Success! </>');
$this->line(' <bg=green;fg=white> </>');
$this->newLine();
$this->line('<fg=blue>created</>: <fg=white;options=underscore>' . $filePath . '</>');
$this->line('<fg=blue>created</>: <fg=white;options=underscore>'.$filePath.'</>');
$this->newLine();
$this->line('Next: Open your new state provider class and start customizing it.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@
use Illuminate\Filesystem\Filesystem;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole;


class MakeStateProviderCommandTest extends TestCase
{
use InteractsWithConsole;
use WithWorkbench;

private ?Filesystem $filesystem;
Expand Down Expand Up @@ -78,7 +75,7 @@ public function testWhenStateProviderClassAlreadyExists(): void
$existingFile = $this->pathResolver->generateStateProviderFilename($providerName);
$this->filesystem->put($existingFile, '<?php // Existing provider');

$expectedError = sprintf('[ERROR] The file "%s" can\'t be generated because it already exists.', $existingFile);
$expectedError = \sprintf('[ERROR] The file "%s" can\'t be generated because it already exists.', $existingFile);

$this->artisan('make:state-provider')
->expectsQuestion('Choose a class name for your state provider (e.g. <fg=yellow>AwesomeStateProvider</>)', $providerName)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
Expand Down
18 changes: 15 additions & 3 deletions src/Laravel/Tests/Console/Maker/Utils/AppServiceFileGenerator.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Laravel\Tests\Console\Maker\Utils;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\Filesystem;

final readonly class AppServiceFileGenerator
{

public function __construct(private Filesystem $filesystem) {}
public function __construct(private Filesystem $filesystem)
{
}

/**
* @throws FileNotFoundException
*/
public function regenerateProviderFile(): void
{
$templatePath = dirname(__DIR__) . '/Resources/skeleton/AppServiceProvider.tpl.php';
$templatePath = \dirname(__DIR__).'/Resources/skeleton/AppServiceProvider.tpl.php';
$targetPath = base_path('app/Providers/AppServiceProvider.php');

$this->regenerateFileFromTemplate($templatePath, $targetPath);
Expand Down
13 changes: 12 additions & 1 deletion src/Laravel/Tests/Console/Maker/Utils/PathResolver.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Laravel\Tests\Console\Maker\Utils;

final readonly class PathResolver
Expand All @@ -11,7 +22,7 @@ public function getServiceProviderFilePath(): string

public function generateStateProviderFilename(string $providerName): string
{
return $this->getStateDirectoryPath() . $providerName . '.php';
return $this->getStateDirectoryPath().$providerName.'.php';
}

public function getStateDirectoryPath(): string
Expand Down

0 comments on commit 5d20b78

Please sign in to comment.