Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
use function array_unshift;
use function count;
use function explode;
use function getcwd;
use function implode;
use function in_array;
use function pathinfo;
use function preg_split;
use function str_replace;
use function strlen;
use function strtolower;
use function substr;
Expand Down Expand Up @@ -53,7 +55,7 @@ public function getTypeNameFromProjectPath(string $path): ?string
}

/** @var string[] $pathParts */
$pathParts = preg_split('~[/\\\]~', $path);
$pathParts = preg_split('~[/\\\]~', str_replace($this->getCurrentDir(), '', $path));
$rootNamespace = null;
while (count($pathParts) > 0) {
array_shift($pathParts);
Expand Down Expand Up @@ -85,4 +87,11 @@ public function getTypeNameFromProjectPath(string $path): ?string
return substr($typeName, 0, -strlen('.' . $extension));
}

protected function getCurrentDir(): string
{
$dir = getcwd();

return $dir !== false ? $dir : '';
}

}
29 changes: 29 additions & 0 deletions tests/Sniffs/Files/FilepathNamespaceExtractorStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace SlevomatCodingStandard\Sniffs\Files;

class FilepathNamespaceExtractorStub extends FilepathNamespaceExtractor
{

/** @var string */
private $currentDir;

/**
* @param string[] $rootNamespaces directory(string) => namespace
* @param string[] $skipDirs
* @param string[] $extensions index(integer) => extension
* @param string $currentDir
*/
public function __construct(array $rootNamespaces, array $skipDirs, array $extensions, string $currentDir)
{
parent::__construct($rootNamespaces, $skipDirs, $extensions);

$this->currentDir = $currentDir;
}

protected function getCurrentDir(): string
{
return $this->currentDir;
}

}
31 changes: 28 additions & 3 deletions tests/Sniffs/Files/FilepathNamespaceExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,81 +21,103 @@ public function dataGetTypeNameFromProjectPath(): array
'unknown.php',
null,
['php'],
'',
],
[
$root . '/unknown',
null,
['php'],
$root,
],
[
$root . '/app',
null,
['php'],
$root,
],
[
$root . '/app/components',
null,
['php'],
$root,
],
[
$root . '/app/components.php',
'Slevomat\\components',
['php'],
$root,
],
[
$root . '/app/Foo/Foo.swift',
null,
['php'],
$root,
],
[
$root . '/app/Foo/Foo.php',
'Slevomat\\Foo\\Foo',
['php'],
$root,
],
[
$root . '/tests/UnitTestCase.php',
'Slevomat\\UnitTestCase',
['php'],
$root,
],
[
$root . '/app/model/Auth/DatabaseAuthenticator.php',
'Slevomat\\Auth\\DatabaseAuthenticator',
['php'],
$root,
],
[
$root . '/app/services/Delivery/Premise/AbstractPremiseImporter.php',
'Slevomat\\Delivery\\Premise\\AbstractPremiseImporter',
['php'],
$root,
],
[
$root . '/app/ui/Admin/Newsletter/CreatePresenter.php',
'Slevomat\\UI\\Admin\\Newsletter\\CreatePresenter',
['php'],
$root,
],
[
$root . '/tests/services/Delivery/Goods3rd/data/StatusEnum.php',
'Slevomat\\Delivery\\Goods3rd\\StatusEnum',
['php'],
$root,
],
[
$root . '/tests/services/Delivery/Goods3rd/data/StatusEnum.PHP',
'Slevomat\\Delivery\\Goods3rd\\StatusEnum',
['php'],
$root,
],
[
$root . '/tests/services/Delivery/Goods3rd/data/StatusEnum.php',
'Slevomat\\Delivery\\Goods3rd\\StatusEnum',
['PHP'],
$root,
],
[
$root . '/tests/services/Delivery/Goods3rd/data/StatusEnum.php',
'Slevomat\\Delivery\\Goods3rd\\StatusEnum',
['php', 'lsp'],
$root,
],
[
$root . '/app/Foo/FooTest.phpt',
'Slevomat\\Foo\\FooTest',
['php', 'phpt'],
$root,
],
[
'/www/src/src/Foo/FooTest.php',
'Slevomat\\Foo\\FooTest',
['php', 'phpt'],
'/www/src',
],
];
}
Expand All @@ -105,17 +127,20 @@ public function dataGetTypeNameFromProjectPath(): array
* @param string $path
* @param string|null $expectedNamespace
* @param string[] $extensions
* @param string $currentDir
*/
public function testGetTypeNameFromProjectPath(string $path, ?string $expectedNamespace, array $extensions): void
public function testGetTypeNameFromProjectPath(string $path, ?string $expectedNamespace, array $extensions, string $currentDir): void
{
$extractor = new FilepathNamespaceExtractor(
$extractor = new FilepathNamespaceExtractorStub(
[
'app/ui' => 'Slevomat\\UI',
'app' => 'Slevomat',
'tests' => 'Slevomat',
'src' => 'Slevomat',
],
['components', 'forms', 'model', 'models', 'services', 'stubs', 'data'],
$extensions
$extensions,
$currentDir
);
$path = str_replace('/', DIRECTORY_SEPARATOR, $path);
self::assertSame($expectedNamespace, $extractor->getTypeNameFromProjectPath($path));
Expand Down