Skip to content
This repository was archived by the owner on Mar 6, 2022. It is now read-only.
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/.phpunit.result.cache
/cache
/vendor
/tests/Workspace
/tests/Assets/workspace
Expand Down
1 change: 1 addition & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in('bin')
->in('lib')
->in('tests')
->exclude([
Expand Down
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 7.2
- 7.3
- 7.4

Expand All @@ -15,7 +14,4 @@ before_script:
- composer install

script:
- ./vendor/bin/php-cs-fixer fix --dry-run
- ./vendor/bin/phpstan analyse lib -c phpstan.neon
- ./vendor/bin/phpunit

- composer integrate-ci
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
Worse Completion
================
Phpactor Completion
===================

[![Build Status](https://travis-ci.org/phpactor/completion.svg?branch=master)](https://travis-ci.org/phpactor/completion)

PHP Code Completion library which uses
[Phpactor's](https://github.com/phpactor/phpactor) [Worse
Reflection](https://github.com/phpactor/worse-reflection).
PHP Code Completion library for [Phpactor](https://github.com/phpactor/phpactor).

This package includes:

- Completion APIs and implementations.
- Phpactor RPC extension and handlers.
- Language Server extension and handlers.

Usage
-----
Expand Down
52 changes: 52 additions & 0 deletions bin/completion
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env php
<?php

use Acme\Extension\StupidCompletion\StupidCompletionExtension;
use Phpactor\Completion\Extension\CompletionExtension;
use Phpactor\Completion\Extension\CompletionRpcExtension;
use Phpactor\Completion\Extension\CompletionWorseExtension;
use Phpactor\Completion\Extension\LanguageServerCompletionExtension;
use Phpactor\Container\PhpactorContainer;
use Phpactor\Extension\ClassToFile\ClassToFileExtension;
use Phpactor\Extension\ComposerAutoloader\ComposerAutoloaderExtension;
use Phpactor\Extension\Console\ConsoleExtension;
use Phpactor\Extension\LanguageServer\LanguageServerExtension;
use Phpactor\Extension\Logger\LoggingExtension;
use Phpactor\Extension\ReferenceFinder\ReferenceFinderExtension;
use Phpactor\Extension\Rpc\RpcExtension;
use Phpactor\Extension\SourceCodeFilesystem\SourceCodeFilesystemExtension;
use Phpactor\Extension\WorseReflection\WorseReflectionExtension;
use Phpactor\FilePathResolverExtension\FilePathResolverExtension;
use Phpactor\Indexer\Extension\IndexerExtension;
use Symfony\Component\Console\Application;

require __DIR__ . '/../vendor/autoload.php';

$container = PhpactorContainer::fromExtensions([
ConsoleExtension::class,
FilePathResolverExtension::class,
LoggingExtension::class,
SourceCodeFilesystemExtension::class,
WorseReflectionExtension::class,
ClassToFileExtension::class,
ComposerAutoloaderExtension::class,
RpcExtension::class,
LanguageServerExtension::class,
CompletionExtension::class,
LanguageServerCompletionExtension::class,
CompletionWorseExtension::class,
CompletionRpcExtension::class,
], [
FilePathResolverExtension::PARAM_APPLICATION_ROOT => __DIR__ . '/../',
WorseReflectionExtension::PARAM_ENABLE_CACHE => true,
LoggingExtension::PARAM_ENABLED => true,
LoggingExtension::PARAM_LEVEL => 'debug',
LoggingExtension::PARAM_PATH=> 'php://stderr',
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

not indexer (also include this in phpcs fixer)

LanguageServerExtension::PARAM_WELCOME_MESSAGE => 'Completion Test Application',
]);

$application = new Application();
$application->setCommandLoader(
$container->get(ConsoleExtension::SERVICE_COMMAND_LOADER)
);
$application->run();
1 change: 0 additions & 1 deletion cache/bcc78826871801191289312312376726.map

This file was deleted.

1 change: 0 additions & 1 deletion cache/e258838c52e5409d9b0491a9deb681b6.map

This file was deleted.

33 changes: 25 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,35 @@
"minimum-stability": "dev",
"description": "Completion library for Worse Reflection",
"license": "MIT",
"prefer-stable": true,
"authors": [
{
"name": "Daniel Leech",
"email": "[email protected]"
}
],
"require": {
"php": "^7.2",
"phpactor/worse-reflection": "~0.2",
"php": "^7.3",
"phpactor/worse-reflection": "~0.4",
"phpactor/source-code-filesystem": "~0.1",
"phpactor/class-to-file": "~0.3",
"phpactor/text-document": "^1.0"
},
"require-dev": {
"phpbench/phpbench": "^1.0@dev",
"phpunit/phpunit": "~7.0",
"phpactor/test-utils": "^1.0@dev",
"phpstan/phpstan": "~0.11.0",
"friendsofphp/php-cs-fixer": "~2.15.0"
"phpbench/phpbench": "^1.0",
"phpunit/phpunit": "~9.0",
"phpactor/test-utils": "^1.0",
"phpstan/phpstan": "~0.12.0",
"friendsofphp/php-cs-fixer": "~2.15.0",
"dms/phpunit-arraysubset-asserts": "dev-master",
"phpspec/prophecy-phpunit": "dev-master",

"phpactor/container": "^1.0",
"phpactor/logging-extension": "~0.1",
"phpactor/rpc-extension": "~0.1",
"phpactor/worse-reflection-extension": "~0.2",
"phpactor/source-code-filesystem-extension": "~0.1",
"phpactor/language-server-extension": "~0.2"
},
"autoload": {
"psr-4": {
Expand All @@ -39,9 +49,16 @@
}
},
"scripts": {
"integrate": [
"integrate-ci": [
"vendor/bin/php-cs-fixer fix --dry-run",
"vendor/bin/phpstan analyse lib -c phpstan.neon",
"vendor/bin/phpunit",
"bin/completion"
],
"integrate": [
"vendor/bin/php-cs-fixer fix",
"vendor/bin/phpstan analyse lib -c phpstan.neon",
"bin/completion",
"vendor/bin/phpunit"
]
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Bridge/TolerantParser/ChainTolerantCompletor.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function complete(TextDocument $source, ByteOffset $byteOffset): Generato
}
}

private function truncateSource(string $source, int $byteOffset)
private function truncateSource(string $source, int $byteOffset): string
{
// truncate source at byte offset - we don't want the rest of the source
// file contaminating the completion (for example `$foo($<>\n $bar =
Expand All @@ -74,7 +74,7 @@ private function truncateSource(string $source, int $byteOffset)
return $truncatedSource;
}

private function filterNonQualifyingClasses(Node $node)
private function filterNonQualifyingClasses(Node $node): array
{
return array_filter($this->tolerantCompletors, function (TolerantCompletor $completor) use ($node) {
if (!$completor instanceof TolerantQualifiable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function couldComplete(Node $node): ?Node
return null;
}

private function isMemberNode(?Node $node)
private function isMemberNode(?Node $node): bool
{
if (null === $node) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\QualifiedName;
use Microsoft\PhpParser\ResolvedName;
use Phpactor\ClassFileConverter\Domain\ClassName;
use Phpactor\ClassFileConverter\Domain\FilePath;
use Phpactor\ClassFileConverter\Domain\FileToClass;
use Phpactor\Completion\Bridge\TolerantParser\Qualifier\ClassQualifier;
Expand Down Expand Up @@ -85,10 +86,7 @@ public function complete(Node $node, TextDocument $source, ByteOffset $offset):
}
}

/**
* @return TextDocument|null
*/
private function getClassNameForImport($candidate, array $imports, string $currentNamespace = null)
private function getClassNameForImport(ClassName $candidate, array $imports, string $currentNamespace = null): ?string
{
$candidateNamespace = $candidate->namespace();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Phpactor\Completion\Bridge\TolerantParser\WorseReflection;

use Generator;
use LogicException;
use Microsoft\PhpParser\MissingToken;
use Microsoft\PhpParser\Node;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function __construct(Reflector $reflector, ObjectFormatter $formatter, Va
$this->variableCompletionHelper = $variableCompletionHelper ?: new VariableCompletionHelper($reflector);
}

protected function populateResponse(Node $callableExpression, ReflectionFunctionLike $functionLikeReflection, array $variables)
protected function populateResponse(Node $callableExpression, ReflectionFunctionLike $functionLikeReflection, array $variables): Generator
{
// function has no parameters, return empty handed
if ($functionLikeReflection->parameters()->count() === 0) {
Expand Down Expand Up @@ -85,7 +86,7 @@ protected function populateResponse(Node $callableExpression, ReflectionFunction
}
}

private function paramIndex(Node $node)
private function paramIndex(Node $node): int
{
$argumentList = $this->argumentListFromNode($node);

Expand Down Expand Up @@ -118,7 +119,7 @@ private function paramIndex(Node $node)
return $index;
}

private function isVariableValidForParameter(WorseVariable $variable, ReflectionParameter $parameter)
private function isVariableValidForParameter(WorseVariable $variable, ReflectionParameter $parameter): bool
{
if ($parameter->inferredTypes()->best() == Type::undefined()) {
return true;
Expand Down Expand Up @@ -146,22 +147,21 @@ private function isVariableValidForParameter(WorseVariable $variable, Reflection
return false;
}

private function reflectedParameter(ReflectionFunctionLike $reflectionFunctionLike, $paramIndex)
private function reflectedParameter(ReflectionFunctionLike $reflectionFunctionLike, int $paramIndex): ReflectionParameter
{
$reflectedIndex = 1;
/** @var ReflectionParameter $parameter */
foreach ($reflectionFunctionLike->parameters() as $parameter) {
if ($reflectedIndex == $paramIndex) {
return $parameter;
break;
}
$reflectedIndex++;
}

throw new LogicException(sprintf('Could not find parameter for index "%s"', $paramIndex));
}

private function numberOfArgumentsExceedParameterArity(ReflectionFunctionLike $reflectionFunctionLike, $paramIndex)
private function numberOfArgumentsExceedParameterArity(ReflectionFunctionLike $reflectionFunctionLike, int $paramIndex): bool
{
return $reflectionFunctionLike->parameters()->count() < $paramIndex;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function variableCompletions(Node $node, string $source, ByteOffset $offs
return $variables;
}

private function offsetToReflect(Node $node, int $offset)
private function offsetToReflect(Node $node, int $offset): int
{
$parentNode = $node->parent;

Expand All @@ -89,7 +89,7 @@ private function offsetToReflect(Node $node, int $offset)
return $offset;
}

private function orderedVariablesUntilOffset(Frame $frame, int $offset)
private function orderedVariablesUntilOffset(Frame $frame, int $offset): array
{
return array_reverse(iterator_to_array($frame->locals()->lessThan($offset)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ private function populateSuggestions(SymbolContext $symbolContext, Type $type, b
if ($classReflection instanceof ReflectionClass ||
$classReflection instanceof ReflectionInterface
) {
/** @var ReflectionClass|ReflectionInterface */
foreach ($classReflection->constants() as $constant) {
yield Suggestion::createWithOptions($constant->name(), [
'type' => Suggestion::TYPE_CONSTANT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private function definedNamesFor(array $reflectedFunctions, string $partialName)
return $this->filterFunctions($functions, $partialName);
}

private function reflectedFunctions(string $source)
private function reflectedFunctions(string $source): array
{
$functionNames = [];
foreach ($this->reflector->reflectFunctionsIn($source) as $function) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function complete(Node $node, TextDocument $source, ByteOffset $offset):
}
}

private function paramIndex(Node $node)
private function paramIndex(Node $node): int
{
$argumentList = $this->argumentListFromNode($node);

Expand Down Expand Up @@ -105,7 +105,7 @@ private function paramIndex(Node $node)
return $index;
}

private function isVariableValidForParameter(WorseVariable $variable, ReflectionParameter $parameter)
private function isVariableValidForParameter(WorseVariable $variable, ReflectionParameter $parameter): bool
{
if ($parameter->inferredTypes()->best() == Type::undefined()) {
return true;
Expand Down Expand Up @@ -133,22 +133,21 @@ private function isVariableValidForParameter(WorseVariable $variable, Reflection
return false;
}

private function reflectedParameter(ReflectionFunctionLike $reflectionFunctionLike, $paramIndex)
private function reflectedParameter(ReflectionFunctionLike $reflectionFunctionLike, int $paramIndex): ReflectionParameter
{
$reflectedIndex = 1;
/** @var ReflectionParameter $parameter */
foreach ($reflectionFunctionLike->parameters() as $parameter) {
if ($reflectedIndex == $paramIndex) {
return $parameter;
break;
}
$reflectedIndex++;
}

throw new LogicException(sprintf('Could not find parameter for index "%s"', $paramIndex));
}

private function numberOfArgumentsExceedParameterArity(ReflectionFunctionLike $reflectionFunctionLike, $paramIndex)
private function numberOfArgumentsExceedParameterArity(ReflectionFunctionLike $reflectionFunctionLike, int $paramIndex): bool
{
return $reflectionFunctionLike->parameters()->count() < $paramIndex;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public function signatureHelp(
$position = substr_count($text, ',');
}

/** @var Node|QualfifiedName $callable */
$callable = $node->callableExpression;

if ($callable instanceof QualifiedName) {
Expand Down Expand Up @@ -124,7 +123,7 @@ private function signatureHelpForFunction(QualifiedName $callable, int $position
return $this->createSignatureHelp($functionReflection, $position);
}

private function createSignatureHelp(ReflectionFunctionLike $functionReflection, int $position)
private function createSignatureHelp(ReflectionFunctionLike $functionReflection, int $position): SignatureHelp
{
$signatures = [];
$parameters = [];
Expand All @@ -141,7 +140,7 @@ private function createSignatureHelp(ReflectionFunctionLike $functionReflection,
return new SignatureHelp($signatures, $position);
}

private function signatureHelpForScopedPropertyAccess(ScopedPropertyAccessExpression $callable, CallExpression $node, int $position)
private function signatureHelpForScopedPropertyAccess(ScopedPropertyAccessExpression $callable, CallExpression $node, int $position): SignatureHelp
{
$scopeResolutionQualifier = $callable->scopeResolutionQualifier;

Expand Down
4 changes: 2 additions & 2 deletions lib/Bridge/WorseReflection/Formatter/ClassFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

class ClassFormatter implements Formatter
{
public function canFormat($object): bool
public function canFormat(object $object): bool
{
return $object instanceof ReflectionClass;
}

public function format(ObjectFormatter $formatter, $class): string
public function format(ObjectFormatter $formatter, object $class): string
{
assert($class instanceof ReflectionClass);

Expand Down
Loading