Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Lander Vanderstraeten committed Apr 19, 2019
1 parent c3e3ae6 commit 51bab84
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
24 changes: 24 additions & 0 deletions spec/Util/StrSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace spec\GrumPHP\Util;

use PhpSpec\ObjectBehavior;

class StrSpec extends ObjectBehavior
{
function it_should_find_a_part_of_a_string_by_one_of_the_provided_needles()
{
$this::containsOneOf('a;randomText-written by me', ['a', 'me'])->shouldBe(true);
$this::containsOneOf('a;randomText-written by me', ['Text'])->shouldBe(true);

$this::containsOneOf('a;randomText-written by me', ['this does not exist'])->shouldBe(false);
$this::containsOneOf('a;randomText-written by me', ['text'])->shouldBe(false);
}

function it_should_split_a_string_by_a_delimiter_and_result_in_a_unique_list()
{
$this::explodeWithCleanup(',', ' a random,list, of things ')->shouldBe([
'a random', 'list', 'of things'
]);
}
}
24 changes: 2 additions & 22 deletions src/Console/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use GrumPHP\Locator\RegisteredFiles;
use GrumPHP\Runner\TaskRunnerContext;
use GrumPHP\Task\Context\RunContext;
use GrumPHP\Util\Str;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -68,7 +69,7 @@ public function execute(InputInterface $input, OutputInterface $output)
$files = $this->getRegisteredFiles();
$testSuites = $this->grumPHP->getTestSuites();

$tasks = $this->parseCommaSeparatedOption($input->getOption("tasks") ?? "");
$tasks = Str::explodeWithCleanup(',', $input->getOption("tasks") ?? '');

$context = new TaskRunnerContext(
new RunContext($files),
Expand All @@ -93,25 +94,4 @@ protected function paths(): PathsHelper
{
return $this->getHelper(PathsHelper::HELPER_NAME);
}

/**
* Split $value on ",", trim the individual parts and
* de-deduplicate the remaining values
*
* @param string $value
* @return string[]
*/
protected function parseCommaSeparatedOption(string $value)
{
$stringValues = explode(",", $value);
$parsedValues = [];
foreach ($stringValues as $k => $v) {
$v = trim($v);
if (empty($v)) {
continue;
}
$parsedValues[] = $v;
}
return $parsedValues;
}
}
19 changes: 13 additions & 6 deletions src/Util/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

namespace GrumPHP\Util;

class Str
final class Str
{
/**
* String contains one of the provided needles
*
* @param string $haystack
* @param array $needles
* @return bool
*/
public static function containsOneOf($haystack, array $needles)
public static function containsOneOf(string $haystack, array $needles): bool
{
foreach ($needles as $needle) {
if ($needle !== '' && mb_strpos($haystack, $needle) !== false) {
Expand All @@ -21,4 +17,15 @@ public static function containsOneOf($haystack, array $needles)

return false;
}

/**
* Split $value on ",", trim the individual parts and
* de-deduplicate the remaining values
*/
public static function explodeWithCleanup(string $delimiter, string $value): array
{
return array_unique(array_map(function (string $value) {
return trim($value);
}, explode($delimiter, $value)));
}
}

0 comments on commit 51bab84

Please sign in to comment.