Skip to content

Commit

Permalink
Cleanups & doc (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Sep 28, 2017
1 parent 70f2f0c commit ee672a3
Show file tree
Hide file tree
Showing 19 changed files with 127 additions and 118 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ file that was distributed with this source code.
<file>src/Console/Application.php</file>
<file>src/Console/Configuration.php</file>
<file>src/Console/Command/SelfUpdateCommand.php</file>
<directory>src/Logger</directory>
<directory>src/NodeVisitor</directory>
<file>src/functions.php</file>
</exclude>
</whitelist>
Expand Down
7 changes: 3 additions & 4 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

final class Application extends SymfonyApplication
{
/** @private */
const LOGO = <<<'ASCII'
private const LOGO = <<<'ASCII'
____ __ ______ _____
/ __ \/ / / / __ \ / ___/_________ ____ ___ _____
Expand All @@ -34,7 +33,7 @@ final class Application extends SymfonyApplication
/**
* @inheritDoc
*/
public function getLongVersion()
public function getLongVersion(): string
{
return sprintf(
'<info>%s</info> version <comment>%s</comment>',
Expand All @@ -46,7 +45,7 @@ public function getLongVersion()
/**
* @inheritdoc
*/
public function getHelp()
public function getHelp(): string
{
return self::LOGO.parent::getHelp();
}
Expand Down
36 changes: 15 additions & 21 deletions src/Console/Command/AddPrefixCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -64,7 +56,7 @@ public function __construct(Filesystem $fileSystem, HandleAddPrefix $handle)
/**
* @inheritdoc
*/
protected function configure()
protected function configure(): void
{
$this
->setName('add-prefix')
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand All @@ -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);

Expand Down
61 changes: 31 additions & 30 deletions src/Console/Command/SelfUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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)
Expand All @@ -112,19 +106,22 @@ 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);
$this->updater->getStrategy()->setPharName(self::REMOTE_FILENAME);
$this->updater->getStrategy()->setCurrentLocalVersion($this->version);
}

private function update()
private function update(): void
{
$this->logger->startUpdating();

try {
$result = $this->updater->update();

Expand All @@ -136,56 +133,60 @@ 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;

try {
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());
}
}
24 changes: 8 additions & 16 deletions src/Console/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -129,20 +121,20 @@ 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'],
array_keys($config)
);
}

private static function validateConfigKey(string $key)
private static function validateConfigKey(string $key): void
{
if (false === in_array($key, self::KEYWORDS)) {
throw new InvalidArgumentException(
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/HandleAddPrefix.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit ee672a3

Please sign in to comment.