Skip to content
This repository was archived by the owner on Sep 20, 2021. It is now read-only.

case-insensitive for Operator name #93

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function offsetGet($id)
}

if (true === is_callable($value)) {
if (true === is_string($value) &&
if (true === is_string($value) &&
false === in_array(strtolower($value), get_defined_functions()['user'])) {
return $value;
}
Expand Down
4 changes: 1 addition & 3 deletions Model/Operator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@
* @copyright Copyright © 2007-2017 Hoa community
* @license New BSD License
*/
class Operator
extends Ruler\Model\Bag\Context
implements Visitor\Element
class Operator extends Ruler\Model\Bag\Context implements Visitor\Element
{
/**
* Lazy evaluation should break.
Expand Down
3 changes: 2 additions & 1 deletion Test/Unit/DynamicCallable.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class DynamicCallable extends Test\Unit\Suite
public function case_is_a_xcallable()
{
$this
->when($result = new SUT(function () {}))
->when($result = new SUT(function () {
}))
->then
->object($result)
->isInstanceOf(Consistency\Xcallable::class);
Expand Down
33 changes: 28 additions & 5 deletions Test/Unit/Visitor/Asserter.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ public function case_set_operator()
->given(
$asserter = new SUT(),
$oldOperators = $asserter->getOperators(),
$operator = function () {}
$operator = function () {
}
)
->when($result = $asserter->setOperator('_foo_', $operator))
->then
Expand All @@ -418,14 +419,34 @@ public function case_set_operator()
->isEqualTo(xcallable($operator));
}

public function case_set_operator_insensitive()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add a case with a non-ASCII symbol, like Λ and λ (resp the capital and small variant). Is it what we want? I guess yes.

{
$this
->given(
$asserter = new SUT(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, run hoa devtools:cs, and simply align =.

$operator = function () {
}
)
->when($result = $asserter->setOperator('_FΛO_', $operator))
->then
->boolean($asserter->operatorExists('_FΛO_'))
->isTrue()
->boolean($asserter->operatorExists('_fλo_'))
->isTrue()
->object($asserter->getOperator('_fλo_'))
->isEqualTo(xcallable($operator));
}

public function case_set_operator_overwrite()
{
$this
->given(
$asserter = new SUT(),
$asserter->setOperator('_foo_', function () {}),
$asserter->setOperator('_foo_', function () {
}),
$oldOperators = $asserter->getOperators(),
$operator = function () {}
$operator = function () {
}
)
->when($result = $asserter->setOperator('_foo_', $operator))
->then
Expand All @@ -444,7 +465,8 @@ public function case_operator_exists()
$this
->given(
$asserter = new SUT(),
$asserter->setOperator('_foo_', function () {})
$asserter->setOperator('_foo_', function () {
})
)
->when($result = $asserter->operatorExists('_foo_'))
->then
Expand All @@ -467,7 +489,8 @@ public function case_get_operator()
$this
->given(
$asserter = new SUT(),
$operator = function () {},
$operator = function () {
},
$asserter->setOperator('_foo_', $operator)
)
->when($result = $asserter->getOperator('_foo_'))
Expand Down
56 changes: 41 additions & 15 deletions Visitor/Asserter.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,43 @@ public function __construct(Ruler\Context $context = null)
$this->setContext($context);
}

$this->setOperator('and', function ($a = false, $b = false) { return $a && $b; });
$this->setOperator('or', function ($a = false, $b = false) { return $a || $b; });
$this->setOperator('xor', function ($a, $b) { return (bool) ($a ^ $b); });
$this->setOperator('not', function ($a) { return !$a; });
$this->setOperator('=', function ($a, $b) { return $a == $b; });
$this->setOperator('is', $this->getOperator('='));
$this->setOperator('!=', function ($a, $b) { return $a != $b; });
$this->setOperator('>', function ($a, $b) { return $a > $b; });
$this->setOperator('>=', function ($a, $b) { return $a >= $b; });
$this->setOperator('<', function ($a, $b) { return $a < $b; });
$this->setOperator('<=', function ($a, $b) { return $a <= $b; });
$this->setOperator('in', function ($a, array $b) { return in_array($a, $b); });
$this->setOperator('sum', function () { return array_sum(func_get_args()); });
$this->setOperator('and', function ($a = false, $b = false) {
return $a && $b;
});
$this->setOperator('or', function ($a = false, $b = false) {
return $a || $b;
});
$this->setOperator('xor', function ($a, $b) {
return (bool) ($a ^ $b);
});
$this->setOperator('not', function ($a) {
return !$a;
});
$this->setOperator('=', function ($a, $b) {
return $a == $b;
});
$this->setOperator('is', $this->getOperator('='));
$this->setOperator('!=', function ($a, $b) {
return $a != $b;
});
$this->setOperator('>', function ($a, $b) {
return $a > $b;
});
$this->setOperator('>=', function ($a, $b) {
return $a >= $b;
});
$this->setOperator('<', function ($a, $b) {
return $a < $b;
});
$this->setOperator('<=', function ($a, $b) {
return $a <= $b;
});
$this->setOperator('in', function ($a, array $b) {
return in_array($a, $b);
});
$this->setOperator('sum', function () {
return array_sum(func_get_args());
});
$this->setOperator('matches', function ($subject, $pattern) {
$escapedPattern = preg_replace('/(?<!\\\)`/', '\`', $pattern);

Expand Down Expand Up @@ -499,7 +523,7 @@ public function getContext()
*/
public function setOperator($operator, $callable)
{
$this->_operators[$operator] = $callable;
$this->_operators[mb_strtolower($operator)] = $callable;

return $this;
}
Expand All @@ -512,7 +536,7 @@ public function setOperator($operator, $callable)
*/
public function operatorExists($operator)
{
return true === array_key_exists($operator, $this->_operators);
return true === array_key_exists(mb_strtolower($operator), $this->_operators);
}

/**
Expand All @@ -527,6 +551,8 @@ public function getOperator($operator)
return null;
}

$operator = mb_strtolower($operator);

$handle = &$this->_operators[$operator];

if (!($handle instanceof Consistency\Xcallable)) {
Expand Down
4 changes: 2 additions & 2 deletions Visitor/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function visit(Visitor\Element $element, &$handle = null, $eldnah = null)

$out .= '(' . "\n";
} else {
$out .= 'func(' . "\n" . $_ . ' ';
$out .= 'func(' . "\n" . $_ . ' ';
$_handle[] = '\'' . $name . '\'';
}

Expand Down Expand Up @@ -173,7 +173,7 @@ protected function visitContext(Ruler\Model\Bag\Context $context, &$handle, $eld
++$this->_indentation;

$value = $dimension[Ruler\Model\Bag\Context::ACCESS_VALUE];
$out .= "\n" . $_ . ' ->';
$out .= "\n" . $_ . ' ->';

switch ($dimension[Ruler\Model\Bag\Context::ACCESS_TYPE]) {
case Ruler\Model\Bag\Context::ARRAY_ACCESS:
Expand Down