Skip to content

Commit

Permalink
Issue #1079 - Fix PHPCS problems
Browse files Browse the repository at this point in the history
  • Loading branch information
Sweetchuck committed Dec 8, 2021
1 parent 1cdf254 commit 932fed0
Show file tree
Hide file tree
Showing 75 changed files with 1,004 additions and 229 deletions.
66 changes: 53 additions & 13 deletions RoboFile.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
use Symfony\Component\Finder\Finder;

use Robo\Symfony\ConsoleIO;
use Robo\Tasks;
use Symfony\Component\Finder\Finder;

class RoboFile extends \Robo\Tasks
class RoboFile extends Tasks
{
const MAIN_BRANCH = '3.x';

Expand All @@ -14,12 +15,14 @@ class RoboFile extends \Robo\Tasks
* to run the tests. This command also runs the remaining Codeception
* tests. You must re-add Codeception to the project to use this.
*/
public function test(ConsoleIO $io, array $args, $options =
[
public function test(
ConsoleIO $io,
array $args,
$options = [
'coverage-html' => false,
'coverage' => false
])
{
]
) {
$io->warning("Deprecated: use 'composer test' instead. Codeception-based tests will fail.");

$collection = $this->collectionBuilder($io);
Expand All @@ -37,7 +40,7 @@ public function test(ConsoleIO $io, array $args, $options =
}

return $collection;
}
}

/**
* Code sniffer.
Expand All @@ -52,25 +55,63 @@ public function test(ConsoleIO $io, array $args, $options =
*/
public function sniff(
ConsoleIO $io,
$file = 'src/',
$file = '',
$options = [
'autofix' => false,
'strict' => false,
]
) {
$strict = $options['strict'] ? '' : '-n';
$result = $this->collectionBuilder($io)->taskExec("./vendor/bin/phpcs --standard=PSR2 {$strict} {$file}")->run();
$command = $this->getPhpcsCommand($file, $options);

$result = $this->collectionBuilder($io)->taskExec($command)->run();
if (!$result->wasSuccessful()) {
if (!$options['autofix']) {
$options['autofix'] = $this->confirm('Would you like to run phpcbf to fix the reported errors?');
}
if ($options['autofix']) {
$result = $this->taskExec("./vendor/bin/phpcbf --standard=PSR2 {$file}")->run();
$result = $this->taskExec($this->getPhpcbfCommand($file))->run();
}
}
return $result;
}

/**
* @param string $file
* @param array $options
*
* @return string
*/
protected function getPhpcsCommand($file = '', array $options = [])
{
$options += [
'strict' => false,
];

$cmdPattern = './vendor/bin/phpcs';
$cmdArgs = [];

if ($options['strict']) {
$cmdPattern .= ' -n';
}

if ($file !== '') {
$cmdPattern .= ' %s';
$cmdArgs[] = escapeshellarg($file);
}

return vsprintf($cmdPattern, $cmdArgs);
}

/**
* @param string $file
*
* @return string
*/
protected function getPhpcbfCommand($file = '')
{
return './vendor/bin/phpcbf' . ($file !== '' ? ' ' . escapeshellarg($file) : '');
}

/**
* Generate a new Robo task that wraps an existing utility class.
*
Expand All @@ -95,8 +136,7 @@ public function release(ConsoleIO $io, $opts = ['beta' => false])
$stable = !$opts['beta'];
if ($stable) {
$version = preg_replace('/-.*/', '', $version);
}
else {
} else {
$version = $this->incrementVersion($version, 'beta');
}
$this->writeVersion($this->collectionBuilder($io), $version);
Expand Down
14 changes: 11 additions & 3 deletions examples/src/Robo/Plugin/Commands/ExampleCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,14 @@ public function tryInput(ConsoleIO $io)
* configuration via the configuration key command.try.config.options.opt.
* @option show-all Also print out the value of all configuration options
*/
public function tryConfig(ConsoleIO $io, $key = 'options.progress-delay', $options = ['opt' => '0', 'show-all' => false])
{
public function tryConfig(
ConsoleIO $io,
$key = 'options.progress-delay',
$options = [
'opt' => '0',
'show-all' => false
]
) {
$value = \Robo\Robo::config()->get($key);

$io->say("The value of $key is " . var_export($value, true));
Expand Down Expand Up @@ -373,10 +379,12 @@ public function tryFormatters($somthing = 'default', $options = ['format' => 'ta
public function tryWrap()
{
$data = [
// phpcs:disable Generic.Files.LineLength.TooLong
[
'first' => 'This is a really long cell that contains a lot of data. When it is rendered, it should be wrapped across multiple lines.',
'second' => 'This is the second column of the same table. It is also very long, and should be wrapped across multiple lines, just like the first column.',
]
// phpcs:enable Generic.Files.LineLength.TooLong
];
return new RowsOfFields($data);
}
Expand Down Expand Up @@ -557,7 +565,7 @@ public function tryProgress(ConsoleIO $io, $options = ['delay' => 500])
->taskForEach($processList)
->iterationMessage('Processing {value}')
->call(
function ($value) use($delay) {
function ($value) use ($delay) {
// TaskForEach::call should only be used to do
// non-Robo operations. To use Robo tasks in an
// iterator, @see TaskForEach::withBuilder.
Expand Down
48 changes: 48 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0"?>
<ruleset
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"
name="Custom">

<file>./examples/</file>
<file>./src/</file>
<file>./tests/</file>
<exclude-pattern>./tests/_data/</exclude-pattern>
<exclude-pattern>./tests/_helpers/_generated/</exclude-pattern>
<exclude-pattern>./tests/_log/</exclude-pattern>
<exclude-pattern>./tests/_bootstrap.php</exclude-pattern>
<file>./RoboFile.php</file>

<rule ref="PSR2"/>
<rule ref="PSR2.Methods.MethodDeclaration.Underscore">
<!-- _before() _after() -->
<exclude-pattern>./tests/</exclude-pattern>

<!-- Task builders -->
<exclude-pattern>./src/Task/Base/Shortcuts.php</exclude-pattern>
<exclude-pattern>./src/Task/Filesystem/FilesystemStack.php</exclude-pattern>
<exclude-pattern>./src/Task/Filesystem/Shortcuts.php</exclude-pattern>
<exclude-pattern>./src/Task/Logfile/Shortcuts.php</exclude-pattern>
<exclude-pattern>./src/Task/Vcs/Shortcuts.php</exclude-pattern>

</rule>
<rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace">
<!-- @todo Tests also should be under namespaces. -->
<exclude-pattern>./tests/</exclude-pattern>
<exclude-pattern>./RoboFile.php</exclude-pattern>
</rule>
<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
<!-- @todo Fix this kind of errors. -->
<exclude-pattern>./examples/src/Robo/Plugin/Commands/ExampleCommands.php</exclude-pattern>
<exclude-pattern>./tests/unit/ConfigurationTest.php</exclude-pattern>
<exclude-pattern>./tests/unit/ResultTest.php</exclude-pattern>
</rule>
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<!-- @todo Fix this kind of errors. -->
<exclude-pattern>./tests/unit/ApplicationTest.php</exclude-pattern>
</rule>
<rule ref="Squiz.Classes.ValidClassName.NotCamelCaps">
<!-- @todo Fix this kind of errors. -->
<exclude-pattern>./tests/unit/AAA_RunnerErrorTest.php</exclude-pattern>
</rule>
</ruleset>
26 changes: 22 additions & 4 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,33 @@ public function __construct($name, $version)

$this->getDefinition()
->addOption(
new InputOption('--simulate', null, InputOption::VALUE_NONE, 'Run in simulated mode (show what would have happened).')
new InputOption(
'--simulate',
null,
InputOption::VALUE_NONE,
'Run in simulated mode (show what would have happened).'
)
);
$this->getDefinition()
->addOption(
new InputOption('--progress-delay', null, InputOption::VALUE_REQUIRED, 'Number of seconds before progress bar is displayed in long-running task collections. Default: 2s.', Config::DEFAULT_PROGRESS_DELAY)
new InputOption(
'--progress-delay',
null,
InputOption::VALUE_REQUIRED,
'Number of seconds before progress bar is displayed in long-running task collections. Default: 2s.',
Config::DEFAULT_PROGRESS_DELAY
)
);

$this->getDefinition()
->addOption(
new InputOption('--define', '-D', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Define a configuration item value.', [])
new InputOption(
'--define',
'-D',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Define a configuration item value.',
[]
)
);
}

Expand All @@ -43,8 +60,9 @@ public function addInitRoboFileCommand($roboFile, $roboClass)
$createRoboFile = new Command('init');
$createRoboFile->setDescription("Intitalizes basic RoboFile in current dir");
$createRoboFile->setCode(function (InputInterface $input, OutputInterface $output) use ($roboClass, $roboFile) {
$roboFileBaseName = basename($roboFile);
$output->writeln("<comment> ~~~ Welcome to Robo! ~~~~ </comment>");
$output->writeln("<comment> " . basename($roboFile) . " will be created in the current directory </comment>");
$output->writeln("<comment> $roboFileBaseName will be created in the current directory </comment>");
file_put_contents(
$roboFile,
'<?php'
Expand Down
2 changes: 1 addition & 1 deletion src/ClassDiscovery/RelativeNamespaceDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function getClasses()
if ($directories) {
foreach ($this->search($directories, $this->searchPattern) as $file) {
$relativePathName = $file->getRelativePathname();
$classes[] = $baseNamespace . $this->convertPathToNamespace($relativePath . '/' . $relativePathName);
$classes[] = $baseNamespace . $this->convertPathToNamespace("$relativePath/$relativePathName");
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/Collection/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,10 @@ public function getCommand()
return $task->getCommand();
}

throw new TaskException($task, get_class($task) . " does not implement CommandInterface, so can't be used to provide a command");
throw new TaskException(
$task,
get_class($task) . " does not implement CommandInterface, so can't be used to provide a command"
);
}

/**
Expand Down Expand Up @@ -648,9 +651,13 @@ protected function runRollbackTasks()
}

/**
* phpcs:disable Generic.Files.LineLength.TooLong
*
* @param \Robo\Contract\TaskInterface|\Robo\Collection\NestedCollectionInterface|\Robo\Contract\WrappedTaskInterface $task
*
* @return \Robo\Result
*
* phpcs:enable Generic.Files.LineLength.TooLongs
*/
protected function runSubtask($task)
{
Expand Down
27 changes: 21 additions & 6 deletions src/Collection/CollectionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@
* In the example above, the `taskDeleteDir` will be called if
* ```
*/
class CollectionBuilder extends BaseTask implements NestedCollectionInterface, WrappedTaskInterface, CommandInterface, StateAwareInterface, InputAwareInterface
class CollectionBuilder extends BaseTask implements
NestedCollectionInterface,
WrappedTaskInterface,
CommandInterface,
StateAwareInterface,
InputAwareInterface
{
use StateAwareTrait;
use InputAwareTrait; // BaseTask has OutputAwareTrait
Expand Down Expand Up @@ -339,7 +344,9 @@ public function defer($callback)
*/
protected function callCollectionStateFunction($functionName, $args)
{
$currentTask = ($this->currentTask instanceof WrappedTaskInterface) ? $this->currentTask->original() : $this->currentTask;
$currentTask = $this->currentTask instanceof WrappedTaskInterface ?
$this->currentTask->original()
: $this->currentTask;

array_unshift($args, $currentTask);
$collection = $this->getCollection();
Expand Down Expand Up @@ -369,7 +376,9 @@ protected function callCollectionStateFuntion($functionName, $args)
*/
public function setVerbosityThreshold($verbosityThreshold)
{
$currentTask = ($this->currentTask instanceof WrappedTaskInterface) ? $this->currentTask->original() : $this->currentTask;
$currentTask = $this->currentTask instanceof WrappedTaskInterface ?
$this->currentTask->original()
: $this->currentTask;
if ($currentTask) {
$currentTask->setVerbosityThreshold($verbosityThreshold);
return $this;
Expand Down Expand Up @@ -455,7 +464,9 @@ public function __call($fn, $args)
$temporaryBuilder = $this->commandFile->getBuiltTask($fn, $args);
$this->commandFile->setBuilder($saveBuilder);
if (!$temporaryBuilder) {
throw new \BadMethodCallException("No such method $fn: task does not exist in " . get_class($this->commandFile));
throw new \BadMethodCallException(
"No such method $fn: task does not exist in " . get_class($this->commandFile)
);
}
$temporaryBuilder->getCollection()->transferTasks($this);
return $this;
Expand All @@ -468,7 +479,9 @@ public function __call($fn, $args)
$result = call_user_func_array([$this->currentTask, $fn], $args);

// If something other than a setter method is called, then return its result.
$currentTask = ($this->currentTask instanceof WrappedTaskInterface) ? $this->currentTask->original() : $this->currentTask;
$currentTask = $this->currentTask instanceof WrappedTaskInterface ?
$this->currentTask->original()
: $this->currentTask;
if (isset($result) && ($result !== $currentTask)) {
return $result;
}
Expand Down Expand Up @@ -644,7 +657,9 @@ public function getCollection()
$this->collection = new Collection();
$this->collection->inflect($this);
$this->collection->setState($this->getState());
$this->collection->setProgressBarAutoDisplayInterval($this->getConfig()->get(Config::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL));
$this->collection->setProgressBarAutoDisplayInterval(
$this->getConfig()->get(Config::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL)
);

if (isset($this->currentTask)) {
$this->collection->add($this->currentTask);
Expand Down
5 changes: 4 additions & 1 deletion src/Common/CommandReceiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ protected function receiveCommand($command)
if ($command instanceof CommandInterface) {
return $command->getCommand();
} else {
throw new TaskException($this, get_class($command) . " does not implement CommandInterface, so can't be passed into this task");
throw new TaskException(
$this,
get_class($command) . " does not implement CommandInterface, so can't be passed into this task"
);
}
}
}
6 changes: 5 additions & 1 deletion src/Common/ProcessExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
use Robo\Contract\VerbosityThresholdInterface;
use Symfony\Component\Process\Process;

class ProcessExecutor implements ConfigAwareInterface, LoggerAwareInterface, OutputAwareInterface, VerbosityThresholdInterface
class ProcessExecutor implements
ConfigAwareInterface,
LoggerAwareInterface,
OutputAwareInterface,
VerbosityThresholdInterface
{
use ExecTrait;
use TaskIO; // uses LoggerAwareTrait and ConfigAwareTrait
Expand Down
6 changes: 5 additions & 1 deletion src/Common/TaskIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ public function logger()

static $gaveDeprecationWarning = false;
if (!$gaveDeprecationWarning) {
trigger_error('No logger set for ' . get_class($this) . '. Use $this->task(Foo::class) rather than new Foo() in Tasks to ensure the builder can initialize task the task, or use $this->collectionBuilder()->taskFoo() if creating one task from within another.', E_USER_DEPRECATED);
trigger_error(
// phpcs:ignore
'No logger set for ' . get_class($this) . '. Use $this->task(Foo::class) rather than new Foo() in Tasks to ensure the builder can initialize task the task, or use $this->collectionBuilder()->taskFoo() if creating one task from within another.',
E_USER_DEPRECATED
);
$gaveDeprecationWarning = true;
}
return Robo::logger();
Expand Down
Loading

0 comments on commit 932fed0

Please sign in to comment.