diff --git a/README.md b/README.md index 71519d3..1e5403b 100644 --- a/README.md +++ b/README.md @@ -10,54 +10,30 @@ Function resolution without the backslash forces the PHP internals to verify for **Idea from Nikita Popov talk**: - [PHP 7 – What changed internally? (PHP Barcelona 2015)](http://www.slideshare.net/nikita_ppv/php-7-what-changed-internally-php-barcelona-2015) (slide [72](http://image.slidesharecdn.com/php7internals-151101105627-lva1-app6891/95/php-7-what-changed-internally-php-barcelona-2015-72-638.jpg?cb=1446375542)) -## Usage - -``` -$ php bin/php_backslasher fix -``` - - ## Installation -### As a dependency using Composer Use [Composer](https://getcomposer.org) to install the package: ``` $ composer require nilportugues/php_backslasher ``` -### As a PHAR file - -You can also use already last built `.phar`. +## Usage -``` bash -$ git clone git@github.com:nilportugues/php_backslasher.git -$ cd php_backslasher -$ php build/php_backslasher.phar ``` - -You can copy the `.phar` file as a global script - -``` bash -$ cp build/php_backslasher.phar /usr/local/bin/php_backslasher +$ php bin/php_backslasher fix ``` +###Output -## Building the PHAR: +Works for function in conditional statements, negative conditionals, placed in an array as key or value and any other normal use. -While the PHAR file is included under `bin/php_backslasher`, but can be built using the following command: +```php +echo strlen('Hello World'); +// becomes: +echo \strlen('Hello World'); ``` -$ php -d phar.readonly=false box.phar build -``` - -You may also like to make it runnable by just giving it permissions to be used as an executable file and hide its extension. - -``` -$ chmod 755 bin/php_backslasher.phar -$ mv bin/php_backslasher.phar bin/php_backslasher -``` - ## Contribute diff --git a/bin/compile b/bin/compile deleted file mode 100755 index 225759d..0000000 --- a/bin/compile +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env php -compile(); - -} catch (\Exception $e) { - echo 'Failed to compile phar: ['.get_class($e).'] '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine(); - exit(1); -} \ No newline at end of file diff --git a/bin/php_backslasher b/bin/php_backslasher index 8a2925b..0f3fce9 100644 --- a/bin/php_backslasher +++ b/bin/php_backslasher @@ -1,26 +1,6 @@ #!/usr/bin/env php - */ - -require __DIR__ . '/../src/bootstrap.php'; - -use NilPortugues\BackslashFixer\Console\Application; - -error_reporting(-1); -ini_set('display_errors', 1); - -// run the command application -$application = new Application(); +$application = new \NilPortugues\BackslashFixer\Console\Application(); $application->run(); \ No newline at end of file diff --git a/build/.gitkeep b/build/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/composer.json b/composer.json index 989595b..a45f844 100644 --- a/composer.json +++ b/composer.json @@ -33,10 +33,6 @@ "fabpot/php-cs-fixer": "1.4.2", "phpunit/phpunit": "4.5.0" }, - "require-dev": { - "phpunit/phpunit": "^5.0", - "fabpot/php-cs-fixer": "^2.0@dev" - }, "autoload": { "psr-4": { "NilPortugues\\BackslashFixer\\": "src/BackslashFixer/" diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7dfba47..b9e25fd 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -42,8 +42,8 @@ - - - + + + diff --git a/src/BackslashFixer/Compiler/Compiler.php b/src/BackslashFixer/Compiler/Compiler.php deleted file mode 100644 index 0d34669..0000000 --- a/src/BackslashFixer/Compiler/Compiler.php +++ /dev/null @@ -1,302 +0,0 @@ -loadVersion(); - - /** - * Creating phar object - */ - $phar = new Phar($pharFilePath, 0, 'php_backslasher.phar'); - $phar->setSignatureAlgorithm(\Phar::SHA1); - - $phar->startBuffering(); - - $this - ->addPHPFiles($phar) - ->addVendorFiles($phar) - ->addComposerVendorFiles($phar) - ->addBin($phar) - ->addStub($phar) - ->addLicense($phar); - - $phar->stopBuffering(); - - unset($phar); - } - - /** - * Add a file into the phar package - * - * @param Phar $phar Phar object - * @param SplFileInfo $file File to add - * @param bool $strip strip - * - * @return Compiler self Object - */ - protected function addFile( - Phar $phar, - SplFileInfo $file, - $strip = true - ) { - $path = \strtr(\str_replace(\dirname(dirname(\dirname(__DIR__))) . DIRECTORY_SEPARATOR, '', $file->getRealPath()), '\\', '/'); - $content = \file_get_contents($file); - if ($strip) { - $content = $this->stripWhitespace($content); - } elseif ('LICENSE' === \basename($file)) { - $content = "\n" . $content . "\n"; - } - - if ($path === 'src/Composer/Composer.php') { - $content = \str_replace('@package_version@', $this->version, $content); - $content = \str_replace('@release_date@', $this->versionDate, $content); - } - - $phar->addFromString($path, $content); - - return $this; - } - - /** - * Add bin into Phar - * - * @param Phar $phar Phar - * - * @return Compiler self Object - */ - protected function addBin(Phar $phar) - { - $content = \file_get_contents(__DIR__ . '/../../../bin/php_backslasher'); - $content = \preg_replace('{^#!/usr/bin/env php\s*}', '', $content); - $phar->addFromString('bin/php_backslasher', $content); - - return $this; - } - - /** - * Removes whitespace from a PHP source string while preserving line numbers. - * - * @param string $source A PHP string - * - * @return string The PHP string with the whitespace removed - */ - protected function stripWhitespace($source) - { - if (!function_exists('token_get_all')) { - return $source; - } - - $output = ''; - foreach (\token_get_all($source) as $token) { - if (\is_string($token)) { - $output .= $token; - } elseif (\in_array($token[0], [T_COMMENT, T_DOC_COMMENT])) { - $output .= \str_repeat("\n", \substr_count($token[1], "\n")); - } elseif (T_WHITESPACE === $token[0]) { - // reduce wide spaces - $whitespace = \preg_replace('{[ \t]+}', ' ', $token[1]); - // normalize newlines to \n - $whitespace = \preg_replace('{(?:\r\n|\r|\n)}', "\n", $whitespace); - // trim leading spaces - $whitespace = \preg_replace('{\n +}', "\n", $whitespace); - $output .= $whitespace; - } else { - $output .= $token[1]; - } - } - - return $output; - } - - protected function addStub(Phar $phar) - { - $stub = <<<'EOF' -#!/usr/bin/env php - - * Date: 11/1/15 - * Time: 12:30 AM - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -Phar::mapPhar('php_backslasher.phar'); - -require 'phar://php_backslasher.phar/bin/php_backslasher'; - -__HALT_COMPILER(); -EOF; - $phar->setStub($stub); - - return $this; - } - - /** - * Add php files - * - * @param Phar $phar Phar instance - * - * @return Compiler self Object - */ - private function addPHPFiles(Phar $phar) - { - /** - * All *.php files - */ - $finder = new Finder(); - $finder - ->files() - ->ignoreVCS(true) - ->name('*.php') - ->notName('Compiler.php') - ->notName('ClassLoader.php') - ->in(\realpath(__DIR__ . '/../../../src')); - - foreach ($finder as $file) { - $this->addFile($phar, $file); - } - - return $this; - } - - /** - * Add vendor files - * - * @param Phar $phar Phar instance - * - * @return Compiler self Object - */ - private function addVendorFiles(Phar $phar) - { - $vendorPath = __DIR__ . '/../../../vendor/'; - - /** - * All *.php files - */ - $finder = new Finder(); - $finder - ->files() - ->ignoreVCS(true) - ->name('*.php') - ->exclude('Tests') - ->in(\realpath($vendorPath . 'symfony/')); - - foreach ($finder as $file) { - $this->addFile($phar, $file); - } - - return $this; - } - - /** - * Add composer vendor files - * - * @param Phar $phar Phar - * - * @return Compiler self Object - */ - private function addComposerVendorFiles(Phar $phar) - { - $vendorPath = __DIR__ . '/../../../vendor/'; - - /** - * Adding composer vendor files - */ - $this - ->addFile($phar, new \SplFileInfo($vendorPath . 'autoload.php')) - ->addFile($phar, new \SplFileInfo($vendorPath . 'composer/autoload_namespaces.php')) - ->addFile($phar, new \SplFileInfo($vendorPath . 'composer/autoload_psr4.php')) - ->addFile($phar, new \SplFileInfo($vendorPath . 'composer/autoload_classmap.php')) - ->addFile($phar, new \SplFileInfo($vendorPath . 'composer/autoload_real.php')) - ->addFile($phar, new \SplFileInfo($vendorPath . 'composer/ClassLoader.php')); - - if (\file_exists($vendorPath . 'composer/include_paths.php')) { - $this->addFile($phar, new \SplFileInfo($vendorPath . 'composer/include_paths.php')); - } - - return $this; - } - - /** - * Add license - * - * @param Phar $phar Phar - * - * @return Compiler self Object - */ - private function addLicense(Phar $phar) - { - $this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../../LICENSE'), false); - - return $this; - } - - /** - * Load versions - */ - private function loadVersion() - { - $process = new Process('git log --pretty="%H" -n1 HEAD', __DIR__); - if ($process->run() != 0) { - throw new \RuntimeException('Can\'t run git log. You must ensure to run compile from php_backslasher git repository clone and that git binary is available.'); - } - $this->version = \trim($process->getOutput()); - - $process = new Process('git log -n1 --pretty=%ci HEAD', __DIR__); - if ($process->run() != 0) { - throw new \RuntimeException('Can\'t run git log. You must ensure to run compile from php_backslasher git repository clone and that git binary is available.'); - } - $date = new \DateTime(\trim($process->getOutput())); - $date->setTimezone(new \DateTimeZone('UTC')); - $this->versionDate = $date->format('Y-m-d H:i:s'); - - $process = new Process('git describe --tags HEAD'); - if ($process->run() == 0) { - $this->version = \trim($process->getOutput()); - } - - return $this; - } -} diff --git a/src/BackslashFixer/Console/Application.php b/src/BackslashFixer/Console/Application.php index 7c6dd40..0f1bb31 100644 --- a/src/BackslashFixer/Console/Application.php +++ b/src/BackslashFixer/Console/Application.php @@ -4,13 +4,9 @@ namespace NilPortugues\BackslashFixer\Console; use NilPortugues\BackslashFixer\Command\FixerCommand; -use Symfony\Component\Console\Application as BaseApplication; -/** - * Class Application - */ -class Application extends BaseApplication +class Application extends \Symfony\Component\Console\Application { /** * Construct method @@ -22,6 +18,8 @@ public function __construct() /** * Initializes all the composer commands + * + * @return \Symfony\Component\Console\Command\Command[] */ protected function getDefaultCommands() { diff --git a/src/bootstrap.php b/src/bootstrap.php index 768d6b1..6f3d95b 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -2,7 +2,7 @@ function includeIfExists($file) { - return \file_exists($file) ? include_once $file : false; + return \file_exists($file) ? include $file : false; } if ((!$loader = includeIfExists(__DIR__ . '/../vendor/autoload.php')) && (!$loader = includeIfExists(__DIR__ . '/../../../autoload.php'))) {