Skip to content

Commit

Permalink
refactor: remove deadcode
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Jan 11, 2025
1 parent d8616e8 commit 68728a9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 121 deletions.
99 changes: 12 additions & 87 deletions src/Foundation/Support/PathHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
{
private const CONTAINERS_DIRECTORY_NAME = 'Containers';

public static function getSharedDirectoryPath(): string
{
return app_path(self::getSharedDirectoryName());
}

public static function getAppDirectoryPath(): string
{
return app_path();
Expand All @@ -23,21 +18,16 @@ public static function getAppDirectoryName(): string
return 'app';
}

public static function getSharedDirectoryName(): string
public static function getSectionDirectoryPath(): string
{
return 'Ship';
return app_path(self::getContainersDirectoryName());
}

public static function getContainersDirectoryName(): string
{
return 'Containers';
}

public static function getSectionDirectoryPath(): string
{
return app_path(self::getContainersDirectoryName());
}

public static function getShipFolderNames(): array
{
$names = [];
Expand All @@ -54,6 +44,16 @@ public static function getShipSubDirectories(): array
return File::directories(self::getSharedDirectoryPath());
}

public static function getSharedDirectoryPath(): string
{
return app_path(self::getSharedDirectoryName());
}

public static function getSharedDirectoryName(): string
{
return 'Ship';
}

public static function getSectionContainerNames(string $sectionName): array
{
$names = [];
Expand All @@ -69,81 +69,6 @@ private static function getSectionPath(string $sectionName): string
return app_path(self::CONTAINERS_DIRECTORY_NAME . DIRECTORY_SEPARATOR . $sectionName);
}

/**
* Get the full name (name \ namespace) of a class from its file path
* result example: (string) "I\Am\The\Namespace\Of\This\Class".
*/
public static function getFQCNFromFile(string $filePathName): string
{
return self::getClassNamespaceFromFile($filePathName) . '\\' . self::getClassNameFromFile($filePathName);
}

// reference: https://stackoverflow.com/questions/7153000/get-class-name-from-file
protected static function getClassNamespaceFromFile(string $filePathName): string|null
{
$src = file_get_contents($filePathName);

$tokens = token_get_all($src);
$count = count($tokens);
$i = 0;
$namespace = '';
$isValidNameSpace = false;
while ($i < $count) {
$token = $tokens[$i];
if (is_array($token) && T_NAMESPACE === $token[0]) {
// Found namespace declaration
while (++$i < $count) {
if (';' === $tokens[$i]) {
$isValidNameSpace = true;
$namespace = trim($namespace);

break;
}
$namespace .= is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
}

break;
}
++$i;
}
if (!$isValidNameSpace) {
return null;
}

return $namespace;
}

protected static function getClassNameFromFile(string $filePathName): mixed
{
$phpCode = file_get_contents($filePathName);

$classes = [];
$tokens = token_get_all($phpCode);
$count = count($tokens);
for ($i = 2; $i < $count; ++$i) {
if (T_CLASS == $tokens[$i - 2][0]
&& T_WHITESPACE == $tokens[$i - 1][0]
&& T_STRING == $tokens[$i][0]
) {
$className = $tokens[$i][1];
$classes[] = $className;
}
}

return $classes[0];
}

/**
* Get the last part of a camel case string.
* Example input = helloDearWorld | returns = World.
*/
public static function getClassType(string $className): mixed
{
$array = preg_split('/(?=[A-Z])/', $className);

return end($array);
}

public static function getContainerNames(): array
{
$names = [];
Expand Down
7 changes: 0 additions & 7 deletions tests/Support/Doubles/Dummies/UselessClass.php

This file was deleted.

7 changes: 0 additions & 7 deletions tests/Support/Doubles/Dummies/UselessInterface.php

This file was deleted.

20 changes: 0 additions & 20 deletions tests/Unit/Foundation/Support/PathHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Apiato\Foundation\Support\PathHelper;
use Illuminate\Support\Facades\File;
use PHPUnit\Framework\Attributes\CoversClass;
use Tests\Support\Doubles\Dummies\UselessClass;
use Tests\UnitTestCase;

#[CoversClass(PathHelper::class)]
Expand Down Expand Up @@ -33,25 +32,6 @@ public function testGetSectionContainerNamesReturnsCorrectNames(): void
$this->assertEquals(['container1', 'container2'], $result);
}

public function testGetClassFullNameFromFileReturnsCorrectFullName(): void
{
$filePath = realpath(__DIR__ . '/../../../Support/Doubles/Dummies/UselessClass.php');
File::shouldReceive('get')
->with($filePath)
->andReturn();

$result = PathHelper::getFQCNFromFile($filePath);

$this->assertEquals(UselessClass::class, $result);
}

public function testGetClassTypeReturnsCorrectType(): void
{
$result = PathHelper::getClassType('HelloDearWorld');

$this->assertEquals('World', $result);
}

public function testGetAllContainerNamesReturnsCorrectNames(): void
{
File::shouldReceive('directories')
Expand Down

0 comments on commit 68728a9

Please sign in to comment.