From b99fb94840e8d160529b2c769dfe8319d70a6b2b Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Thu, 29 Sep 2016 18:14:50 +0200 Subject: [PATCH] Correct code style --- src/Client/Window.php | 120 +++++++-------- src/LanguageServer.php | 10 +- src/PhpDocument.php | 281 ++++++++++++++++++------------------ src/Project.php | 140 +++++++++--------- src/Server/TextDocument.php | 3 +- src/Server/Workspace.php | 116 +++++++-------- src/SymbolFinder.php | 27 ++-- src/utils.php | 12 +- tests/Utils/FileUriTest.php | 4 +- 9 files changed, 353 insertions(+), 360 deletions(-) diff --git a/src/Client/Window.php b/src/Client/Window.php index 431ef7c..192ab47 100644 --- a/src/Client/Window.php +++ b/src/Client/Window.php @@ -1,60 +1,60 @@ -protocolWriter = $protocolWriter; - } - - /** - * The show message notification is sent from a server to a client to ask the client to display a particular message in the user interface. - * - * @param int $type - * @param string $message - */ - public function showMessage(int $type, string $message) - { - $this->protocolWriter->write(new Message(new NotificationBody( - 'window/showMessage', - (object)[ - 'type' => $type, - 'message' => $message - ] - ))); - } - - /** - * The log message notification is sent from the server to the client to ask the client to log a particular message. - * - * @param int $type - * @param string $message - */ - public function logMessage(int $type, string $message) - { - $this->protocolWriter->write(new Message(new NotificationBody( - 'window/logMessage', - (object)[ - 'type' => $type, - 'message' => $message - ] - ))); - } -} \ No newline at end of file +protocolWriter = $protocolWriter; + } + + /** + * The show message notification is sent from a server to a client to ask the client to display a particular message in the user interface. + * + * @param int $type + * @param string $message + */ + public function showMessage(int $type, string $message) + { + $this->protocolWriter->write(new Message(new NotificationBody( + 'window/showMessage', + (object)[ + 'type' => $type, + 'message' => $message + ] + ))); + } + + /** + * The log message notification is sent from the server to the client to ask the client to log a particular message. + * + * @param int $type + * @param string $message + */ + public function logMessage(int $type, string $message) + { + $this->protocolWriter->write(new Message(new NotificationBody( + 'window/logMessage', + (object)[ + 'type' => $type, + 'message' => $message + ] + ))); + } +} diff --git a/src/LanguageServer.php b/src/LanguageServer.php index c9aea3f..1876040 100644 --- a/src/LanguageServer.php +++ b/src/LanguageServer.php @@ -133,22 +133,22 @@ class LanguageServer extends \AdvancedJsonRpc\Dispatcher $numTotalFiles = count($fileList); $startTime = microtime(true); - + $processFile = function() use (&$fileList, &$processFile, $rootPath, $numTotalFiles, $startTime) { if ($file = array_pop($fileList)) { - + $uri = pathToUri($file); $fileNum = $numTotalFiles - count($fileList); $shortName = substr($file, strlen($rootPath)+1); $this->client->window->logMessage(3, "Parsing file $fileNum/$numTotalFiles: $shortName."); - + $this->project->getDocument($uri)->updateContent(file_get_contents($file)); - + Loop\setTimeout($processFile, 0); } else { $duration = (int)(microtime(true) - $startTime); - $mem = (int)(memory_get_usage(true)/(1024*1024)); + $mem = (int)(memory_get_usage(true) / (1024 * 1024)); $this->client->window->logMessage(3, "All PHP files parsed in $duration seconds. $mem MiB allocated."); } }; diff --git a/src/PhpDocument.php b/src/PhpDocument.php index 6049f0d..424afd3 100644 --- a/src/PhpDocument.php +++ b/src/PhpDocument.php @@ -1,141 +1,140 @@ -uri = $uri; - $this->project = $project; - $this->client = $client; - $this->parser = $parser; - } - - /** - * Returns all symbols in this document. - * - * @return SymbolInformation[] - */ - public function getSymbols() - { - return $this->symbols; - } - - /** - * Returns symbols in this document filtered by query string. - * - * @param string $query The search query - * @return SymbolInformation[] - */ - public function findSymbols(string $query) - { - return array_filter($this->symbols, function($symbol) use(&$query) { - return stripos($symbol->name, $query) !== false; - }); - } - - /** - * Updates the content on this document. - * - * @param string $content - */ - public function updateContent(string $content) - { - $this->content = $content; - $this->parse(); - } - - /** - * Re-parses a source file, updates symbols, reports parsing errors - * that may have occured as diagnostics and returns parsed nodes. - * - * @return \PhpParser\Node[] - */ - public function parse() - { - $stmts = null; - $errors = []; - try { - $stmts = $this->parser->parse($this->content); - } - catch(\PhpParser\Error $e) { - // Lexer can throw errors. e.g for unterminated comments - // unfortunately we don't get a location back - $errors[] = $e; - } - - $errors = array_merge($this->parser->getErrors(), $errors); - - $diagnostics = []; - foreach ($errors as $error) { - $diagnostic = new Diagnostic(); - $diagnostic->range = new Range( - new Position($error->getStartLine() - 1, $error->hasColumnInfo() ? $error->getStartColumn($this->content) - 1 : 0), - new Position($error->getEndLine() - 1, $error->hasColumnInfo() ? $error->getEndColumn($this->content) : 0) - ); - $diagnostic->severity = DiagnosticSeverity::ERROR; - $diagnostic->source = 'php'; - // Do not include "on line ..." in the error message - $diagnostic->message = $error->getRawMessage(); - $diagnostics[] = $diagnostic; - } - $this->client->textDocument->publishDiagnostics($this->uri, $diagnostics); - - // $stmts can be null in case of a fatal parsing error - if ($stmts) { - $traverser = new NodeTraverser; - $finder = new SymbolFinder($this->uri); - $traverser->addVisitor(new NameResolver); - $traverser->addVisitor(new ColumnCalculator($this->content)); - $traverser->addVisitor($finder); - $traverser->traverse($stmts); - - $this->symbols = $finder->symbols; - } - - return $stmts; - } - - /** - * Returns this document as formatted text. - * - * @return string - */ - public function getFormattedText() - { - $stmts = $this->parse(); - if (empty($stmts)) { - return []; - } - $prettyPrinter = new PrettyPrinter(); - $edit = new TextEdit(); - $edit->range = new Range(new Position(0, 0), new Position(PHP_INT_MAX, PHP_INT_MAX)); - $edit->newText = $prettyPrinter->prettyPrintFile($stmts); - return [$edit]; - } - - /** - * Returns this document's text content. - * - * @return string - */ - public function getContent() - { - return $this->content; - } -} +uri = $uri; + $this->project = $project; + $this->client = $client; + $this->parser = $parser; + } + + /** + * Returns all symbols in this document. + * + * @return SymbolInformation[] + */ + public function getSymbols() + { + return $this->symbols; + } + + /** + * Returns symbols in this document filtered by query string. + * + * @param string $query The search query + * @return SymbolInformation[] + */ + public function findSymbols(string $query) + { + return array_filter($this->symbols, function($symbol) use(&$query) { + return stripos($symbol->name, $query) !== false; + }); + } + + /** + * Updates the content on this document. + * + * @param string $content + */ + public function updateContent(string $content) + { + $this->content = $content; + $this->parse(); + } + + /** + * Re-parses a source file, updates symbols, reports parsing errors + * that may have occured as diagnostics and returns parsed nodes. + * + * @return \PhpParser\Node[] + */ + public function parse() + { + $stmts = null; + $errors = []; + try { + $stmts = $this->parser->parse($this->content); + } catch(\PhpParser\Error $e) { + // Lexer can throw errors. e.g for unterminated comments + // unfortunately we don't get a location back + $errors[] = $e; + } + + $errors = array_merge($this->parser->getErrors(), $errors); + + $diagnostics = []; + foreach ($errors as $error) { + $diagnostic = new Diagnostic(); + $diagnostic->range = new Range( + new Position($error->getStartLine() - 1, $error->hasColumnInfo() ? $error->getStartColumn($this->content) - 1 : 0), + new Position($error->getEndLine() - 1, $error->hasColumnInfo() ? $error->getEndColumn($this->content) : 0) + ); + $diagnostic->severity = DiagnosticSeverity::ERROR; + $diagnostic->source = 'php'; + // Do not include "on line ..." in the error message + $diagnostic->message = $error->getRawMessage(); + $diagnostics[] = $diagnostic; + } + $this->client->textDocument->publishDiagnostics($this->uri, $diagnostics); + + // $stmts can be null in case of a fatal parsing error + if ($stmts) { + $traverser = new NodeTraverser; + $finder = new SymbolFinder($this->uri); + $traverser->addVisitor(new NameResolver); + $traverser->addVisitor(new ColumnCalculator($this->content)); + $traverser->addVisitor($finder); + $traverser->traverse($stmts); + + $this->symbols = $finder->symbols; + } + + return $stmts; + } + + /** + * Returns this document as formatted text. + * + * @return string + */ + public function getFormattedText() + { + $stmts = $this->parse(); + if (empty($stmts)) { + return []; + } + $prettyPrinter = new PrettyPrinter(); + $edit = new TextEdit(); + $edit->range = new Range(new Position(0, 0), new Position(PHP_INT_MAX, PHP_INT_MAX)); + $edit->newText = $prettyPrinter->prettyPrintFile($stmts); + return [$edit]; + } + + /** + * Returns this document's text content. + * + * @return string + */ + public function getContent() + { + return $this->content; + } +} diff --git a/src/Project.php b/src/Project.php index 27e84ea..d62a8cf 100644 --- a/src/Project.php +++ b/src/Project.php @@ -1,70 +1,70 @@ - PhpDocument] - * that maps URIs to loaded PhpDocuments - * - * @var array - */ - private $documents; - - /** - * Instance of the PHP parser - * - * @var ParserAbstract - */ - private $parser; - - /** - * Reference to the language server client interface - * - * @var LanguageClient - */ - private $client; - - public function __construct(LanguageClient $client) - { - $this->client = $client; - - $lexer = new Lexer(['usedAttributes' => ['comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos']]); - $this->parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7, $lexer, ['throwOnError' => false]); - } - - /** - * Returns the document indicated by uri. Instantiates a new document if none exists. - * - * @param string $uri - * @return LanguageServer\PhpDocument - */ - public function getDocument(string $uri) - { - $uri = urldecode($uri); - if (!isset($this->documents[$uri])){ - $this->documents[$uri] = new PhpDocument($uri, $this, $this->client, $this->parser); - } - return $this->documents[$uri]; - } - - /** - * Finds symbols in all documents, filtered by query parameter. - * - * @param string $query - * @return SymbolInformation[] - */ - public function findSymbols(string $query) - { - $queryResult = []; - foreach($this->documents as $uri => $document) { - $queryResult = array_merge($queryResult, $document->findSymbols($query)); - } - return $queryResult; - } -} \ No newline at end of file + PhpDocument] + * that maps URIs to loaded PhpDocuments + * + * @var array + */ + private $documents; + + /** + * Instance of the PHP parser + * + * @var ParserAbstract + */ + private $parser; + + /** + * Reference to the language server client interface + * + * @var LanguageClient + */ + private $client; + + public function __construct(LanguageClient $client) + { + $this->client = $client; + + $lexer = new Lexer(['usedAttributes' => ['comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos']]); + $this->parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7, $lexer, ['throwOnError' => false]); + } + + /** + * Returns the document indicated by uri. Instantiates a new document if none exists. + * + * @param string $uri + * @return LanguageServer\PhpDocument + */ + public function getDocument(string $uri) + { + $uri = urldecode($uri); + if (!isset($this->documents[$uri])) { + $this->documents[$uri] = new PhpDocument($uri, $this, $this->client, $this->parser); + } + return $this->documents[$uri]; + } + + /** + * Finds symbols in all documents, filtered by query parameter. + * + * @param string $query + * @return SymbolInformation[] + */ + public function findSymbols(string $query) + { + $queryResult = []; + foreach($this->documents as $uri => $document) { + $queryResult = array_merge($queryResult, $document->findSymbols($query)); + } + return $queryResult; + } +} diff --git a/src/Server/TextDocument.php b/src/Server/TextDocument.php index 92a9cef..38c8ea3 100644 --- a/src/Server/TextDocument.php +++ b/src/Server/TextDocument.php @@ -71,7 +71,7 @@ class TextDocument { $this->project->getDocument($textDocument->uri)->updateContent($contentChanges[0]->text); } - + /** * The document formatting request is sent from the server to the client to format a whole document. @@ -84,5 +84,4 @@ class TextDocument { return $this->project->getDocument($textDocument->uri)->getFormattedText(); } - } diff --git a/src/Server/Workspace.php b/src/Server/Workspace.php index ef3e50b..9f193f6 100644 --- a/src/Server/Workspace.php +++ b/src/Server/Workspace.php @@ -1,58 +1,58 @@ -project = $project; - $this->client = $client; - } - - /** - * The workspace symbol request is sent from the client to the server to list project-wide symbols matching the query string. - * document. - * - * @param string $query - * @return SymbolInformation[] - */ - public function symbol(string $query): array - { - return $this->project->findSymbols($query); - } -} +project = $project; + $this->client = $client; + } + + /** + * The workspace symbol request is sent from the client to the server to list project-wide symbols matching the query string. + * document. + * + * @param string $query + * @return SymbolInformation[] + */ + public function symbol(string $query): array + { + return $this->project->findSymbols($query); + } +} diff --git a/src/SymbolFinder.php b/src/SymbolFinder.php index e185d3e..73cb741 100644 --- a/src/SymbolFinder.php +++ b/src/SymbolFinder.php @@ -39,12 +39,12 @@ class SymbolFinder extends NodeVisitorAbstract /** * @var array */ - private $nameStack = array(); + private $nameStack = []; /** * @var array */ - private $nodeStack = array(); + private $nodeStack = []; /** * @var int @@ -58,26 +58,21 @@ class SymbolFinder extends NodeVisitorAbstract public function enterNode(Node $node) { - array_push($this->nodeStack, $node); + $this->nodeStack[] = $node; $containerName = end($this->nameStack); // If we enter a named node, push its name onto name stack. // Else push the current name onto stack. - if (!empty($node->name) && (is_string($node->name) || method_exists($node->name, '__toString')) && !empty((string)$node->name)) { + if (!empty($node->name) && !empty((string)$node->name)) { if (empty($containerName)) { - array_push($this->nameStack, (string)$node->name); + $this->nameStack[] = (string)$node->name; + } else if ($node instanceof Node\Stmt\ClassMethod) { + $this->nameStack[] = $containerName . '::' . (string)$node->name; + } else { + $this->nameStack[] = $containerName . '\\' . (string)$node->name; } - else { - if ($node instanceof Node\Stmt\ClassMethod) { - array_push($this->nameStack, $containerName . '::' . (string)$node->name); - } - else { - array_push($this->nameStack, $containerName . '\\' . (string)$node->name); - } - } - } - else { - array_push($this->nameStack, $containerName); + } else { + $this->nameStack[] = $containerName; } $class = get_class($node); diff --git a/src/utils.php b/src/utils.php index 2d62dc0..8d45cab 100644 --- a/src/utils.php +++ b/src/utils.php @@ -3,7 +3,7 @@ namespace LanguageServer; /** - * Recursively Searches files with matching filename, starting at $path. + * Recursively Searches files with matching filename, starting at $path. * * @param string $path * @param string $pattern @@ -13,15 +13,15 @@ function findFilesRecursive(string $path, string $pattern): array { $dir = new \RecursiveDirectoryIterator($path); $ite = new \RecursiveIteratorIterator($dir); $files = new \RegexIterator($ite, $pattern, \RegexIterator::GET_MATCH); - $fileList = array(); - foreach($files as $file) { + $fileList = []; + foreach ($files as $file) { $fileList = array_merge($fileList, $file); } return $fileList; } /** - * Transforms an absolute file path into a URI as used by the language server protocol. + * Transforms an absolute file path into a URI as used by the language server protocol. * * @param string $filepath * @return string @@ -29,5 +29,5 @@ function findFilesRecursive(string $path, string $pattern): array { function pathToUri(string $filepath): string { $filepath = trim(str_replace('\\', '/', $filepath), '/'); $filepath = implode('/', array_map('urlencode', explode('/', $filepath))); - return 'file:///'.$filepath; -} \ No newline at end of file + return 'file:///' . $filepath; +} diff --git a/tests/Utils/FileUriTest.php b/tests/Utils/FileUriTest.php index dcd3242..802f78b 100644 --- a/tests/Utils/FileUriTest.php +++ b/tests/Utils/FileUriTest.php @@ -21,8 +21,8 @@ class FileUriTest extends TestCase $uri = \LanguageServer\pathToUri('/usr/local/bin'); $this->assertEquals('file:///usr/local/bin', $uri); - $uri = \LanguageServer\pathToUri('a/b/c/'); - $this->assertEquals('file:///a/b/c', $uri); + $uri = \LanguageServer\pathToUri('a/b/c/test.txt'); + $this->assertEquals('file:///a/b/c/test.txt', $uri); $uri = \LanguageServer\pathToUri('/d/e/f'); $this->assertEquals('file:///d/e/f', $uri);