1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Increase limits of literal strings to 50 characters

This commit is contained in:
Brown 2018-11-27 17:45:07 -05:00
parent 2e7a75ba0a
commit f5e788ec1f
7 changed files with 164 additions and 22 deletions

View File

@ -115,7 +115,7 @@ class ExpressionAnalyzer
} elseif ($stmt instanceof PhpParser\Node\Expr\ConstFetch) {
ConstFetchAnalyzer::analyze($statements_analyzer, $stmt, $context);
} elseif ($stmt instanceof PhpParser\Node\Scalar\String_) {
$stmt->inferredType = Type::getString(strlen($stmt->value) < 30 ? $stmt->value : null);
$stmt->inferredType = Type::getString(strlen($stmt->value) < 50 ? $stmt->value : null);
} elseif ($stmt instanceof PhpParser\Node\Scalar\EncapsedStringPart) {
// do nothing
} elseif ($stmt instanceof PhpParser\Node\Scalar\MagicConst) {

View File

@ -370,12 +370,24 @@ class StatementsProvider
$used_cached_statements = true;
$stmts = $existing_statements;
} else {
/** @var array<int, \PhpParser\Node\Stmt> */
$stmts = self::$parser->parse($file_contents, $error_handler) ?: [];
try {
/** @var array<int, \PhpParser\Node\Stmt> */
$stmts = self::$parser->parse($file_contents, $error_handler) ?: [];
} catch (\Throwable $t) {
$stmts = [];
// hope this got caught below
}
}
} else {
/** @var array<int, \PhpParser\Node\Stmt> */
$stmts = self::$parser->parse($file_contents, $error_handler) ?: [];
try {
/** @var array<int, \PhpParser\Node\Stmt> */
$stmts = self::$parser->parse($file_contents, $error_handler) ?: [];
} catch (\Throwable $t) {
$stmts = [];
// hope this got caught below
}
}
if ($error_handler->hasErrors() && $file_path) {

View File

@ -58,7 +58,7 @@ trait GenericTrait
? parent::toNamespacedString($namespace, $aliased_classes, $this_class, $use_phpdoc_format)
: $this->value;
if ($base_value === 'non-empty-string') {
if ($base_value === 'non-empty-array') {
$base_value = 'array';
}

View File

@ -32,6 +32,25 @@ class TLiteralFloat extends TFloat
return 'float(' . $this->value . ')';
}
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param string|null $this_class
* @param int $php_major_version
* @param int $php_minor_version
*
* @return null|string
*/
public function toPhpString(
$namespace,
array $aliased_classes,
$this_class,
$php_major_version,
$php_minor_version
) {
return $php_major_version >= 7 ? 'float' : null;
}
/**
* @param string|null $namespace
* @param array<string> $aliased_classes

View File

@ -32,6 +32,25 @@ class TLiteralInt extends TInt
return 'int(' . $this->value . ')';
}
/**
* @param string|null $namespace
* @param array<string> $aliased_classes
* @param string|null $this_class
* @param int $php_major_version
* @param int $php_minor_version
*
* @return null|string
*/
public function toPhpString(
$namespace,
array $aliased_classes,
$this_class,
$php_major_version,
$php_minor_version
) {
return $php_major_version >= 7 ? 'int' : null;
}
/**
* @param string|null $namespace
* @param array<string> $aliased_classes

View File

@ -885,6 +885,96 @@ class TemporaryUpdateTest extends \Psalm\Tests\TestCase
],
'error_positions' => [[], [238], [], [238], []],
],
'traitMethodRenameSameFile2' => [
[
[
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
use T;
public function foo() : void {
echo $this->bar();
}
}
trait T {
public function bar() : string {
return "hello";
}
}',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
use T;
public function foo() : void {
echo $this->bar();
}
}
trait T {
public function bat() : string {
return "hello";
}
}',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
use T;
public function foo() : void {
echo $this->bat();
}
}
trait T {
public function bat() : string {
return "hello";
}
}',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
use T;
public function foo() : void {
echo $this->bat();
}
}
trait T {
public function bar() : string {
return "hello";
}
}',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
use T;
public function foo() : void {
echo $this->bar();
}
}
trait T {
public function bar() : string {
return "hello";
}
}',
],
],
'error_positions' => [[], [238], [], [238], []],
],
'traitMethodRenameSameFile' => [
[
[
@ -975,6 +1065,22 @@ class TemporaryUpdateTest extends \Psalm\Tests\TestCase
],
'error_positions' => [[], [238], [], [238], []],
],
'parseErrorAfterQuoteChange' => [
[
[
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
$a = ["a thing"];',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
$a = ["a "thing"];',
],
],
'error_possitions' => [[], [44]]
]
];
}
}

View File

@ -701,7 +701,7 @@ class UnusedVariableTest extends TestCase
echo $a;',
],
'loopAssignmentAfterReferenceWithContinueInSwitch' => [
'loopAssignmentAfterReferenceWithContinueInSwitch2' => [
'<?php
$a = 0;
while (rand(0, 1)) {
@ -1275,7 +1275,7 @@ class UnusedVariableTest extends TestCase
}',
'error_message' => 'UnusedVariable',
],
'loopSetIfNullWithBreakWithoutReference' => [
'loopSetIfNullWithBreakWithoutReference2' => [
'<?php
$a = null;
@ -1293,20 +1293,6 @@ class UnusedVariableTest extends TestCase
'<?php
$a = null;
while (rand(0, 1)) {
if ($a === null) {
$a = 4;
continue;
}
$a = 5;
}',
'error_message' => 'UnusedVariable',
],
'loopSetIfNullWithContinueWithoutReference' => [
'<?php
$a = null;
while (rand(0, 1)) {
if (rand(0, 1)) {
$a = 4;