Skip to content

Commit

Permalink
New codestyle and PHP 8.1+
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Smet committed Mar 11, 2023
1 parent 79fe4bb commit 8b9d9c9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 42 deletions.
99 changes: 59 additions & 40 deletions src/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,21 @@
use JBZoo\Utils\Url;

use function JBZoo\Utils\int;
use function JBZoo\Utils\isStrEmpty;

/**
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
final class Path
{
// Minimal alias name length.
public const MIN_ALIAS_LENGTH = 2;

// Mod prepend rule add paths.
// Modifiers adding rules
public const MOD_PREPEND = 'prepend';
public const MOD_APPEND = 'append';
public const MOD_RESET = 'reset';

// Mod append rule add paths.
public const MOD_APPEND = 'append';

// Reset all registered paths.
public const MOD_RESET = 'reset';

/** Flag of result path (If true, is real path. If false, is relative path). */
/** Flag of result path (If true, is real path. If false, is relative path) */
private bool $isReal = true;

/** Holds paths list. */
Expand All @@ -51,7 +47,7 @@ final class Path

public function __construct(?string $root = null)
{
$root = $root ?: Sys::getDocRoot();
$root = isStrEmpty($root) ? Sys::getDocRoot() : $root;
$this->setRoot($root);
}

Expand All @@ -76,7 +72,8 @@ public function set(string $alias, array|string $paths, string $mode = self::MOD

if ($mode === self::MOD_RESET) { // Reset mode
$this->paths[$alias] = [];
$mode = self::MOD_PREPEND; // Add new paths in Prepend mode

$mode = self::MOD_PREPEND; // Add new paths in Prepend mode
}

foreach ($paths as $path) {
Expand All @@ -85,8 +82,8 @@ public function set(string $alias, array|string $paths, string $mode = self::MOD
}

$path = self::cleanPath($path);
if ($path && !\in_array($path, $this->paths[$alias], true)) {
if (\preg_match('/^' . \preg_quote($alias . ':', '') . '/i', $path)) {
if ($path !== '' && !\in_array($path, $this->paths[$alias], true)) {
if (\preg_match('/^' . \preg_quote($alias . ':', '') . '/i', $path) > 0) {
throw new Exception("Added looped path \"{$path}\" to key \"{$alias}\"");
}

Expand Down Expand Up @@ -136,7 +133,7 @@ public function getPaths(string $source): array
*/
public function getRoot(): ?string
{
if (!$this->root) {
if ($this->root === null) {
throw new Exception('Please, set the root directory');
}

Expand Down Expand Up @@ -202,8 +199,12 @@ public function remove(string $fromSource, array|string $paths): bool
*/
public function setRoot(?string $newRootPath): self
{
if (!$newRootPath || !\is_dir($newRootPath)) {
throw new Exception("Not found directory: {$newRootPath}");
if ($newRootPath === '' || $newRootPath === null) {
throw new Exception("New root path is empty: {$newRootPath}");
}

if (!\is_dir($newRootPath)) {
throw new Exception("Directory not found: {$newRootPath}");
}

$this->root = self::cleanPath($newRootPath);
Expand All @@ -218,18 +219,21 @@ public function setRoot(?string $newRootPath): self
public function url(string $source, bool $isFullUrl = true): ?string
{
$details = \explode('?', $source);
if ($path = $this->cleanPathInternal($details[0] ?? '')) {
$path = $this->getUrlPath($path, true);

if (!empty($path)) {
$path = $this->cleanPathInternal($details[0] ?? '');

if ($path !== '' && $path !== null) {
$urlPath = $this->getUrlPath($path, true);

if ($urlPath !== '' && $urlPath !== null) {
if (isset($details[1])) {
$path .= '?' . $details[1];
$urlPath .= '?' . $details[1];
}

$path = '/' . $path;
$root = Url::root();
$urlPath = '/' . $urlPath;
$root = Url::root();

return $isFullUrl ? "{$root}{$path}" : $path;
return $isFullUrl ? "{$root}{$urlPath}" : $urlPath;
}
}

Expand Down Expand Up @@ -295,7 +299,9 @@ public static function prefix(string $path): ?string
{
$path = self::cleanPath($path);

return \preg_match('|^(?P<prefix>([a-zA-Z]+:)?//?)|', $path, $matches) ? $matches['prefix'] : null;
return \preg_match('|^(?P<prefix>([a-zA-Z]+:)?//?)|', $path, $matches) > 0
? $matches['prefix']
: null;
}

/**
Expand All @@ -304,7 +310,9 @@ public static function prefix(string $path): ?string
*/
private function addNewPath(string $path, string $alias, string $mode): self
{
if ($cleanPath = $this->cleanPathInternal($path)) {
$cleanPath = $this->cleanPathInternal($path);

if ($cleanPath !== null && $cleanPath !== '') {
if ($mode === self::MOD_PREPEND) {
\array_unshift($this->paths[$alias], $cleanPath);
}
Expand All @@ -327,10 +335,10 @@ private function cleanPathInternal(string $path): ?string
return self::cleanPath($path);
}

if (self::hasCDBack($path)) {
if (self::hasCDBack($path) > 0) {
$realpath = self::cleanPath((string)\realpath($path));

return $realpath ?: null;
return $realpath !== '' ? $realpath : null;
}

return self::cleanPath($path);
Expand All @@ -342,21 +350,26 @@ private function cleanPathInternal(string $path): ?string
*/
private function getUrlPath(string $path, bool $exitsFile = false): ?string
{
if (!$this->root) {
throw new Exception('Please, set the root directory');
if ($this->root === null || $this->root === '') {
throw new Exception('Please, setup the root directory');
}

/** @noinspection CallableParameterUseCaseInTypeContextInspection */
if ($path = $this->cleanPathInternal($path)) {
$path = $this->cleanPathInternal($path);
if ($path !== null && $path !== '') {
if ($this->isVirtual($path)) {
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
$path = $this->get($path);
}

$subject = $path;
$pattern = '/^' . \preg_quote($this->root, '/') . '/i';

if ($path && $exitsFile && !$this->isVirtual($path) && !\file_exists($path)) {
if (
$path !== null
&& $path !== ''
&& $exitsFile
&& !$this->isVirtual($path)
&& !\file_exists($path)
) {
$subject = null;
}

Expand All @@ -372,7 +385,10 @@ private function getUrlPath(string $path, bool $exitsFile = false): ?string
*/
private function parse(string $source): array
{
[$alias, $path] = \explode(':', $source, 2);
$sourceParts = \explode(':', $source, 2);

$alias = $sourceParts[0] ?? '';
$path = $sourceParts[1] ?? '';

$path = \ltrim($path, '\\/');
$paths = $this->resolvePaths($alias);
Expand All @@ -393,7 +409,8 @@ private function resolvePaths(string $alias): array
$result = [];

foreach ($paths as $originalPath) {
if ($this->isVirtual($originalPath) && $realPath = $this->get($originalPath)) {
$realPath = $this->get($originalPath);
if ($realPath !== null && $realPath !== '' && $this->isVirtual($originalPath)) {
$path = $realPath;
} else {
$path = $this->cleanPathInternal($originalPath);
Expand All @@ -411,7 +428,11 @@ private function resolvePaths(string $alias): array
*/
private function getCurrentPath(string $path): ?string
{
return (string)($this->isReal ? \realpath($path) : $path) ?: null;
$realpath = \realpath($path);
$realpath = $realpath !== false ? $realpath : null;
$currentPath = (string)($this->isReal ? $realpath : $path);

return $currentPath !== '' ? $currentPath : null;
}

/**
Expand All @@ -435,9 +456,8 @@ private static function find(array|string $paths, string $file): ?string

/**
* Find actual file or directory in the paths.
* @param array|string $paths
*/
private static function findViaGlob($paths, string $file): array
private static function findViaGlob(array|string $paths, string $file): array
{
$paths = (array)$paths;
$file = \ltrim($file, '\\/');
Expand All @@ -447,9 +467,8 @@ private static function findViaGlob($paths, string $file): array
$fullPath = self::clean($path . '/' . $file);

$paths = \glob($fullPath, \GLOB_BRACE);
$paths = \array_filter((array)$paths);

return $paths ?: [];
return \array_filter((array)$paths);
}

/**
Expand Down
2 changes: 0 additions & 2 deletions tests/phpbench/CompareWithRealpath.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
*/
class CompareWithRealpath
{
/** @var string */
private string $root;

/** @var Filesystem */
private Filesystem $fs;

public function init(): void
Expand Down

0 comments on commit 8b9d9c9

Please sign in to comment.