1
0
Fork 0

Minor code style fixes

pull/35/head
Felix Becker 2016-10-10 10:46:22 +02:00
parent 4c160e1209
commit f1c799159e
3 changed files with 37 additions and 37 deletions

View File

@ -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);
}
}

View File

@ -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 == []);
}
}
$edits = $formatter->format($expected, 'whatever');
$this->assertSame([], $edits);
}
}

View File

@ -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));
}
}