Skip to content

Commit

Permalink
feat: add phpstan init command
Browse files Browse the repository at this point in the history
  • Loading branch information
Trusted97 committed Aug 7, 2023
1 parent 8c56ae7 commit a305842
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dist/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 6
paths:
- src
- tests
27 changes: 27 additions & 0 deletions src/Command/PhpStanCommand.php
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();
}
}
80 changes: 80 additions & 0 deletions src/Service/PhpStanService.php
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
);
}
}

0 comments on commit a305842

Please sign in to comment.