Skip to content

Commit

Permalink
optimize exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
C0DE8 committed May 13, 2017
1 parent 2f114a7 commit 226a9ed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
29 changes: 21 additions & 8 deletions src/C0DE8/MatchMaker/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,41 @@ public function matchAgainst($value, $pattern) : bool
// get the key matcher closure
$keyMatcher = (new KeyMatcher())->get($pattern);

/**
* @var array $value
* @var string $key
* @var mixed $item
*/
/** @var array $value **/
/** @var string $key **/
/** @var mixed $item **/
foreach ($value as $key => $item) {
if (!$keyMatcher($key, $item)) {
throw new KeyMatcherFailException(
'$keyMatch FAIL by ' . $key . ' => ' . \var_export($item, true)
'$keyMatcher FAIL by ' . $key . ' => '
. \var_export($item, true)
);
}
}

if (!$keyMatcher()) {
throw new KeyMatcherFailException('$keyMatch() call FAIL');
throw new KeyMatcherFailException(
'$keyMatcher() call FAIL (returned FALSE)'
);
}

} elseif (!(new Matcher)->match($value, $pattern)) {

$valueType = gettype($value);

if (is_array($value)) {
$value = json_encode($value);
}

if (is_object($value)) {
$value = get_class($value);
}

throw new MatcherException(
'Matcher FAIL by value [' . $value . '] ('.gettype($value).') => [expected type: ' . var_export($pattern, true).']'
'Matcher FAIL by value [' . $value . '] (' . $valueType . ') =>'
. ' [expected type: ' . var_export($pattern, true) . ']'
);

}

return true;
Expand Down
8 changes: 2 additions & 6 deletions src/C0DE8/MatchMaker/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ class Matcher
{

/**
* @param $value
* @param $pattern
* @param $value
* @param $pattern
* @return bool
* @throws \InvalidArgumentException
*/
public function match($value, $pattern)
{
// echo "\n".__CLASS__ . "\n";
// var_dump('$value = ' . var_export($value, true));
// var_dump('$pattern = ' . var_export($pattern, true));

$args = [];

if (($p = \ltrim($pattern, ':')) != $pattern) {
Expand Down
2 changes: 1 addition & 1 deletion src/C0DE8/MatchMaker/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function ($value) {
*/
'class_exists' =>
function ($value) {
return (\class_exists($value));
return \class_exists($value);
},
'instance' =>
function ($value, $class) {
Expand Down

0 comments on commit 226a9ed

Please sign in to comment.