-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
parameters: | ||
level: 6 | ||
paths: | ||
- src | ||
- tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Whitecat\Command; | ||
|
||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use Whitecat\Service\PhpStanService; | ||
|
||
#[AsCommand( | ||
name: 'phpstan:init', | ||
description: 'Setup basic phpstan for PHP library', | ||
hidden: false | ||
)] | ||
class PhpStanCommand extends Command | ||
{ | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$fs = new Filesystem(); | ||
|
||
return (new PhpStanService($io, $fs))->run(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Whitecat\Service; | ||
|
||
use PHPUnit\Framework\Attributes\CoversNothing; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\Filesystem\Path; | ||
use Whitecat\Enums\DirectoryPath; | ||
use Whitecat\Exception\InvalidComposerException; | ||
use Whitecat\Helper\ComposerHelper; | ||
use Whitecat\Helper\DirectoryCopyHelper; | ||
use Whitecat\Helper\FileCopyHelper; | ||
|
||
class PhpStanService | ||
{ | ||
protected readonly string $workflowDirectoryPath; | ||
protected FileCopyHelper $fileCopyHelper; | ||
protected ComposerHelper $composerHelper; | ||
|
||
public function __construct( | ||
protected readonly SymfonyStyle $io, | ||
protected readonly Filesystem $fs | ||
) { | ||
$this->workflowDirectoryPath = Path::normalize(DirectoryPath::WORKFLOW->value); | ||
$this->fileCopyHelper = new FileCopyHelper($this->io, $this->fs); | ||
$this->composerHelper = new ComposerHelper(); | ||
} | ||
|
||
public function run(): int | ||
{ | ||
$this->io->title('Setup basic PHPStan'); | ||
|
||
try { | ||
$composer = $this->composerHelper->getComposerContent( | ||
composerPath: './composer.json' | ||
); | ||
} catch (\JsonException $jsonException) { | ||
$errorMessage = \sprintf('Error: %s ', $jsonException->getMessage()); | ||
$this->io->error($errorMessage); | ||
|
||
return Command::FAILURE; | ||
} catch (InvalidComposerException) { | ||
$this->io->error('Invalid composer.json file!'); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
$requireDev = $composer['require-dev']; | ||
|
||
$isInstalledPhpUnit = \array_key_exists('phpstan/phpstan', $requireDev); | ||
|
||
if (!$isInstalledPhpUnit) { | ||
$this->io->warning('It seems that phpstan/phpstan is not installed'); | ||
$this->io->warning('Launch in terminal \'composer require --dev phpstan/phpstan\''); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
$this->addPHPUnitConfigFile(); | ||
|
||
$this->io->success('All work was correctly done!'); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
#[CoversNothing] | ||
protected function addPHPUnitConfigFile(): void | ||
{ | ||
$this->fileCopyHelper->copyFile( | ||
fileName: 'phpstan.neon', | ||
questionMessage: 'It seems that a phpstan.neon already exists, do you want to override?', | ||
overrideCommentMessage: 'Adding phpstan.neon', | ||
skippedMessage: 'Skipped creation of phpstan.neon', | ||
sourceFileDirectory: '', | ||
distFileDirectory: DirectoryPath::DIST_DIRECTORY->value | ||
); | ||
} | ||
} |