Skip to content

Commit

Permalink
strict_types=1 (#4)
Browse files Browse the repository at this point in the history
* strict_types=1
* Update deps
  • Loading branch information
SmetDenis committed Apr 21, 2021
1 parent cbc19d6 commit c9ca73c
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 52 deletions.
10 changes: 10 additions & 0 deletions .gitattributes
Expand Up @@ -11,4 +11,14 @@
# @link https://github.com/JBZoo/Path
#

/.phan export-ignore
/build export-ignore
/tests export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/Makefile export-ignore

* text eol=lf
8 changes: 7 additions & 1 deletion .phan/config.php
Expand Up @@ -13,9 +13,11 @@
* @link https://github.com/JBZoo/Path
*/

declare(strict_types=1);

$default = include __DIR__ . '/../vendor/jbzoo/codestyle/src/phan/default.php';

return array_merge($default, [
$config = array_merge($default, [
'directory_list' => [
'src',

Expand All @@ -24,3 +26,7 @@
'vendor/jbzoo/utils'
]
]);

$config['plugins'][] = 'NotFullyQualifiedUsagePlugin';

return $config;
8 changes: 8 additions & 0 deletions .travis.yml
Expand Up @@ -19,6 +19,14 @@ php:
- 7.2
- 7.3
- 7.4
- 8.0
- nightly

jobs:
fast_finish: true
allow_failures:
- php: 8.0
- php: nightly

env:
matrix:
Expand Down
2 changes: 1 addition & 1 deletion README.md
@@ -1,6 +1,6 @@
# JBZoo / Path

[![Build Status](https://travis-ci.org/JBZoo/Path.svg)](https://travis-ci.org/JBZoo/Path) [![Coverage Status](https://coveralls.io/repos/JBZoo/Path/badge.svg)](https://coveralls.io/github/JBZoo/Path) [![Psalm Coverage](https://shepherd.dev/github/JBZoo/Path/coverage.svg)](https://shepherd.dev/github/JBZoo/Path)
[![Build Status](https://travis-ci.org/JBZoo/Path.svg)](https://travis-ci.org/JBZoo/Path) [![Coverage Status](https://coveralls.io/repos/JBZoo/Path/badge.svg)](https://coveralls.io/github/JBZoo/Path) [![Psalm Coverage](https://shepherd.dev/github/JBZoo/Path/coverage.svg)](https://shepherd.dev/github/JBZoo/Path) [![PHP Strict Types](https://img.shields.io/badge/strict__types-%3D1-brightgreen)](https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.strict)
[![Stable Version](https://poser.pugx.org/jbzoo/path/version)](https://packagist.org/packages/jbzoo/path) [![Latest Unstable Version](https://poser.pugx.org/jbzoo/path/v/unstable)](https://packagist.org/packages/jbzoo/path) [![Dependents](https://poser.pugx.org/jbzoo/path/dependents)](https://packagist.org/packages/jbzoo/path/dependents?order_by=downloads) [![GitHub Issues](https://img.shields.io/github/issues/jbzoo/path)](https://github.com/JBZoo/Path/issues) [![Total Downloads](https://poser.pugx.org/jbzoo/path/downloads)](https://packagist.org/packages/jbzoo/path/stats) [![GitHub License](https://img.shields.io/github/license/jbzoo/path)](https://github.com/JBZoo/Path/blob/master/LICENSE)


Expand Down
6 changes: 3 additions & 3 deletions composer.json
Expand Up @@ -19,12 +19,12 @@
"require" : {
"php" : ">=7.2",

"jbzoo/utils" : "^4.2.3",
"jbzoo/data" : "^4.1.3"
"jbzoo/utils" : "^4.3.0",
"jbzoo/data" : "^4.2.0"
},

"require-dev" : {
"jbzoo/toolbox-dev" : "^2.6.2"
"jbzoo/toolbox-dev" : "^2.10.0"
},

"autoload" : {
Expand Down
2 changes: 2 additions & 0 deletions src/Exception.php
Expand Up @@ -13,6 +13,8 @@
* @link https://github.com/JBZoo/Path
*/

declare(strict_types=1);

namespace JBZoo\Path;

/**
Expand Down
72 changes: 37 additions & 35 deletions src/Path.php
Expand Up @@ -13,6 +13,8 @@
* @link https://github.com/JBZoo/Path
*/

declare(strict_types=1);

namespace JBZoo\Path;

use JBZoo\Utils\Arr;
Expand Down Expand Up @@ -91,7 +93,7 @@ public function set(string $alias, $paths, string $mode = Path::MOD_PREPEND): se
$paths = (array)$paths;
$alias = self::cleanAlias($alias);

if (strlen($alias) < self::MIN_ALIAS_LENGTH) {
if (\strlen($alias) < self::MIN_ALIAS_LENGTH) {
throw new Exception('The minimum number of characters is ' . self::MIN_ALIAS_LENGTH);
}

Expand All @@ -110,8 +112,8 @@ public function set(string $alias, $paths, string $mode = Path::MOD_PREPEND): se
}

$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)) {
throw new Exception("Added looped path \"{$path}\" to key \"{$alias}\"");
}

Expand All @@ -134,21 +136,21 @@ public static function clean(string $path): string
$cleanedPath = self::cleanPath($path);

$prefix = (string)self::prefix($cleanedPath);
$cleanedPath = (string)substr($cleanedPath, (int)strlen($prefix));
$cleanedPath = (string)\substr($cleanedPath, (int)\strlen($prefix));

$parts = array_filter(explode('/', $cleanedPath), static function ($value) {
$parts = \array_filter(\explode('/', $cleanedPath), static function ($value) {
return ($value);
});

foreach ($parts as $part) {
if ('..' === $part) {
array_pop($tokens);
\array_pop($tokens);
} elseif ('.' !== $part) {
$tokens[] = $part;
}
}

return $prefix . implode('/', $tokens);
return $prefix . \implode('/', $tokens);
}

/**
Expand Down Expand Up @@ -193,11 +195,9 @@ public function getPaths(string $source): array

/**
* Get root directory.
*
* @return mixed
* @throws Exception
* @return string|null
*/
public function getRoot()
public function getRoot(): ?string
{
if (!$this->root) {
throw new Exception('Please, set the root directory');
Expand Down Expand Up @@ -226,15 +226,17 @@ public function setRealPathFlag(bool $isReal = true): self
*/
public function isVirtual(string $path): bool
{
$parts = explode(':', $path, 2);
$parts = \explode(':', $path, 2);

[$alias] = $parts;
$alias = self::cleanAlias($alias);
if (!array_key_exists($alias, $this->paths) && self::prefix($path) !== null) {
if (!\array_key_exists($alias, $this->paths) && self::prefix($path) !== null) {
return false;
}

return count($parts) === 2;
$validNumberOfParts = 2;

return \count($parts) === $validNumberOfParts;
}

/**
Expand All @@ -246,7 +248,7 @@ public function isVirtual(string $path): bool
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) ? $matches['prefix'] : null;
}

/**
Expand All @@ -268,7 +270,7 @@ public function remove(string $fromSource, $paths): bool
foreach ($paths as $origPath) {
$path = $this->cleanPathInternal(self::cleanPath($origPath));

$key = array_search($path, $this->paths[$alias], true);
$key = \array_search($path, $this->paths[$alias], true);
if (false !== $key) {
unset($this->paths[$alias][$key]);
$return = true;
Expand All @@ -288,7 +290,7 @@ public function remove(string $fromSource, $paths): bool
*/
public function setRoot(?string $newRootPath): self
{
if (!$newRootPath || !is_dir($newRootPath)) {
if (!$newRootPath || !\is_dir($newRootPath)) {
throw new Exception("Not found directory: {$newRootPath}");
}

Expand All @@ -307,7 +309,7 @@ public function setRoot(?string $newRootPath): self
*/
public function url(string $source, bool $isFullUrl = true): ?string
{
$details = explode('?', $source);
$details = \explode('?', $source);
if ($path = $this->cleanPathInternal((string)($details[0] ?? ''))) {
$path = $this->getUrlPath($path, true);

Expand Down Expand Up @@ -367,7 +369,7 @@ protected function addNewPath(string $path, string $alias, string $mode): self
{
if ($cleanPath = $this->cleanPathInternal($path)) {
if ($mode === self::MOD_PREPEND) {
array_unshift($this->paths[$alias], $cleanPath);
\array_unshift($this->paths[$alias], $cleanPath);
}

if ($mode === self::MOD_APPEND) {
Expand All @@ -388,12 +390,12 @@ protected function addNewPath(string $path, string $alias, string $mode): self
protected static function find($paths, string $file): ?string
{
$paths = (array)$paths;
$file = ltrim($file, "\\/");
$file = \ltrim($file, "\\/");

foreach ($paths as $path) {
$fullPath = self::clean($path . '/' . $file);

if (file_exists($fullPath) || is_dir($fullPath)) {
if (\file_exists($fullPath) || \is_dir($fullPath)) {
return $fullPath;
}
}
Expand All @@ -411,14 +413,14 @@ protected static function find($paths, string $file): ?string
protected static function findViaGlob($paths, string $file): array
{
$paths = (array)$paths;
$file = ltrim($file, "\\/");
$file = \ltrim($file, "\\/");

$path = Arr::first($paths);

$fullPath = self::clean($path . '/' . $file);

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

return $paths ?: [];
}
Expand All @@ -436,7 +438,7 @@ protected function cleanPathInternal(string $path): ?string
}

if (self::hasCDBack($path)) {
$realpath = self::cleanPath((string)realpath($path));
$realpath = self::cleanPath((string)\realpath($path));
return $realpath ?: null;
}

Expand Down Expand Up @@ -465,13 +467,13 @@ protected function getUrlPath(string $path, bool $exitsFile = false): ?string
}

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

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

return ltrim((string)preg_replace($pattern, '', (string)$subject), '/');
return \ltrim((string)\preg_replace($pattern, '', (string)$subject), '/');
}

return null;
Expand All @@ -486,7 +488,7 @@ protected function getUrlPath(string $path, bool $exitsFile = false): ?string
protected static function hasCDBack(string $path): int
{
$path = self::cleanPath($path);
return int(preg_match('(/\.\.$|/\.\./$)', $path));
return int(\preg_match('(/\.\.$|/\.\./$)', $path));
}

/**
Expand All @@ -498,9 +500,9 @@ protected static function hasCDBack(string $path): int
*/
protected function parse(string $source): array
{
[$alias, $path] = explode(':', $source, 2);
[$alias, $path] = \explode(':', $source, 2);

$path = ltrim($path, "\\/");
$path = \ltrim($path, "\\/");
$paths = $this->resolvePaths($alias);

return [$alias, $paths, $path];
Expand Down Expand Up @@ -532,8 +534,8 @@ protected function resolvePaths(string $alias): array
$result[] = $this->getCurrentPath((string)$path);
}

$result = array_filter($result); // remove empty
$result = array_values($result); // reset keys
$result = \array_filter($result); // remove empty
$result = \array_values($result); // reset keys

return $result;
}
Expand All @@ -546,7 +548,7 @@ protected function resolvePaths(string $alias): array
*/
protected function getCurrentPath(string $path): ?string
{
return (string)($this->isReal ? realpath($path) : $path) ?: null;
return (string)($this->isReal ? \realpath($path) : $path) ?: null;
}

/**
Expand All @@ -555,7 +557,7 @@ protected function getCurrentPath(string $path): ?string
*/
protected static function cleanAlias(string $alias): string
{
return (string)preg_replace('/[^a-z0-9_\.-]/i', '', $alias);
return (string)\preg_replace('/[^a-z0-9_\.-]/i', '', $alias);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/PathCodeStyleTest.php
Expand Up @@ -13,6 +13,8 @@
* @link https://github.com/JBZoo/Path
*/

declare(strict_types=1);

namespace JBZoo\PHPUnit;

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/PathComposerTest.php
Expand Up @@ -13,6 +13,8 @@
* @link https://github.com/JBZoo/Path
*/

declare(strict_types=1);

namespace JBZoo\PHPUnit;

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/PathCopyrightTest.php
Expand Up @@ -13,6 +13,8 @@
* @link https://github.com/JBZoo/Path
*/

declare(strict_types=1);

namespace JBZoo\PHPUnit;

/**
Expand All @@ -26,4 +28,5 @@ class PathCopyrightTest extends AbstractCopyrightTest
* @var string
*/
protected $packageName = 'Path';
protected $isPhpStrictType = true;
}
8 changes: 8 additions & 0 deletions tests/PathReadmeTest.php
Expand Up @@ -13,6 +13,8 @@
* @link https://github.com/JBZoo/Path
*/

declare(strict_types=1);

namespace JBZoo\PHPUnit;

/**
Expand All @@ -23,4 +25,10 @@
class PathReadmeTest extends AbstractReadmeTest
{
protected $packageName = 'Path';

protected function setUp(): void
{
parent::setUp();
$this->params['strict_types'] = true;
}
}

0 comments on commit c9ca73c

Please sign in to comment.