Skip to content

Commit

Permalink
Merge pull request #1 from awssat/analysis-zRWMV9
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
abdumu authored Mar 17, 2018
2 parents 971f372 + 7a1d233 commit 31ea320
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 61 deletions.
93 changes: 42 additions & 51 deletions src/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace Awssat\ArrayHelper;


/**
* @method \Awssat\ArrayHelper\ArrayHelper push(mixed $value1 [, mixed $... ])
* @method \Awssat\ArrayHelper\ArrayHelper push(mixed $value1 [, mixed $... ])
* @method \Awssat\ArrayHelper\ArrayHelper prepend(mixed $value1 [, mixed $... ])
* @method \Awssat\ArrayHelper\ArrayHelper values()
* @method \Awssat\ArrayHelper\ArrayHelper keys([ mixed $search_value [, bool $strict]])
Expand Down Expand Up @@ -86,32 +85,31 @@ class ArrayHelper implements \Countable, \ArrayAccess, \IteratorAggregate, \Seri
protected $isElse = [];
protected $insideIfCondition = false;
protected $dataHasChanged = false;

protected $aliasMethods = [
'exists' => 'in_array',
'exists' => 'in_array',
'prepend' => 'array_unshift',
];


/**
* Initiate the class.
*
* @param array $value an array
*/
public function __construct()
{
if(func_num_args() > 1) {
if (func_num_args() > 1) {
$this->data = func_get_args();
} else if(func_num_args() == 1) {
} elseif (func_num_args() == 1) {
$this->set(func_get_arg(0));
}

return $this;
}

static public function make()
public static function make()
{
return (new static(...func_get_args()));
return new static(...func_get_args());
}

/**
Expand Down Expand Up @@ -142,28 +140,27 @@ function () use ($methodName, $arguments) {

$snake_method_name = strtolower(preg_replace('/([a-z]{1})([A-Z]{1})/', '$1_$2', $methodName));

if(array_key_exists($snake_method_name, $this->aliasMethods)) {
if (array_key_exists($snake_method_name, $this->aliasMethods)) {
$snake_method_name = $this->aliasMethods[$snake_method_name];
}

if (in_array($snake_method_name, ['combine_keys', 'combine_values', 'equal', 'contains', 'empty'])) {
return $this->do(function() use($snake_method_name, $arguments) {

if($snake_method_name == 'combine_keys') {
if (in_array($snake_method_name, ['combine_keys', 'combine_values', 'equal', 'contains', 'empty'])) {
return $this->do(function () use ($snake_method_name, $arguments) {
if ($snake_method_name == 'combine_keys') {
return array_combine($arguments[0], $this->data);
}

if ($snake_method_name == 'combine_values') {
return array_combine($this->data, $arguments[0]);
}

if ($snake_method_name == 'equal') {
return $this->equal($this->data, $arguments[0]);
}

if ($snake_method_name == 'contains' && sizeof($arguments)) {
foreach($arguments as $argument) {
if(! in_array($argument, $this->data)) {
if ($snake_method_name == 'contains' && count($arguments)) {
foreach ($arguments as $argument) {
if (!in_array($argument, $this->data)) {
return false;
}
}
Expand All @@ -174,14 +171,12 @@ function () use ($methodName, $arguments) {
if ($snake_method_name == 'empty') {
return count($this->data) === 0;
}

});

} else if (function_exists('array_'.$snake_method_name)) {
});
} elseif (function_exists('array_'.$snake_method_name)) {
return $this->do('array_'.$snake_method_name, ...$arguments);
} else if (function_exists($snake_method_name)) {
return $this->do($snake_method_name, ...$arguments);
} else {
} elseif (function_exists($snake_method_name)) {
return $this->do($snake_method_name, ...$arguments);
} else {
// Couldn't find either?
throw new \BadMethodCallException('Method ('.$methodName.') does not exists!');
}
Expand All @@ -190,28 +185,29 @@ function () use ($methodName, $arguments) {
}

/**
* Undocumented function
* Undocumented function.
*
* @param int|null $index
* @param boolean $ignoreConditions
* @param bool $ignoreConditions
*
* @return array|self
*/
public function get($index = null, $ignoreConditions = false)
{
if(! is_null($index)) {
if (!is_null($index)) {
return $this->data[$index];
}

if ($this->skipIfTriggered() && ! $ignoreConditions) {
if ($this->skipIfTriggered() && !$ignoreConditions) {
return $this;
}

return $this->data;
}

/**
* get all items ignoring the current conditions
*
* get all items ignoring the current conditions.
*
* @return array
*/
public function all()
Expand All @@ -229,13 +225,13 @@ public function set($value)
if ($this->skipIfTriggered()) {
return $this;
}

$oldData = $this->data;

if (is_array($value)) {
$this->data = $value;
} elseif ($value instanceof self) {
$this->data = $value->get(null, true);
$this->data = $value->get(null, true);
} elseif ($value instanceof JsonSerializable) {
$this->data = $value->jsonSerialize();
} elseif ($value instanceof Traversable) {
Expand All @@ -251,7 +247,7 @@ public function set($value)
}
}

$this->dataHasChanged = ! $this->equal($oldData, $this->data);
$this->dataHasChanged = !$this->equal($oldData, $this->data);

unset($oldData);

Expand Down Expand Up @@ -328,7 +324,7 @@ public function do($callable, ...$args)

$exceptionIndices = [];

if (! array_key_exists($callable, $exceptionIndices)) {
if (!array_key_exists($callable, $exceptionIndices)) {
foreach ($functionInfo->getParameters() as $order => $arg) {
//no: value
// if ($callable === 'array_intersect') {
Expand Down Expand Up @@ -364,15 +360,14 @@ public function do($callable, ...$args)
!(isset($this->isIf[$this->conditionDepth]) && $this->isIf[$this->conditionDepth]) &&
!(isset($this->isElse[$this->conditionDepth]) && $this->isElse[$this->conditionDepth])
) {

if(in_array($callable, [
if (in_array($callable, [
'array_multisort', 'array_push', 'array_unshift', 'array_walk', 'array_walk_recursive',
'arsort', 'asort', 'krsort', 'ksort', 'rsort', 'sort', 'uasort', 'uksort', 'usort', 'natcasesort',
'natsort', 'shuffle',
])) {
return $this;
}

return $result;
}

Expand All @@ -382,14 +377,14 @@ public function do($callable, ...$args)

$oldData = $this->data;

if(! ($callable instanceof \Closure) && in_array($callable, ['array_fill_keys', 'array_fill']) && is_array($result)) {
if (!($callable instanceof \Closure) && in_array($callable, ['array_fill_keys', 'array_fill']) && is_array($result)) {
$this->data = $this->data + $result;
} else {
$this->data = $result;
}

$this->dataHasChanged = ! $this->equal($oldData, $this->data);
$this->dataHasChanged = !$this->equal($oldData, $this->data);

unset($oldData);

return $this;
Expand Down Expand Up @@ -418,7 +413,6 @@ public function if($callable, ...$args)

$result = $this->do($callable, ...$args);


if ($result instanceof self && $this->equal($lastData, $this->data)) {
$this->falseIfTriggered[$this->conditionDepth] = true;
} elseif (is_array($result) && $this->equal($lastData, $result)) {
Expand All @@ -434,15 +428,15 @@ public function if($callable, ...$args)
return $this;
}

protected function equal($array1, $array2)
protected function equal($array1, $array2)
{
return is_array($array1) && is_array($array2)
&& count($array1) == count($array2)
&& array_diff($array1, $array2) === array_diff($array2, $array1);
}

/**
* did last method changed the array value
* did last method changed the array value.
*
* @return bool
*/
Expand Down Expand Up @@ -489,7 +483,6 @@ public function else()
return $this;
}


/**
* Return the length of the current array.
*
Expand All @@ -516,7 +509,7 @@ public function __debugInfo()
}

/**
* unique hash of the array
* unique hash of the array.
*
* @return string
*/
Expand All @@ -530,7 +523,7 @@ public function offsetGet($offset)
return $this->__get($offset);
}

public function offsetSet($offset, $value)
public function offsetSet($offset, $value)
{
$this->__set($offset, $value);
}
Expand All @@ -547,16 +540,14 @@ public function offsetUnset($offset)

public function __get($index)
{
if (! is_null($index)) {
if (!is_null($index)) {
return $this->get($index);
}

return null;
}

public function __set($index, $value)
{
if($this->data[$index] !== $value) {
if ($this->data[$index] !== $value) {
$this->dataHasChanged = true;
}

Expand Down
7 changes: 3 additions & 4 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

use Awssat\ArrayHelper\ArrayHelper;


if (! function_exists('arr')) {
if (!function_exists('arr')) {
/**
* A flexible & powerful array manipulation helper
* A flexible & powerful array manipulation helper.
*
* @param string $value an array, or multiple parameters to the function
* @param string $value an array, or multiple parameters to the function
*
* @author abdumu <[email protected]>
*
Expand Down
10 changes: 4 additions & 6 deletions tests/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public function arr_convert_to_json_when_converted_to_string()
$this->assertEquals(json_encode($value), (string) arr($value));
}


/** @test */
public function does_arr_has_methods()
{
Expand Down Expand Up @@ -82,6 +81,7 @@ public function tap_does_not_change_value()
arr(['hi'])->tap(function ($value) {
$this->data = [];
$value = [];

return [];
})->get(0)
);
Expand All @@ -106,7 +106,7 @@ public function map_works_fine()
$this->assertEquals(
['a!', 'b!'],
arr(['a', 'b'])->map(function ($item) {
return $item . '!';
return $item.'!';
})
->get()
);
Expand All @@ -118,7 +118,7 @@ public function walk_works_fine_too()
$this->assertEquals(
['a!', 'b!'],
arr(['a', 'b'])->walk(function (&$item) {
$item = $item . '!';
$item = $item.'!';
})
->get()
);
Expand All @@ -139,7 +139,7 @@ public function filter_works_fine2()
{
$this->assertEquals(
['a', 'b'],
arr(['a', 'b', 'c'])->filter(function($item){
arr(['a', 'b', 'c'])->filter(function ($item) {
return $item !== 'c';
})
->get()
Expand Down Expand Up @@ -191,7 +191,6 @@ public function test_if_else_endif_with_built_in_functions()
->endif()
->map('trim')
->get();


$this->assertEquals(['HI', 'WELCOME'], $result);
}
Expand Down Expand Up @@ -235,7 +234,6 @@ public function test_built_in_functions()
}
}


/** @test */
public function ar_throw_exception_if_given_wrong_method()
{
Expand Down

0 comments on commit 31ea320

Please sign in to comment.