Permalink
Browse files

used PHP 7.1 type hints

  • Loading branch information...
dg committed Mar 26, 2018
1 parent 92f319b commit c8d0ccb3a73eafefdec142705aa281ccbaaf67c4
Showing with 32 additions and 54 deletions.
  1. +6 −10 src/Checker.php
  2. +4 −22 src/Result.php
  3. +19 −19 src/Tasks.php
  4. +2 −2 src/bootstrap.php
  5. +1 −1 tests/bootstrap.php
@@ -37,7 +37,7 @@ class Checker
private $relativePath;
public function run($path)
public function run(string $path): bool
{
$this->useColors = PHP_SAPI === 'cli' && ((function_exists('posix_isatty') && posix_isatty(STDOUT))
|| getenv('ConEmuANSI') === 'ON' || getenv('ANSICON') !== false || getenv('term') === 'xterm-256color');
@@ -71,17 +71,13 @@ public function run($path)
}
public function addTask(callable $task, $pattern = null)
public function addTask(callable $task, string $pattern = null): void
{
$this->tasks[] = [$task, $pattern];
}
/**
* @param string
* @return bool
*/
private function processFile($file)
private function processFile(string $file): bool
{
$error = false;
$origContents = $lastContents = file_get_contents($file);
@@ -123,7 +119,7 @@ private function processFile($file)
}
private function matchFileName($pattern, $name)
private function matchFileName(string $pattern, string $name): bool
{
$neg = substr($pattern, 0, 1) === '!';
foreach (explode(',', ltrim($pattern, '!')) as $part) {
@@ -135,7 +131,7 @@ private function matchFileName($pattern, $name)
}
private function write($type, $message, $line, $color)
private function write(string $type, string $message, ?int $line, string $color): void
{
$base = basename($this->relativePath);
echo $this->color($color, str_pad("[$type]", 10)),
@@ -145,7 +141,7 @@ private function write($type, $message, $line, $color)
}
private function color($color = null, $s = null)
private function color(string $color = null, string $s = null): string
{
static $colors = [
'black' => '0;30', 'gray' => '1;30', 'silver' => '0;37', 'white' => '1;37',
@@ -13,43 +13,25 @@ class Result
private $messages = [];
/**
* @param string
* @param int
* @return void
*/
public function fix($message, $line = null)
public function fix(string $message, int $line = null): void
{
$this->messages[] = [self::FIX, $message, $line];
}
/**
* @param string
* @param int
* @return void
*/
public function warning($message, $line = null)
public function warning(string $message, int $line = null): void
{
$this->messages[] = [self::WARNING, $message, $line];
}
/**
* @param string
* @param int
* @return void
*/
public function error($message, $line = null)
public function error(string $message, int $line = null): void
{
$this->messages[] = [self::ERROR, $message, $line];
}
/**
* @return array
*/
public function getMessages()
public function getMessages(): array
{
return $this->messages;
}
@@ -9,15 +9,15 @@
class Tasks
{
public static function controlCharactersChecker($contents, Result $result)
public static function controlCharactersChecker(string $contents, Result $result): void
{
if (!Strings::match($contents, '#^[^\x00-\x08\x0B\x0C\x0E-\x1F]*+$#')) {
$result->error('Contains control characters');
}
}
public static function bomFixer(&$contents, Result $result)
public static function bomFixer(string &$contents, Result $result): void
{
if (substr($contents, 0, 3) === "\xEF\xBB\xBF") {
$result->fix('contains BOM');
@@ -26,15 +26,15 @@ public static function bomFixer(&$contents, Result $result)
}
public static function utf8Checker($contents, Result $result)
public static function utf8Checker(string $contents, Result $result): void
{
if (!Strings::checkEncoding($contents)) {
$result->error('Is not valid UTF-8 file');
}
}
public static function invalidPhpDocChecker($contents, Result $result)
public static function invalidPhpDocChecker(string $contents, Result $result): void
{
foreach (token_get_all($contents) as $token) {
if ($token[0] === T_COMMENT && Strings::match($token[1], '#/\*(?!\*).*(?<!\w)@[a-z]#isA')) {
@@ -47,7 +47,7 @@ public static function invalidPhpDocChecker($contents, Result $result)
}
public static function shortArraySyntaxFixer(&$contents, Result $result)
public static function shortArraySyntaxFixer(string &$contents, Result $result): void
{
$out = '';
$brackets = [];
@@ -79,7 +79,7 @@ public static function shortArraySyntaxFixer(&$contents, Result $result)
}
public static function strictTypesDeclarationChecker($contents, Result $result)
public static function strictTypesDeclarationChecker(string $contents, Result $result): void
{
$declarations = '';
$tokens = token_get_all($contents);
@@ -98,7 +98,7 @@ public static function strictTypesDeclarationChecker($contents, Result $result)
}
public static function invalidDoubleQuotedStringChecker($contents, Result $result)
public static function invalidDoubleQuotedStringChecker(string $contents, Result $result): void
{
$prev = null;
foreach (token_get_all($contents) as $token) {
@@ -115,7 +115,7 @@ public static function invalidDoubleQuotedStringChecker($contents, Result $resul
}
public static function newlineNormalizer(&$contents, Result $result)
public static function newlineNormalizer(string &$contents, Result $result): void
{
$new = str_replace("\n", PHP_EOL, str_replace(["\r\n", "\r"], "\n", $contents));
if ($new !== $contents) {
@@ -125,7 +125,7 @@ public static function newlineNormalizer(&$contents, Result $result)
}
public static function trailingPhpTagRemover(&$contents, Result $result)
public static function trailingPhpTagRemover(string &$contents, Result $result): void
{
$tmp = rtrim($contents);
if (substr($tmp, -2) === '?>') {
@@ -135,7 +135,7 @@ public static function trailingPhpTagRemover(&$contents, Result $result)
}
public static function phpSyntaxChecker($contents, Result $result)
public static function phpSyntaxChecker(string $contents, Result $result): void
{
$php = defined('PHP_BINARY') ? PHP_BINARY : 'php';
$stdin = tmpfile();
@@ -159,7 +159,7 @@ public static function phpSyntaxChecker($contents, Result $result)
}
public static function latteSyntaxChecker($contents, Result $result)
public static function latteSyntaxChecker(string $contents, Result $result): void
{
$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
@@ -181,7 +181,7 @@ public static function latteSyntaxChecker($contents, Result $result)
}
public static function neonSyntaxChecker($contents, Result $result)
public static function neonSyntaxChecker(string $contents, Result $result): void
{
try {
Nette\Neon\Neon::decode($contents);
@@ -191,7 +191,7 @@ public static function neonSyntaxChecker($contents, Result $result)
}
public static function jsonSyntaxChecker($contents, Result $result)
public static function jsonSyntaxChecker(string $contents, Result $result): void
{
try {
Nette\Utils\Json::decode($contents);
@@ -204,15 +204,15 @@ public static function jsonSyntaxChecker($contents, Result $result)
}
public static function yamlIndentationChecker($contents, Result $result)
public static function yamlIndentationChecker(string $contents, Result $result): void
{
if (preg_match('#^\t#m', $contents, $m, PREG_OFFSET_CAPTURE)) {
$result->error('Used tabs to indent instead of spaces', self::offsetToLine($contents, $m[0][1]));
}
}
public static function trailingWhiteSpaceFixer(&$contents, Result $result)
public static function trailingWhiteSpaceFixer(string &$contents, Result $result): void
{
$new = Strings::replace($contents, '#[\t ]+(\r?\n)#', '$1'); // right trim
$eol = preg_match('#\r?\n#', $new, $m) ? $m[0] : PHP_EOL;
@@ -228,7 +228,7 @@ public static function trailingWhiteSpaceFixer(&$contents, Result $result)
}
public static function tabIndentationChecker($contents, Result $result, $origContents = null)
public static function tabIndentationChecker(string $contents, Result $result, string $origContents = null): void
{
$origContents = $origContents ?: $contents;
$offset = 0;
@@ -245,7 +245,7 @@ public static function tabIndentationChecker($contents, Result $result, $origCon
}
public static function tabIndentationPhpChecker($contents, Result $result)
public static function tabIndentationPhpChecker(string $contents, Result $result): void
{
$s = ''; // remove strings from code
foreach (token_get_all($contents) as $token) {
@@ -258,15 +258,15 @@ public static function tabIndentationPhpChecker($contents, Result $result)
}
public static function unexpectedTabsChecker($contents, Result $result)
public static function unexpectedTabsChecker(string $contents, Result $result): void
{
if (($pos = strpos($contents, "\t")) !== false) {
$result->error('Found unexpected tabulator', self::offsetToLine($contents, $pos));
}
}
private static function offsetToLine($s, $offset)
private static function offsetToLine(string $s, int $offset): int
{
return $offset ? substr_count($s, "\n", 0, $offset) + 1 : 1;
}
@@ -18,12 +18,12 @@
exit(1);
}
set_exception_handler(function ($e) {
set_exception_handler(function (\Throwable $e) {
echo "Error: {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}\n";
die(2);
});
set_error_handler(function ($severity, $message, $file, $line) {
set_error_handler(function (int $severity, string $message, string $file, int $line) {
if (($severity & error_reporting()) === $severity) {
throw new \ErrorException($message, 0, $severity, $file, $line);
}
@@ -9,7 +9,7 @@
Tester\Environment::setup();
function test(\Closure $function)
function test(\Closure $function): void
{
$function();
}

0 comments on commit c8d0ccb

Please sign in to comment.