diff --git a/src/Formatter.php b/src/Formatter.php index f5c973c..e83b5b0 100644 --- a/src/Formatter.php +++ b/src/Formatter.php @@ -5,14 +5,14 @@ namespace LanguageServer; use LanguageServer\Protocol\{TextEdit, Range, Position, ErrorCode}; use AdvancedJsonRpc\ResponseError; +use PHP_CodeSniffer; class Formatter { - /** * - * @param string $content - * @param string $uri + * @param string $content + * @param string $uri * * @return \LanguageServer\Protocol\TextEdit[] * @throws \AdvancedJsonRpc\ResponseError @@ -20,14 +20,14 @@ class Formatter public function format(string $content, string $uri) { $path = uriToPath($uri); - $cs = new \PHP_CodeSniffer(); + $cs = new PHP_CodeSniffer(); $cs->initStandard($this->findConfiguration($path)); $file = $cs->processFile(null, $content); $fixed = $file->fixer->fixFile(); if (!$fixed && $file->getErrorCount() > 0) { throw new ResponseError('Unable to format file', ErrorCode::INTERNAL_ERROR); } - + $new = $file->fixer->getContents(); if ($content === $new) { return []; @@ -42,34 +42,34 @@ class Formatter private function calculateEndPosition(string $content): Position { $lines = explode("\n", $content); - return new Position(sizeof($lines) - 1, strlen(end($lines))); + return new Position(count($lines) - 1, strlen(end($lines))); } - + /** * - * @param string $uri + * @param string $path * @return string[] */ - private function findConfiguration(string $uri) + private function findConfiguration(string $path) { - $currentDir = dirname($uri); + $currentDir = dirname($path); do { $default = $currentDir . DIRECTORY_SEPARATOR . 'phpcs.xml'; - if (is_file($default) === true) { + if (is_file($default)) { return [$default]; } - + $default = $currentDir . DIRECTORY_SEPARATOR . 'phpcs.xml.dist'; - if (is_file($default) === true) { + if (is_file($default)) { return [$default]; } - + $lastDir = $currentDir; $currentDir = dirname($currentDir); } while ($currentDir !== '.' && $currentDir !== $lastDir); - - $standard = \PHP_CodeSniffer::getConfigData('default_standard') ?? 'PSR2'; + + $standard = PHP_CodeSniffer::getConfigData('default_standard') ?? 'PSR2'; return explode(',', $standard); } - + } diff --git a/tests/FormatterTest.php b/tests/FormatterTest.php index c6af09a..6f19bcb 100644 --- a/tests/FormatterTest.php +++ b/tests/FormatterTest.php @@ -12,21 +12,20 @@ class FormatterTest extends TestCase public function testFormat() { $formatter = new Formatter(); - + $input = file_get_contents(__DIR__ . '/../fixtures/format.php'); $output = file_get_contents(__DIR__ . '/../fixtures/format_expected.php'); - + $edits = $formatter->format($input, 'whatever'); - $this->assertTrue($edits[0]->newText === $output); + $this->assertSame($output, $edits[0]->newText); } public function testFormatNoChange() { $formatter = new Formatter(); $expected = file_get_contents(__DIR__ . '/../fixtures/format_expected.php'); - - $edits = $formatter->format($expected, 'whatever'); - $this->assertTrue($edits == []); - } -} \ No newline at end of file + $edits = $formatter->format($expected, 'whatever'); + $this->assertSame([], $edits); + } +} diff --git a/tests/Utils/FileUriTest.php b/tests/Utils/FileUriTest.php index 3a54301..0a077a2 100644 --- a/tests/Utils/FileUriTest.php +++ b/tests/Utils/FileUriTest.php @@ -4,50 +4,51 @@ declare(strict_types = 1); namespace LanguageServer\Tests\Utils; use PHPUnit\Framework\TestCase; +use function LanguageServer\{pathToUri, uriToPath}; class FileUriTest extends TestCase { public function testPathToUri() { - $uri = \LanguageServer\pathToUri('var/log'); + $uri = pathToUri('var/log'); $this->assertEquals('file:///var/log', $uri); - $uri = \LanguageServer\pathToUri('/usr/local/bin'); + $uri = pathToUri('/usr/local/bin'); $this->assertEquals('file:///usr/local/bin', $uri); - $uri = \LanguageServer\pathToUri('a/b/c/test.txt'); + $uri = pathToUri('a/b/c/test.txt'); $this->assertEquals('file:///a/b/c/test.txt', $uri); - $uri = \LanguageServer\pathToUri('/d/e/f'); + $uri = pathToUri('/d/e/f'); $this->assertEquals('file:///d/e/f', $uri); // special chars are escaped - $uri = \LanguageServer\pathToUri('c:/path/to/file/dürüm döner.php'); + $uri = pathToUri('c:/path/to/file/dürüm döner.php'); $this->assertEquals('file:///c%3A/path/to/file/d%C3%BCr%C3%BCm+d%C3%B6ner.php', $uri); //backslashes are transformed - $uri = \LanguageServer\pathToUri('c:\\foo\\bar.baz'); + $uri = pathToUri('c:\\foo\\bar.baz'); $this->assertEquals('file:///c%3A/foo/bar.baz', $uri); } public function testUriToPath() { $uri = 'file:///var/log'; - $this->assertEquals('/var/log', \LanguageServer\uriToPath($uri)); + $this->assertEquals('/var/log', uriToPath($uri)); $uri = 'file:///usr/local/bin'; - $this->assertEquals('/usr/local/bin', \LanguageServer\uriToPath($uri)); + $this->assertEquals('/usr/local/bin', uriToPath($uri)); $uri = 'file:///a/b/c/test.txt'; - $this->assertEquals('/a/b/c/test.txt', \LanguageServer\uriToPath($uri)); + $this->assertEquals('/a/b/c/test.txt', uriToPath($uri)); $uri = 'file:///d/e/f'; - $this->assertEquals('/d/e/f', \LanguageServer\uriToPath($uri)); + $this->assertEquals('/d/e/f', uriToPath($uri)); $uri = 'file:///c:/path/to/file/d%C3%BCr%C3%BCm+d%C3%B6ner.php'; - $this->assertEquals('c:\\path\\to\\file\\dürüm döner.php', \LanguageServer\uriToPath($uri)); + $this->assertEquals('c:\\path\\to\\file\\dürüm döner.php', uriToPath($uri)); $uri = 'file:///c:/foo/bar.baz'; - $this->assertEquals('c:\\foo\\bar.baz', \LanguageServer\uriToPath($uri)); + $this->assertEquals('c:\\foo\\bar.baz', uriToPath($uri)); } }