From ee672a369a4b1e98f3509ee577683fc5b35fee6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Thu, 28 Sep 2017 09:15:25 +0100 Subject: [PATCH] Cleanups & doc (#104) --- README.md | 5 ++ phpunit.xml.dist | 2 - src/Console/Application.php | 7 +-- src/Console/Command/AddPrefixCommand.php | 36 +++++------ src/Console/Command/SelfUpdateCommand.php | 61 ++++++++++--------- src/Console/Configuration.php | 24 +++----- src/Handler/HandleAddPrefix.php | 2 +- src/Logger/ConsoleLogger.php | 16 ++--- src/Logger/UpdateConsoleLogger.php | 21 ++++--- src/NodeVisitor/AppendParentNode.php | 11 ++-- .../Collection/NamespaceStmtCollection.php | 7 ++- .../UseStmt/GroupUseStmtTransformer.php | 2 +- src/NodeVisitor/WhitelistedClassAppender.php | 23 +++++++ src/Scoper/PhpScoper.php | 9 +-- src/Scoper/TraverserFactory.php | 2 +- .../AddPrefixCommandIntegrationTest.php | 2 +- .../Console/Command/AddPrefixCommandTest.php | 2 +- tests/Handler/HandleAddPrefixTest.php | 4 +- tests/Scoper/PhpScoperTest.php | 9 +-- 19 files changed, 127 insertions(+), 118 deletions(-) diff --git a/README.md b/README.md index c90b579b..b1aa9725 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,8 @@ composer require --dev bamarni/composer-bin-plugin composer bin php-scoper require --dev humbug/php-scoper ``` +Keep in mind however that this library is not designed to be extended. + ## Usage @@ -287,6 +289,9 @@ return [ Note that only classes are whitelisted, this does not affect constants or functions. +For whitelist to work, you then require to load `vendor/scoper-autoload.php` +instead of the traditional `vendor/autoload.php`. + ## Contributing diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 330fab67..b0f70bfb 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -28,8 +28,6 @@ file that was distributed with this source code. src/Console/Application.php src/Console/Configuration.php src/Console/Command/SelfUpdateCommand.php - src/Logger - src/NodeVisitor src/functions.php diff --git a/src/Console/Application.php b/src/Console/Application.php index 3a6a097b..45207400 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -18,8 +18,7 @@ final class Application extends SymfonyApplication { - /** @private */ - const LOGO = <<<'ASCII' + private const LOGO = <<<'ASCII' ____ __ ______ _____ / __ \/ / / / __ \ / ___/_________ ____ ___ _____ @@ -34,7 +33,7 @@ final class Application extends SymfonyApplication /** * @inheritDoc */ - public function getLongVersion() + public function getLongVersion(): string { return sprintf( '%s version %s', @@ -46,7 +45,7 @@ public function getLongVersion() /** * @inheritdoc */ - public function getHelp() + public function getHelp(): string { return self::LOGO.parent::getHelp(); } diff --git a/src/Console/Command/AddPrefixCommand.php b/src/Console/Command/AddPrefixCommand.php index 43dc926b..3dfc6b8a 100644 --- a/src/Console/Command/AddPrefixCommand.php +++ b/src/Console/Command/AddPrefixCommand.php @@ -30,22 +30,14 @@ final class AddPrefixCommand extends Command { - /** @internal */ - const PATH_ARG = 'paths'; - /** @internal */ - const PREFIX_OPT = 'prefix'; - /** @internal */ - const OUTPUT_DIR_OPT = 'output-dir'; - /** @internal */ - const FORCE_OPT = 'force'; - /** @internal */ - const STOP_ON_FAILURE_OPT = 'stop-on-failure'; - /** @internal */ - const CONFIG_FILE_OPT = 'config'; - /** @internal */ - const CONFIG_FILE_DEFAULT = 'scoper.inc.php'; - /** @internal */ - const WORKING_DIR_OPT = 'working-dir'; + private const PATH_ARG = 'paths'; + private const PREFIX_OPT = 'prefix'; + private const OUTPUT_DIR_OPT = 'output-dir'; + private const FORCE_OPT = 'force'; + private const STOP_ON_FAILURE_OPT = 'stop-on-failure'; + private const CONFIG_FILE_OPT = 'config'; + private const CONFIG_FILE_DEFAULT = 'scoper.inc.php'; + private const WORKING_DIR_OPT = 'working-dir'; private $fileSystem; private $handle; @@ -64,7 +56,7 @@ public function __construct(Filesystem $fileSystem, HandleAddPrefix $handle) /** * @inheritdoc */ - protected function configure() + protected function configure(): void { $this ->setName('add-prefix') @@ -122,7 +114,7 @@ protected function configure() /** * @inheritdoc */ - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); @@ -168,9 +160,11 @@ protected function execute(InputInterface $input, OutputInterface $output) } $logger->outputScopingEnd(); + + return 0; } - private function validatePrefix(InputInterface $input) + private function validatePrefix(InputInterface $input): void { $prefix = $input->getOption(self::PREFIX_OPT); @@ -196,7 +190,7 @@ private function validatePrefix(InputInterface $input) $input->setOption(self::PREFIX_OPT, $prefix); } - private function validatePaths(InputInterface $input) + private function validatePaths(InputInterface $input): void { $cwd = getcwd(); $fileSystem = $this->fileSystem; @@ -215,7 +209,7 @@ function (string $path) use ($cwd, $fileSystem) { $input->setArgument(self::PATH_ARG, $paths); } - private function validateOutputDir(InputInterface $input, OutputStyle $io) + private function validateOutputDir(InputInterface $input, OutputStyle $io): void { $outputDir = $input->getOption(self::OUTPUT_DIR_OPT); diff --git a/src/Console/Command/SelfUpdateCommand.php b/src/Console/Command/SelfUpdateCommand.php index 3e4afb31..e1159637 100644 --- a/src/Console/Command/SelfUpdateCommand.php +++ b/src/Console/Command/SelfUpdateCommand.php @@ -16,28 +16,22 @@ use Humbug\PhpScoper\Logger\UpdateConsoleLogger; use Humbug\SelfUpdate\Updater; +use PHAR; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Throwable; final class SelfUpdateCommand extends Command { - /** @internal */ - const REMOTE_FILENAME = 'php-scoper.phar'; - /** @internal */ - const STABILITY_STABLE = 'stable'; - /** @internal */ - const PACKAGIST_PACKAGE_NAME = 'humbug/php-scoper'; - /** @internal */ - const ROLLBACK_OPT = 'rollback'; - /** @internal */ - const CHECK_OPT = 'check'; + private const REMOTE_FILENAME = 'php-scoper.phar'; + private const STABILITY_STABLE = 'stable'; + private const PACKAGIST_PACKAGE_NAME = 'humbug/php-scoper'; + private const ROLLBACK_OPT = 'rollback'; + private const CHECK_OPT = 'check'; - /** - * @var Updater - */ private $updater; /** @@ -89,7 +83,7 @@ protected function configure() /** * @inheritdoc */ - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { $this->logger = new UpdateConsoleLogger( new SymfonyStyle($input, $output) @@ -112,9 +106,11 @@ protected function execute(InputInterface $input, OutputInterface $output) } $this->update(); + + return 0; } - private function configureUpdater() + private function configureUpdater(): void { $this->updater->setStrategy(Updater::STRATEGY_GITHUB); $this->updater->getStrategy()->setPackageName(self::PACKAGIST_PACKAGE_NAME); @@ -122,9 +118,10 @@ private function configureUpdater() $this->updater->getStrategy()->setCurrentLocalVersion($this->version); } - private function update() + private function update(): void { $this->logger->startUpdating(); + try { $result = $this->updater->update(); @@ -136,34 +133,37 @@ private function update() } else { $this->logger->updateNotNeeded($oldVersion); } - } catch (\Throwable $e) { - $this->logger->error($e); - throw $e; + } catch (Throwable $throwable) { + $this->logger->error($throwable); + + throw $throwable; } } - private function rollback() + private function rollback(): void { try { $result = $this->updater->rollback(); + if ($result) { $this->logger->rollbackSuccess(); } else { $this->logger->rollbackFail(); } - } catch (\Throwable $e) { - $this->logger->error($e); - throw $e; + } catch (Throwable $throwable) { + $this->logger->error($throwable); + + throw $throwable; } } - private function printAvailableUpdates() + private function printAvailableUpdates(): void { $this->logger->printLocalVersion($this->version); $this->printCurrentStableVersion(); } - private function printCurrentStableVersion() + private function printCurrentStableVersion(): void { $stability = self::STABILITY_STABLE; @@ -171,21 +171,22 @@ private function printCurrentStableVersion() if ($this->updater->hasUpdate()) { $this->logger->printRemoteVersion( $stability, - $updater->getNewVersion() + $this->updater->getNewVersion() ); } elseif (false == $this->updater->getNewVersion()) { $this->logger->noNewRemoteVersions($stability); } else { $this->logger->currentVersionInstalled($stability); } - } catch (\Throwable $e) { - $this->logger->error($e); - throw $e; + } catch (Throwable $throwable) { + $this->logger->error($throwable); + + throw $throwable; } } private function getLocalPharName(): string { - return basename(\PHAR::running()); + return basename(PHAR::running()); } } diff --git a/src/Console/Configuration.php b/src/Console/Configuration.php index 90715c88..84ff7f4c 100644 --- a/src/Console/Configuration.php +++ b/src/Console/Configuration.php @@ -19,20 +19,12 @@ final class Configuration { - /** @internal */ - const FINDER_KEYWORD = 'finders'; + private const FINDER_KEYWORD = 'finders'; + private const PATCHERS_KEYWORD = 'patchers'; + private const WHITELIST_KEYWORD = 'whitelist'; + private const GLOBAL_NAMESPACE_KEYWORD = 'global_namespace_whitelist'; - /** @internal */ - const PATCHERS_KEYWORD = 'patchers'; - - /** @internal */ - const WHITELIST_KEYWORD = 'whitelist'; - - /** @internal */ - const GLOBAL_NAMESPACE_KEYWORD = 'global_namespace_whitelist'; - - /** @internal */ - const KEYWORDS = [ + private const KEYWORDS = [ self::FINDER_KEYWORD, self::PATCHERS_KEYWORD, self::WHITELIST_KEYWORD, @@ -129,12 +121,12 @@ public function getWhitelist(): array /** * @return callable[]|string[] */ - public function getGlobalNamespaceWhitelisters() + public function getGlobalNamespaceWhitelisters(): array { return $this->globalNamespaceWhitelisters; } - private static function validateConfigKeys(array $config) + private static function validateConfigKeys(array $config): void { array_map( ['self', 'validateConfigKey'], @@ -142,7 +134,7 @@ private static function validateConfigKeys(array $config) ); } - private static function validateConfigKey(string $key) + private static function validateConfigKey(string $key): void { if (false === in_array($key, self::KEYWORDS)) { throw new InvalidArgumentException( diff --git a/src/Handler/HandleAddPrefix.php b/src/Handler/HandleAddPrefix.php index b51be000..3c65d1b4 100644 --- a/src/Handler/HandleAddPrefix.php +++ b/src/Handler/HandleAddPrefix.php @@ -250,7 +250,7 @@ private function scopeFile( callable $globalWhitelister, bool $stopOnFailure, ConsoleLogger $logger - ) { + ): void { try { $scoppedContent = $this->scoper->scope($inputFilePath, $prefix, $patchers, $whitelist, $globalWhitelister); } catch (Throwable $error) { diff --git a/src/Logger/ConsoleLogger.php b/src/Logger/ConsoleLogger.php index 1557013c..4be0d2b3 100644 --- a/src/Logger/ConsoleLogger.php +++ b/src/Logger/ConsoleLogger.php @@ -44,7 +44,7 @@ public function __construct(Application $application, SymfonyStyle $io) * @param string $prefix * @param string[] $paths */ - public function outputScopingStart(string $prefix, array $paths) + public function outputScopingStart(string $prefix, array $paths): void { $this->io->writeln($this->application->getHelp()); @@ -71,7 +71,7 @@ public function outputScopingStart(string $prefix, array $paths) * * @param int $count */ - public function outputFileCount(int $count) + public function outputFileCount(int $count): void { if (OutputInterface::VERBOSITY_NORMAL === $this->io->getVerbosity()) { $this->progressBar = $this->io->createProgressBar($count); @@ -86,7 +86,7 @@ public function outputFileCount(int $count) * * @param string $path */ - public function outputSuccess(string $path) + public function outputSuccess(string $path): void { if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { $this->io->writeln( @@ -100,7 +100,7 @@ public function outputSuccess(string $path) $this->progressBar->advance(); } - public function outputWarnOfFailure(string $path, ParsingException $exception) + public function outputWarnOfFailure(string $path, ParsingException $exception): void { if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { $this->io->writeln( @@ -128,7 +128,7 @@ public function outputWarnOfFailure(string $path, ParsingException $exception) * * @param string $path */ - public function outputFail(string $path) + public function outputFail(string $path): void { if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { $this->io->writeln( @@ -142,17 +142,17 @@ public function outputFail(string $path) $this->progressBar->advance(); } - public function outputScopingEnd() + public function outputScopingEnd(): void { $this->finish(false); } - public function outputScopingEndWithFailure() + public function outputScopingEndWithFailure(): void { $this->finish(true); } - private function finish(bool $failed) + private function finish(bool $failed): void { $this->progressBar->finish(); $this->io->newLine(2); diff --git a/src/Logger/UpdateConsoleLogger.php b/src/Logger/UpdateConsoleLogger.php index 28cab272..b9b5d566 100644 --- a/src/Logger/UpdateConsoleLogger.php +++ b/src/Logger/UpdateConsoleLogger.php @@ -15,6 +15,7 @@ namespace Humbug\PhpScoper\Logger; use Symfony\Component\Console\Style\SymfonyStyle; +use Throwable; /** * @private @@ -32,12 +33,12 @@ public function __construct(SymfonyStyle $io) $this->io = $io; } - public function startUpdating() + public function startUpdating(): void { $this->io->writeln('Updating...'); } - public function updateSuccess(string $newVersion, string $oldVersion) + public function updateSuccess(string $newVersion, string $oldVersion): void { $this->io->success('PHP-Scoper has been updated.'); $this->io->writeln(sprintf( @@ -50,7 +51,7 @@ public function updateSuccess(string $newVersion, string $oldVersion) )); } - public function updateNotNeeded(string $oldVersion) + public function updateNotNeeded(string $oldVersion): void { $this->io->writeln('PHP-Scoper is currently up to date.'); $this->io->writeln(sprintf( @@ -59,22 +60,22 @@ public function updateNotNeeded(string $oldVersion) )); } - public function error(\Throwable $e) + public function error(Throwable $e): void { $this->io->error('Unexpected error. If updating, your original phar is untouched.'); } - public function rollbackSuccess() + public function rollbackSuccess(): void { $this->io->success('PHP-Scoper has been rolled back to prior version.'); } - public function rollbackFail() + public function rollbackFail(): void { $this->io->error('Rollback failed for reasons unknown.'); } - public function printLocalVersion(string $version) + public function printLocalVersion(string $version): void { $this->io->writeln(sprintf( 'Your current local version is: %s', @@ -82,7 +83,7 @@ public function printLocalVersion(string $version) )); } - public function printRemoteVersion(string $stability, string $version) + public function printRemoteVersion(string $stability, string $version): void { $this->io->writeln(sprintf( 'The current %s build available remotely is: %s', @@ -91,12 +92,12 @@ public function printRemoteVersion(string $stability, string $version) )); } - public function noNewRemoteVersions(string $stability) + public function noNewRemoteVersions(string $stability): void { $this->io->writeln(sprintf('There are no new %s builds available.', $stability)); } - public function currentVersionInstalled(string $stability) + public function currentVersionInstalled(string $stability): void { $this->io->writeln(sprintf('You have the current %s build installed.', $stability)); } diff --git a/src/NodeVisitor/AppendParentNode.php b/src/NodeVisitor/AppendParentNode.php index 000e1aa1..b7aded2f 100644 --- a/src/NodeVisitor/AppendParentNode.php +++ b/src/NodeVisitor/AppendParentNode.php @@ -23,8 +23,7 @@ */ final class AppendParentNode extends NodeVisitorAbstract { - /** @private */ - const PARENT_ATTRIBUTE = 'parent'; + private const PARENT_ATTRIBUTE = 'parent'; private $stack; @@ -41,9 +40,11 @@ public static function getParent(Node $node): Node /** * @inheritdoc */ - public function beforeTraverse(array $nodes) + public function beforeTraverse(array $nodes): ?array { $this->stack = []; + + return $nodes; } /** @@ -63,8 +64,10 @@ public function enterNode(Node $node): Node /** * @inheritdoc */ - public function leaveNode(Node $node) + public function leaveNode(Node $node): Node { array_pop($this->stack); + + return $node; } } diff --git a/src/NodeVisitor/Collection/NamespaceStmtCollection.php b/src/NodeVisitor/Collection/NamespaceStmtCollection.php index 44c2d328..811cfe85 100644 --- a/src/NodeVisitor/Collection/NamespaceStmtCollection.php +++ b/src/NodeVisitor/Collection/NamespaceStmtCollection.php @@ -56,11 +56,12 @@ public function findNamespaceForNode(Node $node): ?Name return null; } - if (1 < count($this->nodes)) { - return $this->getNodeNamespace($node); + // Shortcut if there is only one namespace + if (1 === count($this->nodes)) { + return $this->nodes[0]->name; } - return $this->nodes[0]->name; + return $this->getNodeNamespace($node); } public function getCurrentNamespaceName(): ?Name diff --git a/src/NodeVisitor/UseStmt/GroupUseStmtTransformer.php b/src/NodeVisitor/UseStmt/GroupUseStmtTransformer.php index d779185d..19c51069 100644 --- a/src/NodeVisitor/UseStmt/GroupUseStmtTransformer.php +++ b/src/NodeVisitor/UseStmt/GroupUseStmtTransformer.php @@ -42,7 +42,7 @@ final class GroupUseStmtTransformer extends NodeVisitorAbstract /** * @inheritdoc */ - public function beforeTraverse(array $nodes) + public function beforeTraverse(array $nodes): array { $newNodes = []; diff --git a/src/NodeVisitor/WhitelistedClassAppender.php b/src/NodeVisitor/WhitelistedClassAppender.php index aee2eac8..5ba496d1 100644 --- a/src/NodeVisitor/WhitelistedClassAppender.php +++ b/src/NodeVisitor/WhitelistedClassAppender.php @@ -26,6 +26,29 @@ use PhpParser\Node\Stmt\Namespace_; use PhpParser\NodeVisitorAbstract; +/** + * Appends a `class_alias` to the whitelisted classes. + * + * ``` + * namespace A; + * + * class Foo + * { + * } + * ``` + * + * => + * + * ``` + * namespace Humbug\A; + * + * class Foo + * { + * } + * + * class_alias('Humbug\A\Foo', 'A\Foo', false); + * ``` + */ final class WhitelistedClassAppender extends NodeVisitorAbstract { private $whitelist; diff --git a/src/Scoper/PhpScoper.php b/src/Scoper/PhpScoper.php index c3b741c0..2311dfe4 100644 --- a/src/Scoper/PhpScoper.php +++ b/src/Scoper/PhpScoper.php @@ -21,12 +21,9 @@ final class PhpScoper implements Scoper { - /** @internal */ - const FILE_PATH_PATTERN = '/.*\.php$/'; - /** @internal */ - const NOT_FILE_BINARY = '/\..+?$/'; - /** @internal */ - const PHP_BINARY = '/^#!.+?php.*\n{1,}<\?php/'; + private const FILE_PATH_PATTERN = '/.*\.php$/'; + private const NOT_FILE_BINARY = '/\..+?$/'; + private const PHP_BINARY = '/^#!.+?php.*\n{1,}<\?php/'; private $parser; private $decoratedScoper; diff --git a/src/Scoper/TraverserFactory.php b/src/Scoper/TraverserFactory.php index 3ca8e790..2e00fd27 100644 --- a/src/Scoper/TraverserFactory.php +++ b/src/Scoper/TraverserFactory.php @@ -26,7 +26,7 @@ final class TraverserFactory /** * Functions for which the arguments will be prefixed. */ - const WHITELISTED_FUNCTIONS = [ + public const WHITELISTED_FUNCTIONS = [ 'class_exists', 'interface_exists', ]; diff --git a/tests/Console/Command/AddPrefixCommandIntegrationTest.php b/tests/Console/Command/AddPrefixCommandIntegrationTest.php index 49065058..8381855c 100644 --- a/tests/Console/Command/AddPrefixCommandIntegrationTest.php +++ b/tests/Console/Command/AddPrefixCommandIntegrationTest.php @@ -29,7 +29,7 @@ */ class AddPrefixCommandIntegrationTest extends TestCase { - const FIXTURE_PATH = __DIR__.'/../../../fixtures/set002/original'; + private const FIXTURE_PATH = __DIR__.'/../../../fixtures/set002/original'; /** * @var ApplicationTester diff --git a/tests/Console/Command/AddPrefixCommandTest.php b/tests/Console/Command/AddPrefixCommandTest.php index 7146bb55..426874e7 100644 --- a/tests/Console/Command/AddPrefixCommandTest.php +++ b/tests/Console/Command/AddPrefixCommandTest.php @@ -34,7 +34,7 @@ */ class AddPrefixCommandTest extends TestCase { - const FIXTURE_PATH = __DIR__.'/../../../fixtures'; + private const FIXTURE_PATH = __DIR__.'/../../../fixtures'; /** * @var ApplicationTester diff --git a/tests/Handler/HandleAddPrefixTest.php b/tests/Handler/HandleAddPrefixTest.php index 535dcfeb..91aa99d3 100644 --- a/tests/Handler/HandleAddPrefixTest.php +++ b/tests/Handler/HandleAddPrefixTest.php @@ -36,8 +36,8 @@ */ class HandleAddPrefixTest extends TestCase { - const FIXTURE_PATH_000 = __DIR__.'/../../fixtures/set000'; - const FIXTURE_PATH_001 = __DIR__.'/../../fixtures/set001'; + private const FIXTURE_PATH_000 = __DIR__.'/../../fixtures/set000'; + private const FIXTURE_PATH_001 = __DIR__.'/../../fixtures/set001'; /** * @var Scoper|ObjectProphecy diff --git a/tests/Scoper/PhpScoperTest.php b/tests/Scoper/PhpScoperTest.php index d16ed9cb..ac53f713 100644 --- a/tests/Scoper/PhpScoperTest.php +++ b/tests/Scoper/PhpScoperTest.php @@ -30,15 +30,10 @@ use function Humbug\PhpScoper\make_tmp_dir; use function Humbug\PhpScoper\remove_dir; -/** - * @covers \Humbug\PhpScoper\Scoper\PhpScoper - */ class PhpScoperTest extends TestCase { - /** @private */ - const SPECS_PATH = __DIR__.'/../../specs'; - /** @private */ - const SECONDARY_SPECS_PATH = __DIR__.'/../../_specs'; + private const SPECS_PATH = __DIR__.'/../../specs'; + private const SECONDARY_SPECS_PATH = __DIR__.'/../../_specs'; /** * @var Scoper