Skip to content

Commit

Permalink
Merge pull request #1 from XPocketProX/main
Browse files Browse the repository at this point in the history
Fix
  • Loading branch information
ClousCloud authored Sep 22, 2024
2 parents ef86b81 + 9fda610 commit 43b4169
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 29 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"symfony/console": "^5.0"
},
"require-dev": {
"phpstan/phpstan": "^1.11.5",
"phpunit/phpunit": "^1.0.0"
"phpstan/phpstan": "^1.10 || ^1.11",
"phpunit/phpunit": "^9 || ^10"
}
}
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
parameters:
level: max
paths:
- src
- tests
excludePaths:
- vendor
checkMissingIterableValueType: true
checkGenericClassInNonGenericObjectType: true
checkMissingTypehints: true
10 changes: 10 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="Unit Tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
36 changes: 23 additions & 13 deletions src/FixerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,46 @@

class FixerManager
{
/** @var FixerInterface[] */
private array $fixers;

/** @var array<string, mixed> */
private array $config;

public function __construct()
{
// Memuat konfigurasi dari file
$this->config = require __DIR__ . '/../config/.php_cs_config';

$this->fixers = [];

// Daftarkan fixer sesuai dengan konfigurasi
if ($this->config['rules']['indentation']) {
$this->fixers[] = new Fixers\IndentationFixer();
$this->fixers[] = new fixers\IndentationFixer();
}

if ($this->config['rules']['line_length']) {
$this->fixers[] = new Fixers\LineLengthFixer($this->config['rules']['line_length']);
$this->fixers[] = new fixers\LineLengthFixer($this->config['rules']['line_length']);
}
}

public function fix(string $path)
{
$files = glob($path . '/*.php');
foreach ($files as $file) {
$content = file_get_contents($file);
public function fix(string $path): void
{
$files = glob($path . '/*.php');
if ($files === false) {
return;
}

foreach ($this->fixers as $fixer) {
$content = $fixer->fix($content);
}
foreach ($files as $file) {
$content = file_get_contents($file);
if ($content === false) {
continue;
}

file_put_contents($file, $content);
foreach ($this->fixers as $fixer) {
$content = $fixer->fix($content);
}

file_put_contents($file, $content);
}
}

}
27 changes: 19 additions & 8 deletions src/fixers/Command/FixCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,37 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use phpcodestyle\fixers\FixerManager;
use phpcodestyle\FixerManager;

class FixCommand extends Command
{
protected static $defaultName = 'fix';
private FixerManager $fixerManager;

protected function configure()
public function __construct(FixerManager $fixerManager)
{
$this->fixerManager = $fixerManager;
}

protected function configure(): void
{
$this
->setDescription('Fix PHP code style.')
->addArgument('path', InputArgument::REQUIRED, 'The path to the PHP files.');
->addArgument('path', InputArgument::REQUIRED, 'The path to the PHP files.', 'string');
}

protected function execute(InputInterface $input, OutputInterface $output)
public function execute(InputInterface $input, OutputInterface $output): int
{
$path = $input->getArgument('path');
$manager = new FixerManager();
$manager->fix($path);

$output->writeln('Code style fixed.');

if (!is_string($path)) {
throw new \InvalidArgumentException('Path must be a string.');
}

$this->fixerManager->fix($path);

$output->writeln("<info>PHP code style fixed successfully.</info>");

return Command::SUCCESS;
}
}
5 changes: 3 additions & 2 deletions src/fixers/IndentationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ class IndentationFixer implements FixerInterface
{
public function fix(string $content): string
{
// Mengganti 4 spasi menjadi tab
return preg_replace('/^ {4}/m', "\t", $content);
$result = preg_replace('/^ {4}/m', "\t", $content);
return $result !== null ? $result : $content;
}


public function getDescription(): string
{
return "Converts 4 spaces to tabs.";
Expand Down
5 changes: 2 additions & 3 deletions src/fixers/LineLengthFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ public function __construct(int $maxLength)

public function fix(string $content): string
{
return preg_replace_callback('/^.{'.($this->maxLength + 1).',}$/m', function ($matches) {
return wordwrap($matches[0], $this->maxLength);
}, $content);
$result = preg_replace('/.{100}/', '$0' . PHP_EOL, $content);
return $result !== null ? $result : $content;
}

public function getDescription(): string
Expand Down
2 changes: 1 addition & 1 deletion tests/IndentationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class IndentationFixerTest extends TestCase
{
public function testFix()
public function testFix() : void
{
$fixer = new IndentationFixer();
$content = " function test() {}\n function anotherTest() {}";
Expand Down

0 comments on commit 43b4169

Please sign in to comment.