Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: nette/utils
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v4.0.1
Choose a base ref
...
head repository: nette/utils
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v4.0.2
Choose a head ref
  • 9 commits
  • 12 files changed
  • 4 contributors

Commits on Aug 22, 2023

  1. Copy the full SHA
    8d225e3 View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    f48f657 View commit details
  3. composer: updated suggestions

    dg committed Aug 22, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    4ce2098 View commit details

Commits on Sep 19, 2023

  1. typo

    dg committed Sep 19, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    23d2aa9 View commit details
  2. StaticClass: constructor is private [Closes nette/di#292]

    - ReflectionClass::isInstance() returns false
    - it is marked as an error in the IDE
    dg committed Sep 19, 2023
    Copy the full SHA
    dd7502e View commit details
  3. Copy the full SHA
    380966d View commit details
  4. Finder: getType() replaced with isDir() / isFile()

    - getType() throws an exception when the file (no longer) exists
    - there is a difference regarding handling of symlinks [#295]
    dg committed Sep 19, 2023
    Copy the full SHA
    58572a8 View commit details
  5. tests: added symlink Finder tests (#295)

    nufue authored and dg committed Sep 19, 2023
    Copy the full SHA
    0d93403 View commit details
  6. DateTime: build from timestamp use method setTimestamp(), keep defaul…

    …t timezone (#300)
    h4kuna authored and dg committed Sep 19, 2023
    Copy the full SHA
    cead663 View commit details
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -32,7 +32,6 @@
"ext-json": "to use Nette\\Utils\\Json",
"ext-intl": "to use Strings::webalize(), toAscii(), normalize() and compare()",
"ext-mbstring": "to use Strings::lower() etc...",
"ext-xml": "to use Strings::length() etc. when mbstring is not available",
"ext-gd": "to use Image",
"ext-tokenizer": "to use Nette\\Utils\\Reflection::getUseStatements()"
},
6 changes: 2 additions & 4 deletions src/StaticClass.php
Original file line number Diff line number Diff line change
@@ -16,12 +16,10 @@
trait StaticClass
{
/**
* @return never
* @throws \Error
* Class is static and cannot be instantiated.
*/
final public function __construct()
final private function __construct()
{
throw new \Error('Class ' . static::class . ' is static and cannot be instantiated.');
}


14 changes: 14 additions & 0 deletions src/Utils/Arrays.php
Original file line number Diff line number Diff line change
@@ -332,6 +332,10 @@ 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 callback with signature `function ($value, $key, array $array): bool`.
* @template K
* @template V
* @param iterable<K, V> $array
* @param callable(V, K, ($array is array ? array<K, V> : iterable<K, V>)): bool $callback
*/
public static function some(iterable $array, callable $callback): bool
{
@@ -348,6 +352,10 @@ public static function some(iterable $array, callable $callback): 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
* @template V
* @param iterable<K, V> $array
* @param callable(V, K, ($array is array ? array<K, V> : iterable<K, V>)): bool $callback
*/
public static function every(iterable $array, callable $callback): bool
{
@@ -364,6 +372,12 @@ public static function every(iterable $array, callable $callback): bool
/**
* Calls $callback on all elements in the array and returns the array of return values.
* The callback has the signature `function ($value, $key, array $array): bool`.
* @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 $callback
* @return array<K, R>
*/
public static function map(iterable $array, callable $callback): array
{
4 changes: 2 additions & 2 deletions src/Utils/DateTime.php
Original file line number Diff line number Diff line change
@@ -52,7 +52,7 @@ public static function from(string|int|\DateTimeInterface|null $time): static
$time += time();
}

return (new static('@' . $time))->setTimezone(new \DateTimeZone(date_default_timezone_get()));
return (new static)->setTimestamp((int) $time);

} else { // textual or null
return new static((string) $time);
@@ -130,7 +130,7 @@ public function __toString(): string


/**
* Creates a copy with a modified time.
* You'd better use: (clone $dt)->modify(...)
*/
public function modifyClone(string $modify = ''): static
{
2 changes: 0 additions & 2 deletions src/Utils/FileSystem.php
Original file line number Diff line number Diff line change
@@ -17,8 +17,6 @@
*/
final class FileSystem
{
use Nette\StaticClass;

/**
* Creates a directory if it does not exist, including parent directories.
* @throws Nette\IOException on error occurred
6 changes: 3 additions & 3 deletions src/Utils/Finder.php
Original file line number Diff line number Diff line change
@@ -337,7 +337,7 @@ public function getIterator(): \Generator


/**
* @param array<\stdClass{pattern: string, mode: string, recursive: bool}> $searches
* @param array<object{pattern: string, mode: string, recursive: bool}> $searches
* @param string[] $subdirs
* @return \Generator<string, FileInfo>
*/
@@ -385,7 +385,7 @@ private function traverseDir(string $dir, array $searches, array $subdirs = []):
$relativePathname = FileSystem::unixSlashes($file->getRelativePathname());
foreach ($searches as $search) {
if (
$file->getType() === $search->mode
$file->{'is' . $search->mode}()
&& preg_match($search->pattern, $relativePathname)
&& $this->proveFilters($this->filters, $file, $cache)
) {
@@ -427,7 +427,7 @@ private function proveFilters(array $filters, FileInfo $file, array &$cache): bo
}


/** @return array<string, array<\stdClass{pattern: string, mode: string, recursive: bool}>> */
/** @return array<string, array<object{pattern: string, mode: string, recursive: bool}>> */
private function buildPlan(): array
{
$plan = $dirCache = [];
8 changes: 5 additions & 3 deletions src/Utils/Strings.php
Original file line number Diff line number Diff line change
@@ -397,9 +397,11 @@ public static function findPrefix(array $strings): string
*/
public static function length(string $s): int
{
return function_exists('mb_strlen')
? mb_strlen($s, 'UTF-8')
: strlen(utf8_decode($s));
return match (true) {
extension_loaded('mbstring') => mb_strlen($s, 'UTF-8'),
extension_loaded('iconv') => iconv_strlen($s, 'UTF-8'),
default => strlen(@utf8_decode($s)), // deprecated
};
}


26 changes: 25 additions & 1 deletion tests/Utils/Finder.basic.phpt
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ function export($iterator, bool $sort = true)
}


test('expty search', function () {
test('empty search', function () {
$finder = (new Finder)->in('fixtures.finder');
Assert::same([], export($finder));

@@ -172,3 +172,27 @@ test('absolute path in mask', function () { // will not work if there are charac
FileSystem::unixSlashes(__DIR__),
], export($finder));
});


test('symlink to file', function () {
$finder = Finder::find('subdir/*.txt')->in('fixtures.finder3');
Assert::same([
'fixtures.finder3/subdir/file.txt',
], export($finder));
});


test('symlink to directory', function () {
$finder = Finder::findDirectories()->in('fixtures.finder3/another_subdir');
Assert::same([
'fixtures.finder3/another_subdir/subdir',
], export($finder));
});


test('symlink to file in symlinked directory', function () {
$finder = Finder::find('subdir/*.txt')->in('fixtures.finder3/another_subdir');
Assert::same([
'fixtures.finder3/another_subdir/subdir/file.txt',
], export($finder));
});
2 changes: 1 addition & 1 deletion tests/Utils/StaticClass.phpt
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ class TestClass
Assert::exception(
fn() => new TestClass,
Error::class,
'Class TestClass is static and cannot be instantiated.',
'Call to private TestClass::__construct() from global scope',
);

Assert::exception(
1 change: 1 addition & 0 deletions tests/Utils/fixtures.finder3/another_subdir/subdir
1 change: 1 addition & 0 deletions tests/Utils/fixtures.finder3/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
File for testing purposes
1 change: 1 addition & 0 deletions tests/Utils/fixtures.finder3/subdir/file.txt