Skip to content

Commit

Permalink
Cleaning code
Browse files Browse the repository at this point in the history
  • Loading branch information
jubianchi committed Jul 10, 2017
1 parent 726c930 commit 6e3c205
Show file tree
Hide file tree
Showing 19 changed files with 144 additions and 240 deletions.
8 changes: 4 additions & 4 deletions classes/asserter.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ protected function getTypeOf($mixed)
return $this->analyzer->getTypeOf($mixed);
}

protected function _($string)
protected function _(...$arguments)
{
return call_user_func_array([$this->locale, '_'], func_get_args());
return call_user_func_array([$this->locale, '_'], $arguments);
}

protected function __($singular, $plural, $quantity)
protected function __(...$arguments)
{
return call_user_func_array([$this->locale, '__'], func_get_args());
return call_user_func_array([$this->locale, '__'], $arguments);
}
}
3 changes: 1 addition & 2 deletions classes/asserter/generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
use mageekguy\atoum;
use mageekguy\atoum\asserter;
use mageekguy\atoum\exceptions;
use mageekguy\atoum\test\assertion;

class generator
{
protected $locale = null;
protected $resolver = null;

public function __construct(atoum\locale $locale = null, asserter\resolver $resolver = null, assertion\aliaser $aliaser = null)
public function __construct(atoum\locale $locale = null, asserter\resolver $resolver = null)
{
$this
->setLocale($locale)
Expand Down
61 changes: 22 additions & 39 deletions classes/asserters/error.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,56 +79,20 @@ public function exists()
$this->score->deleteError($key);
$this->pass();
} else {
switch (true) {
case $this->type === null && $this->message === null:
$failReason = $this->_('error does not exist');
break;

case $this->type === null && $this->message !== null:
$failReason = $this->_('error with message \'%s\' does not exist', $this->message);
break;

case $this->type !== null && $this->message === null:
$failReason = $this->_('error of type %s does not exist', self::getAsString($this->type));
break;

default:
$failReason = $this->_('error of type %s with message \'%s\' does not exist', self::getAsString($this->type), $this->message);
}

$this->fail($failReason);
$this->fail($this->getFailMessage(true));
}

return $this;
}

public function notExists()
{
$score = $this->getScore();

$key = $score->errorExists($this->message, $this->type, $this->messageIsPattern);
$key = $this->getScore()->errorExists($this->message, $this->type, $this->messageIsPattern);

if ($key === null) {
$this->pass();
} else {
switch (true) {
case $this->type === null && $this->message === null:
$failReason = $this->_('error exists');
break;

case $this->type === null && $this->message !== null:
$failReason = $this->_('error with message \'%s\' exists', $this->message);
break;

case $this->type !== null && $this->message === null:
$failReason = $this->_('error of type %s exists', self::getAsString($this->type));
break;

default:
$failReason = $this->_('error of type %s with message \'%s\' exists', self::getAsString($this->type), $this->message);
}

$this->fail($failReason);
$this->fail($this->getFailMessage());
}

return $this;
Expand Down Expand Up @@ -232,4 +196,23 @@ public static function getAsString($errorType)
return 'UNKNOWN';
}
}

private function getFailMessage($negative = false)
{
$verb = $negative ? 'does not exist' : 'exists';

switch (true) {
case $this->type === null && $this->message === null:
return $this->_('error %s', $verb);

case $this->type === null && $this->message !== null:
return $this->_('error with message \'%s\' %s', $this->message, $verb);

case $this->type !== null && $this->message === null:
return $this->_('error of type %s %s', self::getAsString($this->type), $verb);

default:
return $this->_('error of type %s with message \'%s\' %s', self::getAsString($this->type), $this->message, $verb);
}
}
}
46 changes: 26 additions & 20 deletions classes/asserters/phpArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,17 +410,9 @@ protected function notContainsValue($value, $failMessage, $strict)

