-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Implement operator whitespace fixer
And basic stuff, which is needed in every fixer.
- Loading branch information
Showing
40 changed files
with
7,613 additions
and
2 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.Build | ||
composer.lock | ||
composer.lock | ||
.phpunit.result.cache | ||
coverage |
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,4 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
require __DIR__ . '/../.Build/vendor/autoload.php'; |
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,19 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
if (is_file(__DIR__ . '/../autoload.php')) { | ||
require __DIR__.'/../autoload.php'; | ||
} else { | ||
require __DIR__ . '/autoload.php'; | ||
} | ||
|
||
use Pluswerk\TypoScriptAutoFixer\Command\FixCommand; | ||
use Symfony\Component\Console\Application; | ||
|
||
$application = new Application('test', '1.0.0'); | ||
$command = new FixCommand(); | ||
|
||
$application->add($command); | ||
|
||
$application->setDefaultCommand('fix'); | ||
$application->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 |
---|---|---|
|
@@ -49,5 +49,6 @@ | |
"grumphp": { | ||
"config-default-path": "grumphp.yml" | ||
} | ||
} | ||
}, | ||
"bin": "./bin/tscsf" | ||
} |
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,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.4/phpunit.xsd" | ||
bootstrap=".Build/vendor/autoload.php" | ||
executionOrder="depends,defects" | ||
forceCoversAnnotation="true" | ||
beStrictAboutCoversAnnotation="false" | ||
beStrictAboutOutputDuringTests="true" | ||
beStrictAboutTodoAnnotatedTests="true" | ||
verbose="true"> | ||
<extensions> | ||
<extension class="Pluswerk\TypoScriptAutoFixer\Hook\BypassFinalHook"/> | ||
</extensions> | ||
<testsuites> | ||
<testsuite name="default"> | ||
<directory suffix="Test.php">tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
|
||
<logging> | ||
<log type="coverage-html" target="coverage/html"/> | ||
<log type="coverage-clover" target="coverage/clover.xml"/> | ||
</logging> | ||
</phpunit> |
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,49 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Pluswerk\TypoScriptAutoFixer\Command; | ||
|
||
use Pluswerk\TypoScriptAutoFixer\Fixer\IssueFixer; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class FixCommand extends Command | ||
{ | ||
/** | ||
* @var IssueFixer | ||
*/ | ||
private $issueFixer; | ||
|
||
public function __construct(string $name = null) | ||
{ | ||
$name = $name ?? 'fix'; | ||
parent::__construct($name); | ||
$this->issueFixer = new IssueFixer(); | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$this->addArgument('files', InputArgument::IS_ARRAY, 'files to fix', []); | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* | ||
* @return int|null | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): ?int | ||
{ | ||
$files = $input->getArgument('files'); | ||
if (count($files) > 0) { | ||
foreach ($files as $file) { | ||
if (is_file($file)) { | ||
$this->issueFixer->fixIssuesForFile($file); | ||
} | ||
} | ||
} | ||
return 0; | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Pluswerk\TypoScriptAutoFixer\Exception; | ||
|
||
use RuntimeException; | ||
|
||
final class FileNotWritableException extends RuntimeException | ||
{ | ||
} |
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,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Pluswerk\TypoScriptAutoFixer\Exception; | ||
|
||
use Exception; | ||
|
||
final class InvalidIssueException extends Exception | ||
{ | ||
|
||
} |
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,10 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Pluswerk\TypoScriptAutoFixer\Exception; | ||
|
||
use RuntimeException; | ||
|
||
final class NoOperatorFoundException extends RuntimeException | ||
{ | ||
} |
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,10 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Pluswerk\TypoScriptAutoFixer\Exception; | ||
|
||
use RuntimeException; | ||
|
||
final class WriteFileFailedException extends RuntimeException | ||
{ | ||
} |
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,97 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Pluswerk\TypoScriptAutoFixer; | ||
|
||
use Pluswerk\TypoScriptAutoFixer\Exception\FileNotWritableException; | ||
use Pluswerk\TypoScriptAutoFixer\Exception\WriteFileFailedException; | ||
use Pluswerk\TypoScriptAutoFixer\Issue\IssueCollection; | ||
|
||
final class File extends \SplFileInfo | ||
{ | ||
/** | ||
* @var IssueCollection | ||
*/ | ||
private $issues; | ||
|
||
public function __construct($file_name) | ||
{ | ||
parent::__construct($file_name); | ||
$this->issues = new IssueCollection(); | ||
} | ||
|
||
/** | ||
* @param int $line | ||
* | ||
* @return string | ||
*/ | ||
public function readLine(int $line): string | ||
{ | ||
$fileObject = $this->openFile('r'); | ||
$fileObject->seek($line - 1); | ||
return $fileObject->current(); | ||
} | ||
|
||
/** | ||
* @param string $lineValue | ||
* @param int $line | ||
* | ||
* @todo: Try to make this method a little bit nicer ;-). | ||
*/ | ||
public function replaceLine(string $lineValue, int $line): void | ||
{ | ||
$fileObject = $this->openFile('r'); | ||
$content = ''; | ||
$fileObject->rewind(); | ||
|
||
while (!$fileObject->eof()) { | ||
if ($fileObject->key() === ($line - 1)) { | ||
$content .= $lineValue; | ||
|
||
if (strpos($fileObject->current(), PHP_EOL) !== false) { | ||
$content .= PHP_EOL; | ||
} | ||
} else { | ||
$content .= $fileObject->current(); | ||
} | ||
|
||
$fileObject->next(); | ||
} | ||
|
||
$this->overwriteFileContent($content); | ||
} | ||
|
||
/** | ||
* @param IssueCollection $issueCollection | ||
*/ | ||
public function updateIssueCollection(IssueCollection $issueCollection): void | ||
{ | ||
$this->issues = $issueCollection; | ||
} | ||
|
||
/** | ||
* @param $content | ||
*/ | ||
private function overwriteFileContent($content): void | ||
{ | ||
try { | ||
$writeFileObject = $this->openFile('w'); | ||
} catch (\RuntimeException $e) { | ||
throw new FileNotWritableException($e->getMessage()); | ||
} | ||
|
||
$writeResult = $writeFileObject->fwrite($content); | ||
|
||
if ($writeResult === false || $writeResult === null) { | ||
throw new WriteFileFailedException('write file ' . $this->getPathname() . ' failed!'); | ||
} | ||
} | ||
|
||
/** | ||
* @return IssueCollection | ||
*/ | ||
public function issues(): IssueCollection | ||
{ | ||
return $this->issues; | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Pluswerk\TypoScriptAutoFixer; | ||
|
||
final class FileBuilder | ||
{ | ||
/** | ||
* @var Linter | ||
*/ | ||
private $linter; | ||
|
||
/** | ||
* FileBuilder constructor. | ||
* | ||
* @param Linter|null $linter | ||
*/ | ||
public function __construct(Linter $linter = null) | ||
{ | ||
$this->linter = $linter ?? new Linter(); | ||
} | ||
|
||
/** | ||
* @param string $filePath | ||
* | ||
* @return File | ||
*/ | ||
public function buildFile(string $filePath): File | ||
{ | ||
$file = new File($filePath); | ||
|
||
$issueCollection = $this->linter->lint($filePath); | ||
|
||
$file->updateIssueCollection($issueCollection); | ||
|
||
return $file; | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Pluswerk\TypoScriptAutoFixer\Fixer; | ||
|
||
use Pluswerk\TypoScriptAutoFixer\FileBuilder; | ||
|
||
abstract class AbstractFixer implements FixerInterface | ||
{ | ||
/** | ||
* @var FileBuilder | ||
*/ | ||
protected $fileBuilder; | ||
|
||
/** | ||
* AbstractFixer constructor. | ||
* | ||
* @param FileBuilder $fileBuilder | ||
*/ | ||
public function __construct(FileBuilder $fileBuilder = null) | ||
{ | ||
$this->fileBuilder = $fileBuilder ?? new FileBuilder(); | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Pluswerk\TypoScriptAutoFixer\Fixer; | ||
|
||
use Pluswerk\TypoScriptAutoFixer\Fixer\OperatorWhitespace\OperatorWhitespaceFixer; | ||
use Pluswerk\TypoScriptAutoFixer\Issue\AbstractIssue; | ||
|
||
final class FixerFactory | ||
{ | ||
/** | ||
* @param AbstractIssue $issue | ||
* | ||
* @return FixerInterface|null | ||
*/ | ||
public function getFixerByIssue(AbstractIssue $issue): ?FixerInterface | ||
{ | ||
return new OperatorWhitespaceFixer(); | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Pluswerk\TypoScriptAutoFixer\Fixer; | ||
|
||
use Pluswerk\TypoScriptAutoFixer\File; | ||
use Pluswerk\TypoScriptAutoFixer\Issue\AbstractIssue; | ||
|
||
interface FixerInterface | ||
{ | ||
/** | ||
* @param File $file | ||
* @param AbstractIssue $issue | ||
* | ||
* @return File | ||
*/ | ||
public function fixIssue(File $file, AbstractIssue $issue): File; | ||
} |
Oops, something went wrong.