Linting (#107)
* Update travis config. * Add phpcs config file. * Exclude rules * Ignore failures in tests * Automatic fixes * Inline ParsingMode enum as class constants * Loosen FormatTest because of excluded rulepull/111/head
parent
83afa0c1b8
commit
5ecab683eb
|
@ -11,6 +11,7 @@ install:
|
|||
- composer install
|
||||
|
||||
script:
|
||||
- vendor/bin/phpcs -n
|
||||
- vendor/bin/phpunit --coverage-clover=coverage.xml
|
||||
|
||||
after_success:
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0"?>
|
||||
<ruleset name="PHP Language Server">
|
||||
<file>src</file>
|
||||
<file>tests</file>
|
||||
<rule ref="PSR2">
|
||||
<exclude name="PSR2.Namespaces.UseDeclaration.MultipleDeclarations"/>
|
||||
<exclude name="PSR2.ControlStructures.ElseIfDeclaration.NotAllowed"/>
|
||||
<exclude name="PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace"/>
|
||||
</rule>
|
||||
</ruleset>
|
|
@ -85,5 +85,4 @@ abstract class Formatter
|
|||
$standard = PHP_CodeSniffer::getConfigData('default_standard') ?? 'PSR2';
|
||||
return explode(',', $standard);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -178,7 +178,8 @@ class Project
|
|||
* @param string $fqn The fully qualified name of the symbol
|
||||
* @return void
|
||||
*/
|
||||
public function removeSymbol(string $fqn) {
|
||||
public function removeSymbol(string $fqn)
|
||||
{
|
||||
unset($this->symbols[$fqn]);
|
||||
unset($this->references[$fqn]);
|
||||
}
|
||||
|
@ -207,7 +208,8 @@ class Project
|
|||
* @param string $uri The URI
|
||||
* @return void
|
||||
*/
|
||||
public function removeReferenceUri(string $fqn, string $uri) {
|
||||
public function removeReferenceUri(string $fqn, string $uri)
|
||||
{
|
||||
if (!isset($this->references[$fqn])) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,8 @@ namespace LanguageServer\Protocol;
|
|||
/**
|
||||
* The kind of a completion entry.
|
||||
*/
|
||||
abstract class CompletionItemKind {
|
||||
abstract class CompletionItemKind
|
||||
{
|
||||
const TEXT = 1;
|
||||
const METHOD = 2;
|
||||
const FUNCTION = 3;
|
||||
|
|
|
@ -8,16 +8,13 @@ use AdvancedJsonRpc\Message as MessageBody;
|
|||
use Sabre\Event\Loop;
|
||||
use RuntimeException;
|
||||
|
||||
abstract class ParsingMode
|
||||
{
|
||||
const HEADERS = 1;
|
||||
const BODY = 2;
|
||||
}
|
||||
|
||||
class ProtocolStreamReader implements ProtocolReader
|
||||
{
|
||||
const PARSE_HEADERS = 1;
|
||||
const PARSE_BODY = 2;
|
||||
|
||||
private $input;
|
||||
private $parsingMode = ParsingMode::HEADERS;
|
||||
private $parsingMode = self::PARSE_HEADERS;
|
||||
private $buffer = '';
|
||||
private $headers = [];
|
||||
private $contentLength;
|
||||
|
@ -37,9 +34,9 @@ class ProtocolStreamReader implements ProtocolReader
|
|||
while (($c = fgetc($this->input)) !== false && $c !== '') {
|
||||
$this->buffer .= $c;
|
||||
switch ($this->parsingMode) {
|
||||
case ParsingMode::HEADERS:
|
||||
case self::PARSE_HEADERS:
|
||||
if ($this->buffer === "\r\n") {
|
||||
$this->parsingMode = ParsingMode::BODY;
|
||||
$this->parsingMode = self::PARSE_BODY;
|
||||
$this->contentLength = (int)$this->headers['Content-Length'];
|
||||
$this->buffer = '';
|
||||
} else if (substr($this->buffer, -2) === "\r\n") {
|
||||
|
@ -48,14 +45,14 @@ class ProtocolStreamReader implements ProtocolReader
|
|||
$this->buffer = '';
|
||||
}
|
||||
break;
|
||||
case ParsingMode::BODY:
|
||||
case self::PARSE_BODY:
|
||||
if (strlen($this->buffer) === $this->contentLength) {
|
||||
if (isset($this->listener)) {
|
||||
$msg = new Message(MessageBody::parse($this->buffer), $this->headers);
|
||||
$listener = $this->listener;
|
||||
$listener($msg);
|
||||
}
|
||||
$this->parsingMode = ParsingMode::HEADERS;
|
||||
$this->parsingMode = self::PARSE_HEADERS;
|
||||
$this->headers = [];
|
||||
$this->buffer = '';
|
||||
}
|
||||
|
|
|
@ -12,7 +12,8 @@ use InvalidArgumentException;
|
|||
* @param string $pattern
|
||||
* @return array
|
||||
*/
|
||||
function findFilesRecursive(string $path, string $pattern): array {
|
||||
function findFilesRecursive(string $path, string $pattern): array
|
||||
{
|
||||
$dir = new \RecursiveDirectoryIterator($path);
|
||||
$ite = new \RecursiveIteratorIterator($dir);
|
||||
$files = new \RegexIterator($ite, $pattern, \RegexIterator::GET_MATCH);
|
||||
|
@ -29,7 +30,8 @@ function findFilesRecursive(string $path, string $pattern): array {
|
|||
* @param string $filepath
|
||||
* @return string
|
||||
*/
|
||||
function pathToUri(string $filepath): string {
|
||||
function pathToUri(string $filepath): string
|
||||
{
|
||||
$filepath = trim(str_replace('\\', '/', $filepath), '/');
|
||||
$parts = explode('/', $filepath);
|
||||
// Don't %-encode the colon after a Windows drive letter
|
||||
|
|
|
@ -36,4 +36,3 @@ class MockProtocolStream implements ProtocolReader, ProtocolWriter
|
|||
$this->listener = $listener;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -59,6 +59,7 @@ abstract class ServerTestCase extends TestCase
|
|||
$this->project->loadDocument($globalReferencesUri);
|
||||
$this->project->loadDocument($useUri);
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
$this->definitionLocations = [
|
||||
|
||||
// Global
|
||||
|
@ -166,6 +167,7 @@ abstract class ServerTestCase extends TestCase
|
|||
0 => new Location($globalReferencesUri, new Range(new Position(10, 0), new Position(10, 13)))
|
||||
]
|
||||
];
|
||||
// @codingStandardsIgnoreEnd
|
||||
}
|
||||
|
||||
protected function getDefinitionLocation(string $fqn): Location
|
||||
|
|
|
@ -9,13 +9,15 @@ use function LanguageServer\pathToUri;
|
|||
|
||||
class GlobalTest extends ServerTestCase
|
||||
{
|
||||
public function testDefinitionFileBeginning() {
|
||||
public function testDefinitionFileBeginning()
|
||||
{
|
||||
// |<?php
|
||||
$result = $this->textDocument->definition(new TextDocumentIdentifier(pathToUri(realpath(__DIR__ . '/../../../../fixtures/references.php'))), new Position(0, 0));
|
||||
$this->assertEquals([], $result);
|
||||
}
|
||||
|
||||
public function testDefinitionEmptyResult() {
|
||||
public function testDefinitionEmptyResult()
|
||||
{
|
||||
// namespace keyword
|
||||
$result = $this->textDocument->definition(new TextDocumentIdentifier(pathToUri(realpath(__DIR__ . '/../../../../fixtures/references.php'))), new Position(2, 4));
|
||||
$this->assertEquals([], $result);
|
||||
|
|
|
@ -16,6 +16,7 @@ class DocumentSymbolTest extends ServerTestCase
|
|||
// Request symbols
|
||||
$uri = pathToUri(realpath(__DIR__ . '/../../../fixtures/symbols.php'));
|
||||
$result = $this->textDocument->documentSymbol(new TextDocumentIdentifier($uri));
|
||||
// @codingStandardsIgnoreStart
|
||||
$this->assertEquals([
|
||||
new SymbolInformation('TEST_CONST', SymbolKind::CONSTANT, $this->getDefinitionLocation('TestNamespace\\TEST_CONST'), 'TestNamespace'),
|
||||
new SymbolInformation('TestClass', SymbolKind::CLASS_, $this->getDefinitionLocation('TestNamespace\\TestClass'), 'TestNamespace'),
|
||||
|
@ -28,5 +29,6 @@ class DocumentSymbolTest extends ServerTestCase
|
|||
new SymbolInformation('TestInterface', SymbolKind::INTERFACE, $this->getDefinitionLocation('TestNamespace\\TestInterface'), 'TestNamespace'),
|
||||
new SymbolInformation('test_function', SymbolKind::FUNCTION, $this->getDefinitionLocation('TestNamespace\\test_function()'), 'TestNamespace'),
|
||||
], $result);
|
||||
// @codingStandardsIgnoreEnd
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,7 +36,8 @@ class ParseErrorsTest extends TestCase
|
|||
$this->textDocument = new Server\TextDocument($project, $client);
|
||||
}
|
||||
|
||||
private function openFile($file) {
|
||||
private function openFile($file)
|
||||
{
|
||||
$textDocumentItem = new TextDocumentItem();
|
||||
$textDocumentItem->uri = 'whatever';
|
||||
$textDocumentItem->languageId = 'php';
|
||||
|
|
|
@ -16,6 +16,7 @@ class SymbolTest extends ServerTestCase
|
|||
{
|
||||
// Request symbols
|
||||
$result = $this->workspace->symbol('');
|
||||
// @codingStandardsIgnoreStart
|
||||
$this->assertEquals([
|
||||
// Namespaced
|
||||
new SymbolInformation('TEST_CONST', SymbolKind::CONSTANT, $this->getDefinitionLocation('TestNamespace\\TEST_CONST'), 'TestNamespace'),
|
||||
|
@ -42,17 +43,20 @@ class SymbolTest extends ServerTestCase
|
|||
new SymbolInformation('test_function', SymbolKind::FUNCTION, $this->getDefinitionLocation('test_function()'), ''),
|
||||
new SymbolInformation('whatever', SymbolKind::FUNCTION, $this->getDefinitionLocation('whatever()'), '')
|
||||
], $result);
|
||||
// @codingStandardsIgnoreEnd
|
||||
}
|
||||
|
||||
public function testQueryFiltersResults()
|
||||
{
|
||||
// Request symbols
|
||||
$result = $this->workspace->symbol('testmethod');
|
||||
// @codingStandardsIgnoreStart
|
||||
$this->assertEquals([
|
||||
new SymbolInformation('staticTestMethod', SymbolKind::METHOD, $this->getDefinitionLocation('TestNamespace\\TestClass::staticTestMethod()'), 'TestNamespace\\TestClass'),
|
||||
new SymbolInformation('testMethod', SymbolKind::METHOD, $this->getDefinitionLocation('TestNamespace\\TestClass::testMethod()'), 'TestNamespace\\TestClass'),
|
||||
new SymbolInformation('staticTestMethod', SymbolKind::METHOD, $this->getDefinitionLocation('TestClass::staticTestMethod()'), 'TestClass'),
|
||||
new SymbolInformation('testMethod', SymbolKind::METHOD, $this->getDefinitionLocation('TestClass::testMethod()'), 'TestClass')
|
||||
], $result);
|
||||
// @codingStandardsIgnoreEnd
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue