1
0
Fork 0

Fix autofixable formatting errors

pull/357/head
Rob Lourens 2017-05-25 11:39:42 -07:00
parent 05aa935908
commit 2568a42e12
12 changed files with 128 additions and 114 deletions

View File

@ -150,8 +150,9 @@ class CompletionProvider
stripStringOverlap($doc->getRange(new Range(new Position(0, 0), $pos)), '<?php')
);
$list->items[] = $item;
}
// VARIABLES
} /*
VARIABLES */
elseif (
$node instanceof Node\Expression\Variable &&
!(
@ -172,11 +173,11 @@ class CompletionProvider
);
$list->items[] = $item;
}
}
} /*
// MEMBER ACCESS EXPRESSIONS
// $a->c#
// $a->#
MEMBER ACCESS EXPRESSIONS
$a->c#
$a-># */
elseif ($node instanceof Node\Expression\MemberAccessExpression) {
$prefixes = FqnUtilities::getFqnsFromType(
$this->definitionResolver->resolveExpressionNodeToType($node->dereferencableExpression)
@ -196,14 +197,14 @@ class CompletionProvider
}
}
}
}
} /*
// SCOPED PROPERTY ACCESS EXPRESSIONS
// A\B\C::$a#
// A\B\C::#
// A\B\C::$#
// A\B\C::foo#
// TODO: $a::#
SCOPED PROPERTY ACCESS EXPRESSIONS
A\B\C::$a#
A\B\C::#
A\B\C::$#
A\B\C::foo#
TODO: $a::# */
elseif (
($scoped = $node->parent) instanceof Node\Expression\ScopedPropertyAccessExpression ||
($scoped = $node) instanceof Node\Expression\ScopedPropertyAccessExpression
@ -230,7 +231,6 @@ class CompletionProvider
} elseif (ParserHelpers::isConstantFetch($node) ||
($creation = $node->parent) instanceof Node\Expression\ObjectCreationExpression ||
(($creation = $node) instanceof Node\Expression\ObjectCreationExpression)) {
$class = isset($creation) ? $creation->classTypeDesignator : $node;
$prefix = $class instanceof Node\QualifiedName
@ -267,8 +267,7 @@ class CompletionProvider
if (!$isNotFullyQualified && ($fqnStartsWithPrefix || strStartsWith($fqn, $namespacePrefix . "\\" . $prefix))) {
// $fqn = $fqn;
}
elseif (!$isAliased && !array_search($fqn, array_values($namespaceImportTable))) {
} elseif (!$isAliased && !array_search($fqn, array_values($namespaceImportTable))) {
if (empty($prefix)) {
$fqn = '\\' . $fqn;
} elseif (strStartsWith($fqn, $namespacePrefix . "\\" . $prefix)) {

View File

@ -197,8 +197,7 @@ class DefinitionResolver
if ($node instanceof Node\Statement\ClassDeclaration &&
// TODO - this should be bette rrpreented in the parser API
$node->classBaseClause !== null && $node->classBaseClause->baseClass !== null)
{
$node->classBaseClause !== null && $node->classBaseClause->baseClass !== null) {
$def->extends = [(string)$node->classBaseClause->baseClass->getResolvedName()];
// TODO - why is this represented as an array?
// TODO interface implementations.
@ -237,8 +236,7 @@ class DefinitionResolver
// Ignore variable nodes that are part of ScopedPropertyAccessExpression,
// as the scoped property access expression node is handled separately.
if ($node instanceof Node\Expression\Variable &&
!($parent instanceof Node\Expression\ScopedPropertyAccessExpression))
{
!($parent instanceof Node\Expression\ScopedPropertyAccessExpression)) {
// Resolve $this to the containing class definition.
if ($node->getName() === 'this' && $fqn = $this->getContainingClassFqn($node)) {
return $this->index->getDefinition($fqn, false);
@ -272,19 +270,16 @@ class DefinitionResolver
* @param Node $node
* @return string|null
*/
public function resolveReferenceNodeToFqn($node) {
public function resolveReferenceNodeToFqn($node)
{
// TODO all name tokens should be a part of a node
if ($node instanceof Node\QualifiedName) {
return $this->resolveQualifiedNameNodeToFqn($node);
}
else if ($node instanceof Node\Expression\MemberAccessExpression) {
} else if ($node instanceof Node\Expression\MemberAccessExpression) {
return $this->resolveMemberAccessExpressionNodeToFqn($node);
}
else if (ParserHelpers::isConstantFetch($node)) {
} else if (ParserHelpers::isConstantFetch($node)) {
return (string)($node->getNamespacedName());
}
else if (
} else if (
// A\B::C - constant access expression
$node instanceof Node\Expression\ScopedPropertyAccessExpression
&& !($node->memberName instanceof Node\Expression\Variable)
@ -300,7 +295,8 @@ class DefinitionResolver
return null;
}
private function resolveQualifiedNameNodeToFqn(Node\QualifiedName $node) {
private function resolveQualifiedNameNodeToFqn(Node\QualifiedName $node)
{
$parent = $node->parent;
if ($parent instanceof Node\TraitSelectOrAliasClause) {
@ -346,7 +342,8 @@ class DefinitionResolver
return $name;
}
private function resolveMemberAccessExpressionNodeToFqn(Node\Expression\MemberAccessExpression $access) {
private function resolveMemberAccessExpressionNodeToFqn(Node\Expression\MemberAccessExpression $access)
{
if ($access->memberName instanceof Node\Expression) {
// Cannot get definition if right-hand side is expression
return null;
@ -393,7 +390,6 @@ class DefinitionResolver
while ($implementorFqn = array_shift($implementorFqns)) {
// If the member FQN exists, return it
if ($this->index->getDefinition($implementorFqn . $memberSuffix)) {
return $implementorFqn . $memberSuffix;
}
// Get Definition of implementor class
@ -413,7 +409,8 @@ class DefinitionResolver
return $classFqn . $memberSuffix;
}
private function resolveScopedPropertyAccessExpressionNodeToFqn(Node\Expression\ScopedPropertyAccessExpression $scoped) {
private function resolveScopedPropertyAccessExpressionNodeToFqn(Node\Expression\ScopedPropertyAccessExpression $scoped)
{
if ($scoped->scopeResolutionQualifier instanceof Node\Expression\Variable) {
$varType = $this->getTypeFromNode($scoped->scopeResolutionQualifier);
if ($varType === null) {
@ -502,7 +499,6 @@ class DefinitionResolver
// If a function is met, check the parameters and use statements
if (ParserHelpers::isFunctionLike($n)) {
if ($n->parameters !== null) {
foreach ($n->parameters->getElements() as $param) {
if ($param->getName() === $name) {
return $param;
@ -513,8 +509,7 @@ class DefinitionResolver
if ($n instanceof Node\Expression\AnonymousFunctionCreationExpression &&
$n->anonymousFunctionUseClause !== null &&
$n->anonymousFunctionUseClause->useVariableNameList !== null) {
foreach ($n->anonymousFunctionUseClause->useVariableNameList->getElements() as $use
) {
foreach ($n->anonymousFunctionUseClause->useVariableNameList->getElements() as $use) {
if ($use->getName() === $name) {
return $use;
}
@ -585,7 +580,6 @@ class DefinitionResolver
$expr->callableExpression instanceof Node\Expression\ScopedPropertyAccessExpression ||
$expr->callableExpression instanceof Node\Expression\MemberAccessExpression)
) {
// Find the function definition
if ($expr->callableExpression instanceof Node\Expression) {
// Cannot get type for dynamic function call
@ -1023,8 +1017,7 @@ class DefinitionResolver
ParserHelpers::tryGetPropertyDeclaration($node) ??
ParserHelpers::tryGetConstOrClassConstDeclaration($node)
) !== null ||
($node = $node->parent) instanceof Node\Expression\AssignmentExpression)
{
($node = $node->parent) instanceof Node\Expression\AssignmentExpression) {
$declarationNode = $declarationNode ?? $node;
// Property, constant or variable
@ -1084,25 +1077,27 @@ class DefinitionResolver
// INPUT OUTPUT:
// namespace A\B; A\B
else if ($node instanceof Node\Statement\NamespaceDefinition && $node->name instanceof Node\QualifiedName) {
if ($node instanceof Node\Statement\NamespaceDefinition && $node->name instanceof Node\QualifiedName) {
$name = (string) PhpParser\ResolvedName::buildName($node->name->nameParts, $node->getFileContents());
return \count($name) > 0 ? $name : null;
}
// INPUT OUTPUT:
// namespace A\B;
// function a(); A\B\a();
else if ($node instanceof Node\Statement\FunctionDeclaration) {
if ($node instanceof Node\Statement\FunctionDeclaration) {
// Function: use functionName() as the name
$name = (string)$node->getNamespacedName();
return \count($name) > 0 ? $name . '()' : null;
}
// INPUT OUTPUT
// namespace A\B;
// class C {
// function a () {} A\B\C->a()
// static function b() {} A\B\C::b()
// }
else if ($node instanceof Node\MethodDeclaration) {
if ($node instanceof Node\MethodDeclaration) {
// Class method: use ClassName->methodName() as name
$class = $node->getFirstAncestor(
Node\Expression\ObjectCreationExpression::class,
@ -1127,7 +1122,7 @@ class DefinitionResolver
// static $a = 4, $b = 4 A\B\C::$a, A\B\C::$b
// $a = 4, $b = 4 A\B\C->$a, A\B\C->$b // TODO verify variable name
// }
else if (
if (
($propertyDeclaration = ParserHelpers::tryGetPropertyDeclaration($node)) !== null &&
($classDeclaration =
$node->getFirstAncestor(
@ -1136,8 +1131,7 @@ class DefinitionResolver
Node\Statement\InterfaceDeclaration::class,
Node\Statement\TraitDeclaration::class
)
) !== null && isset($classDeclaration->name))
{
) !== null && isset($classDeclaration->name)) {
$name = $node->getName();
if ($propertyDeclaration->isStatic()) {
// Static Property: use ClassName::$propertyName as name
@ -1154,7 +1148,7 @@ class DefinitionResolver
// class C {
// const $a, $b = 4 A\B\C::$a(), A\B\C::$b
// }
else if (($constDeclaration = ParserHelpers::tryGetConstOrClassConstDeclaration($node)) !== null) {
if (($constDeclaration = ParserHelpers::tryGetConstOrClassConstDeclaration($node)) !== null) {
if ($constDeclaration instanceof Node\Statement\ConstDeclaration) {
// Basic constant: use CONSTANT_NAME as name
return (string)$node->getNamespacedName();
@ -1174,7 +1168,7 @@ class DefinitionResolver
return (string)$classDeclaration->getNamespacedName() . '::' . $node->getName();
}
else if (ParserHelpers::isConstDefineExpression($node)) {
if (ParserHelpers::isConstDefineExpression($node)) {
return $node->argumentExpressionList->children[0]->expression->getStringContentsText();
}
@ -1186,7 +1180,8 @@ class DefinitionResolver
* @param $variableName
* @return DocBlock\Tags\Param | null
*/
private function tryGetDocBlockTagForParameter($docBlock, $variableName) {
private function tryGetDocBlockTagForParameter($docBlock, $variableName)
{
if ($docBlock === null) {
return null;
}

View File

@ -6,8 +6,10 @@ namespace LanguageServer;
use Microsoft\PhpParser;
use Microsoft\PhpParser\Node;
class ParserHelpers {
public static function isConstantFetch(Node $node) : bool {
class ParserHelpers
{
public static function isConstantFetch(Node $node) : bool
{
$parent = $node->parent;
return
(
@ -34,18 +36,21 @@ class ParserHelpers {
));
}
public static function getFunctionLikeDeclarationFromParameter(Node\Parameter $node) {
public static function getFunctionLikeDeclarationFromParameter(Node\Parameter $node)
{
return $node->parent->parent;
}
public static function isFunctionLike(Node $node) {
public static function isFunctionLike(Node $node)
{
return
$node instanceof Node\Statement\FunctionDeclaration ||
$node instanceof Node\MethodDeclaration ||
$node instanceof Node\Expression\AnonymousFunctionCreationExpression;
}
public static function isBooleanExpression($expression) : bool {
public static function isBooleanExpression($expression) : bool
{
if (!($expression instanceof Node\Expression\BinaryExpression)) {
return false;
}
@ -77,7 +82,8 @@ class ParserHelpers {
* @param Node $node
* @return Node\PropertyDeclaration | null $node
*/
public static function tryGetPropertyDeclaration(Node $node) {
public static function tryGetPropertyDeclaration(Node $node)
{
if ($node instanceof Node\Expression\Variable &&
(($propertyDeclaration = $node->parent->parent) instanceof Node\PropertyDeclaration ||
($propertyDeclaration = $propertyDeclaration->parent) instanceof Node\PropertyDeclaration)
@ -92,7 +98,8 @@ class ParserHelpers {
* @param Node $node
* @return Node\Statement\ConstDeclaration | Node\ClassConstDeclaration | null $node
*/
public static function tryGetConstOrClassConstDeclaration(Node $node) {
public static function tryGetConstOrClassConstDeclaration(Node $node)
{
if (
$node instanceof Node\ConstElement && (
($constDeclaration = $node->parent->parent) instanceof Node\ClassConstDeclaration ||
@ -109,7 +116,8 @@ class ParserHelpers {
* @param Node $node
* @return bool
*/
public static function isConstDefineExpression(Node $node): bool {
public static function isConstDefineExpression(Node $node): bool
{
return $node instanceof Node\Expression\CallExpression
&& $node->callableExpression instanceof Node\QualifiedName
&& strtolower($node->callableExpression->getText()) === 'define'

View File

@ -2,7 +2,6 @@
namespace LanguageServer;
class ParserKind
{
const PHP_PARSER = 1;

View File

@ -54,8 +54,7 @@ class SymbolInformation
$symbol->kind = SymbolKind::CLASS_;
} else if ($node instanceof Node\Statement\TraitDeclaration) {
$symbol->kind = SymbolKind::CLASS_;
}
else if (\LanguageServer\ParserHelpers::isConstDefineExpression($node)) {
} else if (\LanguageServer\ParserHelpers::isConstDefineExpression($node)) {
// constants with define() like
// define('TEST_DEFINE_CONSTANT', false);
$symbol->kind = SymbolKind::CONSTANT;
@ -77,9 +76,7 @@ class SymbolInformation
$symbol->kind = SymbolKind::PROPERTY;
} else if ($node instanceof Node\ConstElement) {
$symbol->kind = SymbolKind::CONSTANT;
}
else if (
} else if (
(
($node instanceof Node\Expression\AssignmentExpression)
&& $node->leftOperand instanceof Node\Expression\Variable

View File

@ -10,7 +10,8 @@ use Sabre\Uri;
use Microsoft\PhpParser;
use Microsoft\PhpParser\Node;
class TreeAnalyzer {
class TreeAnalyzer
{
private $parser;
/** @var Node */
@ -28,7 +29,8 @@ class TreeAnalyzer {
* @param DefinitionResolver $definitionResolver
* @param $uri
*/
public function __construct($parser, $content, $docBlockFactory, $definitionResolver, $uri) {
public function __construct($parser, $content, $docBlockFactory, $definitionResolver, $uri)
{
$this->uri = $uri;
$this->parser = $parser;
$this->docBlockFactory = $docBlockFactory;
@ -41,7 +43,8 @@ class TreeAnalyzer {
$this->collectDefinitionsAndReferences($this->stmts);
}
public function collectDefinitionsAndReferences(Node $stmts) {
public function collectDefinitionsAndReferences(Node $stmts)
{
foreach ($stmts::CHILD_NAMES as $name) {
$node = $stmts->$name;
@ -79,7 +82,8 @@ class TreeAnalyzer {
}
}
public function update($node) {
public function update($node)
{
$fqn = ($this->definitionResolver)::getDefinedFqn($node);
// Only index definitions with an FQN (no variables)
if ($fqn !== null) {
@ -98,7 +102,6 @@ class TreeAnalyzer {
))
|| ($parent instanceof Node\Statement\NamespaceDefinition && $parent->name !== null && $parent->name->getStart() === $node->getStart()))
) {
$fqn = $this->definitionResolver->resolveReferenceNodeToFqn($node);
if ($fqn !== null) {
$this->addReference($fqn, $node);
@ -138,7 +141,8 @@ class TreeAnalyzer {
$this->collectDefinitionsAndReferences($node);
}
public function getDiagnostics() {
public function getDiagnostics()
{
return $this->diagnostics ?? [];
}
@ -150,19 +154,23 @@ class TreeAnalyzer {
$this->referenceNodes[$fqn][] = $node;
}
public function getDefinitions() {
public function getDefinitions()
{
return $this->definitions ?? [];
}
public function getDefinitionNodes() {
public function getDefinitionNodes()
{
return $this->definitionNodes ?? [];
}
public function getReferenceNodes() {
public function getReferenceNodes()
{
return $this->referenceNodes ?? [];
}
public function getStmts() {
public function getStmts()
{
return $this->stmts;
}
/**

View File

@ -172,6 +172,7 @@ function getVendorDir(\stdClass $composerJson = null): string
return $composerJson->config->{'vendor-dir'} ?? 'vendor';
}
function strStartsWith(string $haystack, string $prefix): bool {
function strStartsWith(string $haystack, string $prefix): bool
{
return empty($prefix) || strpos($haystack, $prefix) === 0;
}

View File

@ -44,7 +44,8 @@ class PhpDocumentTest extends TestCase
$this->assertEquals('SomeClass', (string)$node);
}
private function assertQualifiedName($node) {
private function assertQualifiedName($node)
{
$this->assertInstanceOf(Node\QualifiedName::class, $node);
}

View File

@ -499,7 +499,8 @@ class CompletionTest extends TestCase
], true), $items);
}
private function assertCompletionsListSubset(CompletionList $subsetList, CompletionList $list) {
private function assertCompletionsListSubset(CompletionList $subsetList, CompletionList $list)
{
foreach ($subsetList->items as $expectedItem) {
$this->assertContains($expectedItem, $list->items, null, null, false);
}

View File

@ -8,7 +8,8 @@ use function LanguageServer\{strStartsWith};
class UtilsTest extends TestCase
{
public function testStrStartsWithDataProvider(): array {
public function testStrStartsWithDataProvider(): array
{
return [
['a', 'b', false],
['', 'a', false],

View File

@ -25,7 +25,8 @@ $frameworksDir = realpath(__DIR__ . '/../../validation/frameworks');
class ValidationTest extends TestCase
{
public function frameworkErrorProvider() {
public function frameworkErrorProvider()
{
global $frameworksDir;
$frameworks = glob($frameworksDir . '/*', GLOB_ONLYDIR);
@ -61,7 +62,8 @@ class ValidationTest extends TestCase
* @param $testCaseFile
* @param $frameworkName
*/
public function testDefinitionErrors($testCaseFile, $frameworkName) {
public function testDefinitionErrors($testCaseFile, $frameworkName)
{
$fileContents = file_get_contents($testCaseFile);
$actualValues = $this->getActualTestValues($testCaseFile, $fileContents);
@ -90,7 +92,8 @@ class ValidationTest extends TestCase
}
}
private function getActualTestValues($filename, $fileContents): array {
private function getActualTestValues($filename, $fileContents): array
{
global $frameworksDir;
$index = new Index();
@ -184,6 +187,7 @@ class ValidationTest extends TestCase
}
}
function getExpectedValuesFile($testCaseFile): string {
function getExpectedValuesFile($testCaseFile): string
{
return $testCaseFile . '.expected.json';
}