Skip to content

Commit f431e3e

Browse files
committed
update helper function names to avoid collisons with Laravel helpers
1 parent 90de89c commit f431e3e

File tree

10 files changed

+33
-28
lines changed

10 files changed

+33
-28
lines changed

src/Results/Nodes/ParameterNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(Node\Param $node)
2626
if ($node->type instanceof Node\NullableType) {
2727
$this->type = $node->getType();
2828
} else {
29-
$this->type = optional($node->type)->toString();
29+
$this->type = opt($node->type)->toString();
3030
}
3131

3232
$this->default = $node->default ? ExpressionTransformer::parserNodeToResultNode($node->default) : null;

src/Results/Nodes/Traits/BootsTraits.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ protected function bootTraits($node): void
1010
{
1111
$reflectionObject = new \ReflectionObject($this);
1212

13-
collect($reflectionObject->getTraitNames())
13+
collection($reflectionObject->getTraitNames())
1414
->map(function (string $name) {
1515
if (strpos($name, __NAMESPACE__) === false) {
1616
return null;

src/Searcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ protected function findReferences(array $ast, string $class, array $names): arra
180180
{
181181
$nodes = (new NodeFinder())->findInstanceOf($ast, $class);
182182

183-
return collect($nodes)->filter(function (Node $node) use ($names) {
183+
return collection($nodes)->filter(function (Node $node) use ($names) {
184184
$name = NameResolver::resolve($node) ?? false;
185185

186186
return $name && Arr::matchesAny($name, $names, true);

src/Support/Arr.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static function matchesAny($strings, array $values, bool $allowRegex = tr
4646
$strings = [$strings];
4747
}
4848

49-
return collect($strings)->map(function ($str) use ($values, $allowRegex) {
49+
return collection($strings)->map(function ($str) use ($values, $allowRegex) {
5050
if (is_array($str)) {
5151
return self::matchesAny($str, $values, $allowRegex);
5252
}
@@ -123,7 +123,7 @@ public static function first($array, callable $callback = null, $default = null)
123123
{
124124
if (is_null($callback)) {
125125
if (empty($array)) {
126-
return value($default);
126+
return val($default);
127127
}
128128

129129
foreach ($array as $item) {
@@ -137,7 +137,7 @@ public static function first($array, callable $callback = null, $default = null)
137137
}
138138
}
139139

140-
return value($default);
140+
return val($default);
141141
}
142142

143143
/**
@@ -151,7 +151,7 @@ public static function first($array, callable $callback = null, $default = null)
151151
public static function last(array $array, callable $callback = null, $default = null)
152152
{
153153
if (is_null($callback)) {
154-
return empty($array) ? value($default) : end($array);
154+
return empty($array) ? val($default) : end($array);
155155
}
156156

157157
return static::first(array_reverse($array, true), $callback, $default);
@@ -172,15 +172,15 @@ public static function pluck($array, $value, $key = null): array
172172
[$value, $key] = static::explodePluckParameters($value, $key);
173173

174174
foreach ($array as $item) {
175-
$itemValue = data_get($item, $value);
175+
$itemValue = get_data($item, $value);
176176

177177
// If the key is "null", we will just append the value to the array and keep
178178
// looping. Otherwise we will key the array using the value of the key we
179179
// received from the developer. Then we'll return the final array form.
180180
if (is_null($key)) {
181181
$results[] = $itemValue;
182182
} else {
183-
$itemKey = data_get($item, $key);
183+
$itemKey = get_data($item, $key);
184184

185185
if (is_object($itemKey) && method_exists($itemKey, '__toString')) {
186186
$itemKey = (string) $itemKey;

src/Support/Collections/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public function whereIn(string $key, $values, bool $strict = false): self
178178
$values = $this->getArrayableItems($values);
179179

180180
return $this->filter(function ($item) use ($key, $values, $strict) {
181-
return in_array(data_get($item, $key), $values, $strict);
181+
return in_array(get_data($item, $key), $values, $strict);
182182
});
183183
}
184184

@@ -210,7 +210,7 @@ protected function operatorForWhere(string $key, ?string $operator = null, $valu
210210
}
211211

212212
return function ($item) use ($key, $operator, $value) {
213-
$retrieved = data_get($item, $key);
213+
$retrieved = get_data($item, $key);
214214

215215
$strings = array_filter([$retrieved, $value], function ($value) {
216216
return is_string($value) || (is_object($value) && method_exists($value, '__toString'));

src/Support/ExpressionTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ExpressionTransformer
3535
{
3636
public static function parserNodesToResultNodes(array $nodes): array
3737
{
38-
return collect($nodes)->map(function ($node) {
38+
return collection($nodes)->map(function ($node) {
3939
return static::parserNodeToResultNode($node);
4040
})->all();
4141
}

src/Support/NameResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function resolve($node)
6868

6969
public static function resolveAll(array $nodes): array
7070
{
71-
return collect($nodes)->each(function ($node) {
71+
return collection($nodes)->each(function ($node) {
7272
return self::resolve($node);
7373
})->filter()->all();
7474
}

src/Support/StatementTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function parserNodeToResultNode(Node $node)
3030

3131
public static function parserNodesToResultNode(array $nodes): array
3232
{
33-
return collect($nodes)->map(function ($node) {
33+
return collection($nodes)->map(function ($node) {
3434
return self::parserNodeToResultNode($node);
3535
})->toArray();
3636
}

src/Support/helpers.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
use Permafrost\PhpCodeSearch\Support\Arr;
44
use Permafrost\PhpCodeSearch\Support\Collections\Collection;
55

6-
if (! function_exists('optional')) {
7-
function optional($value)
6+
if (! function_exists('opt')) {
7+
/**
8+
* "optional" helper function
9+
* @param $value
10+
* @return mixed|__anonymous@237
11+
*/
12+
function opt($value)
813
{
914
if (! $value) {
1015
return new class {
@@ -20,12 +25,12 @@ public function __call($name, $arguments)
2025
};
2126
}
2227

23-
return value($value);
28+
return val($value);
2429
}
2530
}
2631

2732
/** @codeCoverageIgnore */
28-
if (! function_exists('data_get')) {
33+
if (! function_exists('get_data')) {
2934
/**
3035
* Get an item from an array or object using "dot" notation.
3136
*
@@ -34,7 +39,7 @@ public function __call($name, $arguments)
3439
* @param mixed $default
3540
* @return mixed
3641
*/
37-
function data_get($target, $key, $default = null)
42+
function get_data($target, $key, $default = null)
3843
{
3944
if (is_null($key)) {
4045
return $target;
@@ -53,13 +58,13 @@ function data_get($target, $key, $default = null)
5358
if ($target instanceof Collection) {
5459
$target = $target->all();
5560
} elseif (! is_iterable($target)) {
56-
return value($default);
61+
return val($default);
5762
}
5863

5964
$result = [];
6065

6166
foreach ($target as $item) {
62-
$result[] = data_get($item, $key);
67+
$result[] = get_data($item, $key);
6368
}
6469

6570
return in_array('*', $key) ? Arr::collapse($result) : $result;
@@ -70,7 +75,7 @@ function data_get($target, $key, $default = null)
7075
} elseif (is_object($target) && isset($target->{$segment})) {
7176
$target = $target->{$segment};
7277
} else {
73-
return value($default);
78+
return val($default);
7479
}
7580
}
7681

@@ -79,21 +84,21 @@ function data_get($target, $key, $default = null)
7984
}
8085

8186
/** @codeCoverageIgnore */
82-
if (! function_exists('value')) {
87+
if (! function_exists('val')) {
8388
/**
8489
* Return the default value of the given value.
8590
*
8691
* @param mixed $value
8792
* @return mixed
8893
*/
89-
function value($value, ...$args)
94+
function val($value, ...$args)
9095
{
9196
return $value instanceof Closure ? $value(...$args) : $value;
9297
}
9398
}
9499

95-
if (! function_exists('collect')) {
96-
function collect($items)
100+
if (! function_exists('collection')) {
101+
function collection($items)
97102
{
98103
return new Collection($items);
99104
}

tests/Support/HelpersTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class HelpersTest extends TestCase
99
/** @test */
1010
public function it_optionally_allows_chaining_method_calls_when_passed_a_null_argument()
1111
{
12-
$this->assertNull(optional(null)->test());
12+
$this->assertNull(opt(null)->test());
1313
}
1414

1515
/** @test */
@@ -22,6 +22,6 @@ public function test()
2222
}
2323
};
2424

25-
$this->assertEquals(123, optional($class)->test());
25+
$this->assertEquals(123, opt($class)->test());
2626
}
2727
}

0 commit comments

Comments
 (0)