From 94336941bd585fb90df749f8b27183236603bff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Steitz?= Date: Sat, 18 Feb 2017 19:30:34 +0100 Subject: [PATCH] Let JsonMapper intialize the options To sanitize the file type option, we provide a setter method for the property that will be called by the JsonMapper. --- src/Indexer.php | 2 +- src/LanguageServer.php | 7 +++---- src/Options.php | 38 ++++++-------------------------------- 3 files changed, 10 insertions(+), 37 deletions(-) diff --git a/src/Indexer.php b/src/Indexer.php index c341dd2..4cd85d3 100644 --- a/src/Indexer.php +++ b/src/Indexer.php @@ -112,7 +112,7 @@ class Indexer public function index(): Promise { return coroutine(function () { - $fileTypes = implode(',', $this->options->getFileTypes()); + $fileTypes = implode(',', $this->options->fileTypes); $pattern = Path::makeAbsolute('**/*{' . $fileTypes . '}', $this->rootPath); $uris = yield $this->filesFinder->find($pattern); diff --git a/src/LanguageServer.php b/src/LanguageServer.php index 12ee173..7e5074b 100644 --- a/src/LanguageServer.php +++ b/src/LanguageServer.php @@ -167,10 +167,10 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher * @param ClientCapabilities $capabilities The capabilities provided by the client (editor) * @param string|null $rootPath The rootPath of the workspace. Is null if no folder is open. * @param int|null $processId The process Id of the parent process that started the server. Is null if the process has not been started by another process. If the parent process is not alive then the server should exit (see exit notification) its process. - * @param mixed $initializationOptions The options send from client to initialize the server + * @param Options $initializationOptions The options send from client to initialize the server * @return Promise */ - public function initialize(ClientCapabilities $capabilities, string $rootPath = null, int $processId = null, $initializationOptions = null): Promise + public function initialize(ClientCapabilities $capabilities, string $rootPath = null, int $processId = null, Options $initializationOptions = null): Promise { return coroutine(function () use ($capabilities, $rootPath, $processId, $initializationOptions) { @@ -191,7 +191,6 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher $this->projectIndex = new ProjectIndex($sourceIndex, $dependenciesIndex, $this->composerJson); $stubsIndex = StubsIndex::read(); $this->globalIndex = new GlobalIndex($stubsIndex, $this->projectIndex); - $options = new Options($initializationOptions); // The DefinitionResolver should look in stubs, the project source and dependencies $this->definitionResolver = new DefinitionResolver($this->globalIndex); @@ -238,7 +237,7 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher $this->documentLoader, $this->composerLock, $this->composerJson, - $options + $initializationOptions ); $indexer->index()->otherwise('\\LanguageServer\\crash'); } diff --git a/src/Options.php b/src/Options.php index d7e7884..99f1aab 100644 --- a/src/Options.php +++ b/src/Options.php @@ -7,51 +7,25 @@ class Options /** * Filetypes the indexer should process * - * @var array + * @var string[] */ - private $fileTypes = [".php"]; + public $fileTypes = ['.php']; /** - * @param \Traversable|\stdClass|array|null $options - */ - public function __construct($options = null) - { - // Do nothing when the $options parameter is not an object - if (!is_object($options) && !is_array($options) && (!$options instanceof \Traversable)) { - return; - } - - foreach ($options as $option => $value) { - $method = 'set' . ucfirst($option); - - call_user_func([$this, $method], $value); - } - } - - /** - * Validate and set options for file types + * Validate/Filter input and set options for file types * * @param array $fileTypes List of file types */ public function setFileTypes(array $fileTypes) { $fileTypes = filter_var_array($fileTypes, FILTER_SANITIZE_STRING); - $fileTypes = filter_var($fileTypes, FILTER_CALLBACK, ['options' => [$this, 'filterFileTypes']]); - $fileTypes = array_filter($fileTypes); + $fileTypes = filter_var($fileTypes, FILTER_CALLBACK, ['options' => [$this, 'filterFileTypes']]); // validate file type format + $fileTypes = array_filter($fileTypes, 'strlen'); // filter empty items + $fileTypes = array_values($fileTypes); //rebase indexes $this->fileTypes = !empty($fileTypes) ? $fileTypes : $this->fileTypes; } - /** - * Get list of registered file types - * - * @return array - */ - public function getFileTypes(): array - { - return $this->fileTypes; - } - /** * Filter valid file type *