Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding better types #2614

Merged
merged 5 commits into from Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/reference/configuration.rst
Expand Up @@ -894,7 +894,7 @@ FilePathResolverExtension
"""""""""""""""""""""""""""""""""""


**Default**: ``"\/home\/daniel\/www\/phpactor\/phpactor"``
**Default**: ``"\/home\/mamazu\/packages\/phpactor\/phpactor"``


.. _param_file_path_resolver.app_name:
Expand Down Expand Up @@ -1348,7 +1348,7 @@ Amount of time (in milliseconds) to wait before responding to a shutdown notific
Internal use only - name path to Phpactor binary


**Default**: ``"\/home\/daniel\/www\/phpactor\/phpactor\/lib\/Extension\/LanguageServer\/..\/..\/..\/bin\/phpactor"``
**Default**: ``"\/home\/mamazu\/packages\/phpactor\/phpactor\/lib\/Extension\/LanguageServer\/..\/..\/..\/bin\/phpactor"``


.. _param_language_server.self_destruct_timeout:
Expand Down
18 changes: 4 additions & 14 deletions lib/Filesystem/Domain/FilePath.php
Expand Up @@ -29,6 +29,9 @@ public static function fromString(string $string): FilePath
return new self($textDocumentUri);
}

/**
* @param array<string> $parts
*/
public static function fromParts(array $parts): FilePath
{
$path = Path::join(...$parts);
Expand All @@ -45,7 +48,7 @@ public static function fromSplFileInfo(SplFileInfo $fileInfo): FilePath
return self::fromString((string) $fileInfo);
}

public static function fromUnknown($path): FilePath
public static function fromUnknown(FilePath|string $path): FilePath
{
if ($path instanceof FilePath) {
return $path;
Expand All @@ -54,19 +57,6 @@ public static function fromUnknown($path): FilePath
if (is_string($path)) {
return self::fromString($path);
}

if (is_array($path)) {
return self::fromParts($path);
}

if ($path instanceof SplFileInfo) {
return self::fromSplFileInfo($path);
}

throw new RuntimeException(sprintf(
'Do not know how to create FilePath from "%s"',
is_object($path) ? get_class($path) : gettype($path)
));
Comment on lines -57 to -69
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those were only used in tests. The rest of the code already uses the correct function.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they were used by tests though, removing this changes the purpose of the function (fromUnknown)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can rename to fromFilePathOrString for clarity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I mean most other function that deal with paths also except both.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible I would like to remove those kinds of methods entirely and reduce the amount of "unknown"s from phpactor. :D

}

public function isDirectory(): bool
Expand Down
3 changes: 3 additions & 0 deletions lib/Filesystem/Domain/FilesystemRegistry.php
Expand Up @@ -8,5 +8,8 @@ public function get(string $name): Filesystem;

public function has(string $name): bool;

/**
* @return array<string>
*/
public function names(): array;
}
1 change: 0 additions & 1 deletion lib/Filesystem/Domain/MappedFilesystemRegistry.php
Expand Up @@ -35,7 +35,6 @@ public function has(string $name): bool
return isset($this->filesystems[$name]);
}

/** @return array<string> */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to the interface.

public function names(): array
{
return array_keys($this->filesystems);
Expand Down
2 changes: 0 additions & 2 deletions lib/Filesystem/Tests/Adapter/AdapterTestCase.php
Expand Up @@ -6,8 +6,6 @@

abstract class AdapterTestCase extends IntegrationTestCase
{
private $filesystem;

public function setUp(): void
{
$this->initWorkspace();
Expand Down
4 changes: 2 additions & 2 deletions lib/Filesystem/Tests/Adapter/IntegrationTestCase.php
Expand Up @@ -17,7 +17,7 @@ protected function initWorkspace(): void
$filesystem->mkdir($this->workspacePath());
}

protected function workspacePath()
protected function workspacePath(): string
{
return realpath(__DIR__.'/..') . '/Workspace';
}
Expand All @@ -31,7 +31,7 @@ protected function loadProject(): void
exec('composer dumpautoload 2> /dev/null');
}

protected function getProjectAutoloader()
protected function getProjectAutoloader(): string
{
return require __DIR__.'/project/vendor/autoload.php';
}
Expand Down
33 changes: 7 additions & 26 deletions lib/Filesystem/Tests/Unit/Domain/FilePathTest.php
Expand Up @@ -2,14 +2,14 @@

namespace Phpactor\Filesystem\Tests\Unit\Domain;

use Generator;
use PHPUnit\Framework\TestCase;
use Phpactor\Filesystem\Domain\FilePath;
use Phpactor\TextDocument\Exception\InvalidUriException;
use Phpactor\TextDocument\TextDocumentUri;
use RuntimeException;
use SplFileInfo;
use Symfony\Component\Filesystem\Path;
use stdClass;

class FilePathTest extends TestCase
{
Expand All @@ -32,14 +32,14 @@ public function testFromParts(): void
/**
* @dataProvider provideUnknown
*/
public function testFromUnknownWith($path, string $expectedPath): void
public function testFromUnknownWith(FilePath|string $path, string $expectedPath): void
{
$filePath = FilePath::fromUnknown($path);
$this->assertInstanceOf(FilePath::class, $filePath);
$this->assertEquals($expectedPath, (string) $filePath);
}

public function provideUnknown()
public function provideUnknown(): Generator
{
yield 'FilePath instance' => [
FilePath::fromString('/foo.php'),
Expand All @@ -65,39 +65,20 @@ public function provideUnknown()
'phar:///foo.php',
'/foo.php',
];

yield 'array' => [
[ 'one', 'two' ],
'/one/two'
];

yield 'SplFileInfo' => [
new SplFileInfo(__FILE__),
Path::canonicalize(__FILE__)
];
yield 'SplFileInfo with scheme' => [
new SplFileInfo((string)TextDocumentUri::fromString(__FILE__)),
Path::canonicalize(__FILE__)
];
}

/**
* @dataProvider provideUnsupportedInput
*/
public function testThrowExceptionOnUnknowableType($input, string $expectedExceptionMessage): void
public function testThrowExceptionOnUnknowableType(string $input, string $expectedExceptionMessage): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage($expectedExceptionMessage);
FilePath::fromUnknown($input);
FilePath::fromString($input);
}

public function provideUnsupportedInput()
public function provideUnsupportedInput(): Generator
{
yield 'object' => [
new stdClass(),
'Do not know',
];

yield 'unsupported scheme' => [
'ftp://host/foo.php',
'are supported', // only X schemes are supported
Expand Down Expand Up @@ -186,7 +167,7 @@ public function testIsNamed(): void

public function testAsSplFileInfo(): void
{
$path1 = FilePath::fromUnknown(new SplFileInfo((string)TextDocumentUri::fromString(__FILE__)));
$path1 = FilePath::fromSplFileInfo(new SplFileInfo((string)TextDocumentUri::fromString(__FILE__)));
self::assertEquals(Path::canonicalize(__FILE__), $path1->__toString());
self::assertEquals(Path::canonicalize(__FILE__), $path1->asSplFileInfo()->__toString());
}
Expand Down
Expand Up @@ -55,7 +55,8 @@ public function testExceptionOnNotFound(): void
$registry->get('barfoo');
}

private function createRegistry(array $filesystems)
/** @param array<string, Filesystem> $filesystems */
private function createRegistry(array $filesystems): MappedFilesystemRegistry
{
return new MappedFilesystemRegistry($filesystems);
}
Expand Down
11 changes: 0 additions & 11 deletions lib/TextDocument/Location.php
Expand Up @@ -18,17 +18,6 @@ public static function fromPathAndOffsets(string $path, int $start, int $end): s
);
}

/**
* @deprecated Use fromPathAndOffsets instead
*/
public static function fromPathAndOffset(string $path, int $offset): self
{
return new self(
TextDocumentUri::fromString($path),
ByteOffsetRange::fromInts($offset, $offset),
);
}

public function uri(): TextDocumentUri
{
return $this->uri;
Expand Down
Expand Up @@ -11,7 +11,6 @@
use Phpactor\WorseReflection\Core\Reflection\Collection\ReflectionMethodCollection as PhpactorReflectionMethodCollection;
use Phpactor\WorseReflection\Core\Reflection\Collection\ReflectionTraitCollection;
use Phpactor\WorseReflection\Core\Reflection\ReflectionClassLike;
use Phpactor\WorseReflection\Core\Reflection\ReflectionEnum;
use Phpactor\WorseReflection\Core\TemplateMap;
use Phpactor\WorseReflection\Core\TypeFactory;
use Phpactor\WorseReflection\Core\Type\ReflectedClassType;
Expand All @@ -22,21 +21,6 @@ abstract class AbstractReflectionClass extends AbstractReflectedNode implements
abstract public function name(): ClassName;
abstract public function docblock(): DocBlock;

public function isInterface(): bool
{
return $this instanceof ReflectionInterface;
}

public function isTrait(): bool
{
return $this instanceof ReflectionTrait;
}

public function isEnum(): bool
{
return $this instanceof ReflectionEnum;
}

public function isClass(): bool
{
return $this instanceof ReflectionClass;
Expand Down
Expand Up @@ -300,11 +300,7 @@ public function sourceCode(): TextDocument

public function isConcrete(): bool
{
if (false === $this->isClass()) {
return false;
}

return false === $this->isAbstract();
return !$this->isAbstract();
}

public function docblock(): DocBlock
Expand Down
13 changes: 0 additions & 13 deletions lib/WorseReflection/Core/Reflection/ReflectionClassLike.php
Expand Up @@ -32,21 +32,8 @@ public function ownMembers(): ReflectionMemberCollection;

public function sourceCode(): TextDocument;

/**
* @deprecated Use instanceof instead
*/
public function isInterface(): bool;

public function isInstanceOf(ClassName $className): bool;


/**
* @deprecated Use instanceof instead
*/
public function isClass(): bool;

public function isEnum(): bool;

public function isConcrete(): bool;

public function docblock(): DocBlock;
Expand Down
Expand Up @@ -7,7 +7,6 @@
use Phpactor\WorseReflection\Core\Type;
use Phpactor\WorseReflection\Core\Reflection\ReflectionClass;
use Phpactor\WorseReflection\Core\Reflection\ReflectionClassLike;
use Phpactor\WorseReflection\Core\Reflection\ReflectionInterface;

class MethodTypeResolver
{
Expand Down Expand Up @@ -73,15 +72,11 @@ private function getTypesFromParentClass(ReflectionClassLike $reflectionClassLik

private function getTypesFromInterfaces(ReflectionClassLike $reflectionClassLike): Type
{
if (false === $reflectionClassLike->isClass()) {
if (!$reflectionClassLike instanceof ReflectionClass) {
return TypeFactory::undefined();
}

/** @var ReflectionClass $reflectionClass */
$reflectionClass = $reflectionClassLike;

/** @var ReflectionInterface $interface */
foreach ($reflectionClass->interfaces() as $interface) {
foreach ($reflectionClassLike->interfaces() as $interface) {
if ($interface->methods()->has($this->method->name())) {
return $interface->methods()->get($this->method->name())->inferredType();
}
Expand Down
Expand Up @@ -48,7 +48,6 @@ class Foobar
function ($class): void {
$this->assertEquals('Foobar', (string) $class->name()->short());
$this->assertInstanceOf(ReflectionClass::class, $class);
$this->assertFalse($class->isInterface());
},
];

Expand Down
Expand Up @@ -38,7 +38,6 @@ enum Barfoo
function ($class): void {
$this->assertEquals('Barfoo', (string) $class->name()->short());
$this->assertInstanceOf(ReflectionEnum::class, $class);
$this->assertTrue($class->isEnum());
},
];
yield 'It reflect enum methods' => [
Expand Down
Expand Up @@ -37,7 +37,6 @@ interface Barfoo
function ($class): void {
$this->assertEquals('Barfoo', (string) $class->name()->short());
$this->assertInstanceOf(ReflectionInterface::class, $class);
$this->assertTrue($class->isInterface());
},
];

Expand Down