Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileSystem: add resolvePath #308

Open
wants to merge 3 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
3 changes: 1 addition & 2 deletions src/Iterators/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
namespace Nette\Iterators;



/**
* Applies the callback to the elements of the inner iterator.
* @deprecated use Nette\Utils\Iterables::map()
*/
class Mapper extends \IteratorIterator
{
Expand Down
22 changes: 11 additions & 11 deletions src/Utils/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,11 @@ public static function pick(array &$array, string|int $key, mixed $default = nul

/**
* Tests whether at least one element in the array passes the test implemented by the provided function,
* which has the signature `function ($value, $key, array $array): bool`.
* @template K
* which has the signature `function (mixed $value, int|string $key, array $array): bool`.
* @template K of array-key
* @template V
* @param iterable<K, V> $array
* @param callable(V, K, ($array is array ? array<K, V> : iterable<K, V>)): bool $predicate
* @param array<K, V> $array
* @param callable(V, K, array<K, V>): bool $predicate
*/
public static function some(iterable $array, callable $predicate): bool
{
Expand All @@ -389,11 +389,11 @@ public static function some(iterable $array, callable $predicate): bool

/**
* Tests whether all elements in the array pass the test implemented by the provided function,
* which has the signature `function ($value, $key, array $array): bool`.
* @template K
* which has the signature `function (mixed $value, int|string $key, array $array): bool`.
* @template K of array-key
* @template V
* @param iterable<K, V> $array
* @param callable(V, K, ($array is array ? array<K, V> : iterable<K, V>)): bool $predicate
* @param array<K, V> $array
* @param callable(V, K, array<K, V>): bool $predicate
*/
public static function every(iterable $array, callable $predicate): bool
{
Expand Down Expand Up @@ -430,12 +430,12 @@ public static function filter(array $array, callable $predicate): array

/**
* Returns an array containing the original keys and results of applying the given transform function to each element.
* The function has signature `function ($value, $key, array $array): mixed`.
* The function has signature `function (mixed $value, int|string $key, array $array): mixed`.
* @template K of array-key
* @template V
* @template R
* @param iterable<K, V> $array
* @param callable(V, K, ($array is array ? array<K, V> : iterable<K, V>)): R $transformer
* @param array<K, V> $array
* @param callable(V, K, array<K, V>): R $transformer
* @return array<K, R>
*/
public static function map(iterable $array, callable $transformer): array
Expand Down
4 changes: 3 additions & 1 deletion src/Utils/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@
}

if (is_string($callable) && str_contains($callable, '::')) {
return new ReflectionMethod($callable);
return PHP_VERSION_ID < 80300
? new ReflectionMethod($callable)
: ReflectionMethod::createFromMethodName($callable);

Check failure on line 99 in src/Utils/Callback.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined static method Nette\Utils\ReflectionMethod::createFromMethodName().
} elseif (is_array($callable)) {
return new ReflectionMethod($callable[0], $callable[1]);
} elseif (is_object($callable) && !$callable instanceof \Closure) {
Expand Down
12 changes: 12 additions & 0 deletions src/Utils/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,18 @@ public static function joinPaths(string ...$paths): string
}


public static function resolvePath(string ...$paths): string
{
for ($i = count($paths) - 1; $i >= 0; $i--) {
if (self::isAbsolute($paths[$i])) {
return self::joinPaths(...array_slice($paths, $i));
}
}

return self::joinPaths(getcwd(), ...$paths);
}


/**
* Converts backslashes to slashes.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static function getMethodDeclaringMethod(\ReflectionMethod $method): \Ref

$hash = [$method->getFileName(), $method->getStartLine(), $method->getEndLine()];
if (($alias = $decl->getTraitAliases()[$method->name] ?? null)
&& ($m = new \ReflectionMethod($alias))
&& ($m = PHP_VERSION_ID < 80300 ? new \ReflectionMethod($alias) : \ReflectionMethod::createFromMethodName($alias))
&& $hash === [$m->getFileName(), $m->getStartLine(), $m->getEndLine()]
) {
return self::getMethodDeclaringMethod($m);
Expand Down
21 changes: 21 additions & 0 deletions tests/Utils/FileSystem.resolvePath.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use Nette\Utils\FileSystem;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


test('', function () {
$S = DIRECTORY_SEPARATOR;
$cwd = getcwd();
Assert::same($cwd, FileSystem::resolvePath());
Assert::same("{$S}foo{$S}bar", FileSystem::resolvePath('/foo', 'bar'));
Assert::same("{$S}foo{$S}bar{$S}baz", FileSystem::resolvePath('/foo', 'bar', 'baz'));
Assert::same("{$S}foo{$S}baz", FileSystem::resolvePath('/foo', 'bar/..', 'baz'));
Assert::same("{$S}bar{$S}baz", FileSystem::resolvePath('foo', '/bar', 'baz'));
Assert::same("{$cwd}{$S}foo{$S}bar{$S}baz", FileSystem::resolvePath('foo', 'bar', 'baz'));
});
Loading