protected function intersect(array $values, $failMessage, $strict)
{
$this->valueIsSet();

$unknownValues = [];

foreach ($values as $value) {
if (in_array($value, $this->value, $strict) === false) {
$unknownValues[] = $value;
}
}
$unknownValues = $this->valueIsSet()->getDifference($values, $strict);

if (count($unknownValues) <= 0) {
if (count($unknownValues) === 0) {
$this->pass();
} else {
if ($failMessage === null) {
Expand All @@ -439,17 +431,9 @@ protected function intersect(array $values, $failMessage, $strict)

protected function notIntersect(array $values, $failMessage, $strict)
{
$this->valueIsSet();

$knownValues = [];

foreach ($values as $value) {
if (in_array($value, $this->value, $strict) === true) {
$knownValues[] = $value;
}
}
$knownValues = $this->valueIsSet()->getIntersection($values, $strict);

if (count($knownValues) <= 0) {
if (count($knownValues) === 0) {
$this->pass();
} else {
if ($failMessage === null) {
Expand All @@ -466,6 +450,28 @@ protected function notIntersect(array $values, $failMessage, $strict)
return $this;
}

protected function getIntersection(array $values, $strict)
{
return $this->getValues($values, true, $strict);
}

protected function getDifference(array $values, $strict)
{
return $this->getValues($values, false, $strict);
}

protected function getValues(array $values, $equal, $strict)
{
return array_values(
array_filter(
$values,
function($value) use ($strict, $equal) {
return in_array($value, $this->value, $strict) === $equal;
}
)
);
}

protected function valueIsSet($message = 'Array is undefined')
{
return parent::valueIsSet($message);
Expand Down
2 changes: 1 addition & 1 deletion classes/autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ protected function getClassAlias($class)

protected function handleNamespaceOfClass($class)
{
foreach ($this->directories as $namespace => $directories) {
foreach (array_keys($this->directories) as $namespace) {
if (strpos($class, $namespace) === 0) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion classes/configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(scripts\runner $script)
$this->script = $script;

foreach ($this->script->getHelp() as $help) {
list($arguments, $values) = $help;
list($arguments) = $help;

foreach ($arguments as $argument) {
$this->methods[strtolower(str_replace('-', '', $argument))] = $argument;
Expand Down
4 changes: 2 additions & 2 deletions classes/mock/generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function getMockedClassCode($class, $mockNamespace = null, $mockClass = n
}

if ($this->adapter->class_exists($class, true) === false && $this->adapter->interface_exists($class, true) === false) {
$code = self::generateUnknownClassCode($class, $mockNamespace, $mockClass, $this->eachInstanceIsUnique);
$code = self::generateUnknownClassCode($mockNamespace, $mockClass, $this->eachInstanceIsUnique);
} else {
$reflectionClass = call_user_func($this->reflectionClassFactory, $class);

Expand Down Expand Up @@ -884,7 +884,7 @@ protected static function generateGetMockedMethod(array $mockedMethodNames)
;
}

protected static function generateUnknownClassCode($class, $mockNamespace, $mockClass, $uniqueId = false, $useStrictTypes = false)
protected static function generateUnknownClassCode($mockNamespace, $mockClass, $uniqueId = false, $useStrictTypes = false)
{
return ($useStrictTypes ? 'declare(strict_types=1);' . PHP_EOL : '') .
'namespace ' . ltrim($mockNamespace, '\\') . ' {' . PHP_EOL .
Expand Down
35 changes: 1 addition & 34 deletions classes/php/tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function tokenize($string)
{
$this->currentIterator = $this->iterator;

foreach ($this->tokens = new \arrayIterator(token_get_all($string)) as $key => $token) {
foreach ($this->tokens = new \arrayIterator(token_get_all($string)) as $token) {
switch ($token[0]) {
case T_CONST:
$token = $this->appendConstant();
Expand Down Expand Up @@ -228,39 +228,6 @@ private function appendCommentAndWhitespace()
}
}

private function appendArray()
{
$this->appendCommentAndWhitespace();

$this->tokens->next();

if ($this->tokens->valid() === true) {
$token = $this->tokens->current();

if ($token[0] === '(') {
$this->currentIterator->append(new token($token[0], isset($token[1]) === false ? null : $token[1], isset($token[2]) === false ? null : $token[2]));

$stack = 1;

while ($stack > 0 && $this->tokens->valid() === true) {
$this->tokens->next();

if ($this->tokens->valid() === true) {
$token = $this->tokens->current();

if ($token[0] === '(') {
$stack++;
} elseif ($token[0] === ')') {
$stack--;
}

$this->currentIterator->append(new token($token[0], isset($token[1]) === false ? null : $token[1], isset($token[2]) === false ? null : $token[2]));
}
}
}
}
}

private function nextTokenIs($tokenName, array $skipedTags = [T_WHITESPACE, T_COMMENT, T_INLINE_HTML])
{
$key = $this->tokens->key() + 1;
Expand Down
2 changes: 1 addition & 1 deletion classes/report/fields/runner/coverage/html.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __toString()

ksort($classes, \SORT_STRING);

foreach ($classes as $className => $classFile) {
foreach (array_keys($classes) as $className) {
$classCoverageTemplates->className = $className;
$classCoverageTemplates->classUrl = str_replace('\\', '/', $className) . self::htmlExtensionFile;

Expand Down
2 changes: 1 addition & 1 deletion classes/report/fields/runner/coverage/treemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __toString()
'children' => []
];

foreach ($this->coverage->getClasses() as $className => $classPath) {
foreach (array_keys($this->coverage->getClasses()) as $className) {
$node = & $nodes;

$class = new \reflectionClass($className);
Expand Down
Loading

0 comments on commit 6e3c205

Please sign in to comment.