Skip to content

Commit

Permalink
Backslashing constants too
Browse files Browse the repository at this point in the history
  • Loading branch information
Nil Portugués committed Nov 3, 2015
1 parent 8564b7f commit 4e1b8f9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/BackslashFixer/Command/FixerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function configure()
* @param InputInterface $input Input
* @param OutputInterface $output Output
*
* @return int|null|void
* @return int|\null|void
*
* @throws Exception
*/
Expand All @@ -58,7 +58,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$fileEditor->addBackslashesToFunctions($file);
}

$output->write('Success!', true);
$output->write('Success!', \true);

return $output;
}
Expand Down
30 changes: 30 additions & 0 deletions src/BackslashFixer/Fixer/FileEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class FileEditor
*/
private static $characters = [" ", "(", ",", "!", "[", "="];

/**
* @var array
*/
private static $constants = [];

/**
*
*/
Expand All @@ -44,6 +49,8 @@ public function addBackslashesToFunctions($path)
$source = \str_replace($functions, $this->buildBackslashedFunctions($character), $source);
}

$source = \str_replace($this->getDefinedConstants(), $this->replaceConstants(), $source);
$source = \str_replace(['true', 'false', 'null'], ['\true', '\false', '\null'], $source);
$source = \str_replace("function \\", "function ", $source);

\file_put_contents($path, $source);
Expand Down Expand Up @@ -136,4 +143,27 @@ private function buildFunctions($previousCharacter = ' ')
$functions = \array_map($callback, $functions);
return $functions;
}

/**
* @return array
*/
private function replaceConstants()
{
$constants = $this->getDefinedConstants();
$callback = function ($v) {
return sprintf('\%s', $v);
};
return \array_map($callback, $constants);
}

/**
* @return array
*/
private function getDefinedConstants()
{
if(empty(self::$constants)) {
self::$constants = \array_keys(\get_defined_constants(\false));
}
return self::$constants;
}
}
2 changes: 1 addition & 1 deletion src/BackslashFixer/Fixer/PhpFilesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PhpFilesRepository
{
public function find($path)
{
if (false === \is_dir($path) && false === \is_file($path)) {
if (\false === \is_dir($path) && \false === \is_file($path)) {
throw new InvalidArgumentException("Provided input is not a file nor a valid directory");
}

Expand Down
8 changes: 4 additions & 4 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

function includeIfExists($file)
{
return \file_exists($file) ? include $file : false;
return \file_exists($file) ? include $file : \false;
}

if ((!$loader = includeIfExists(__DIR__ . '/../vendor/autoload.php')) && (!$loader = includeIfExists(__DIR__ . '/../../../autoload.php'))) {
echo 'You must set up the project dependencies, run the following commands:' . PHP_EOL .
'curl -sS https://getcomposer.org/installer | php' . PHP_EOL .
'php composer.phar install' . PHP_EOL;
echo 'You must set up the project dependencies, run the following commands:' . \PHP_EOL .
'curl -sS https://getcomposer.org/installer | php' . \PHP_EOL .
'php composer.phar install' . \PHP_EOL;
exit(1);
}

Expand Down

0 comments on commit 4e1b8f9

Please sign in to comment.