1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fixing remaining PSR-2 violations.

This commit is contained in:
Jon Ursenbach 2016-11-03 20:51:56 -04:00
parent 51ca4f4384
commit 8a6e8e9940
6 changed files with 135 additions and 47 deletions

View File

@ -27,8 +27,7 @@ class TemplateChecker extends Psalm\Checker\FileChecker
$check_class_statements = true,
Context $file_context = null,
$cache = true
)
{
) {
$stmts = $this->getStatements();
if (empty($stmts)) {
@ -40,7 +39,6 @@ class TemplateChecker extends Psalm\Checker\FileChecker
$this_params = null;
if (($first_stmt instanceof PhpParser\Node\Stmt\Nop) && ($doc_comment = $first_stmt->getDocComment())) {
$comment_block = CommentChecker::parseDocComment(trim($doc_comment->getText()));
if (isset($comment_block['specials']['variablesfrom'])) {

View File

@ -300,7 +300,8 @@ abstract class ClassLikeChecker implements StatementsSource
foreach ($extra_interfaces as $extra_interface_name) {
FileChecker::addFileInheritanceToClass($long_file_name, $extra_interface_name);
self::$class_implements[$this->absolute_class][strtolower($extra_interface_name)] = $extra_interface_name;
self::$class_implements[$this->absolute_class][strtolower($extra_interface_name)] =
$extra_interface_name;
}
}
@ -440,17 +441,33 @@ abstract class ClassLikeChecker implements StatementsSource
self::$public_class_properties[$this->absolute_class] = self::$public_class_properties[$parent_class];
self::$protected_class_properties[$this->absolute_class] = self::$protected_class_properties[$parent_class];
self::$public_static_class_properties[$this->absolute_class] = self::$public_static_class_properties[$parent_class];
self::$protected_static_class_properties[$this->absolute_class] = self::$protected_static_class_properties[$parent_class];
self::$public_static_class_properties[$this->absolute_class] =
self::$public_static_class_properties[$parent_class];
self::$protected_static_class_properties[$this->absolute_class] =
self::$protected_static_class_properties[$parent_class];
self::$public_class_constants[$this->absolute_class] = array_merge(
self::$public_class_constants[$parent_class],
self::$public_class_constants[$this->absolute_class]
);
return null;
}
protected function visitClassMethod(PhpParser\Node\Stmt\ClassMethod $stmt, Context $class_context, array &$method_checkers, $cache_method_checker)
{
/**
* @param PhpParser\Node\Stmt\ClassMethod $stmt
* @param Context $class_context
* @param array $method_checkers
* @param $cache_method_checker
* @return void
*/
protected function visitClassMethod(
PhpParser\Node\Stmt\ClassMethod $stmt,
Context $class_context,
array &$method_checkers,
$cache_method_checker
) {
$method_id = $this->absolute_class . '::' . strtolower($stmt->name);
if (!isset(self::$method_checkers[$method_id])) {
@ -469,12 +486,22 @@ abstract class ClassLikeChecker implements StatementsSource
$class_context->self . '::' . $this->getMappedMethodName(strtolower($stmt->name)),
$method_id
);
self::$class_methods[$class_context->self][strtolower($stmt->name)] = true;
}
}
protected function visitTraitUse(PhpParser\Node\Stmt\TraitUse $stmt, Context $class_context, array &$trait_checkers)
{
/**
* @param PhpParser\Node\Stmt\TraitUse $stmt
* @param Context $class_context
* @param array $trait_checkers
* @return false|null
*/
protected function visitTraitUse(
PhpParser\Node\Stmt\TraitUse $stmt,
Context $class_context,
array &$trait_checkers
) {
$method_map = [];
foreach ($stmt->adaptations as $adaptation) {
@ -504,7 +531,11 @@ abstract class ClassLikeChecker implements StatementsSource
$reflection_trait = new \ReflectionClass($trait_name);
} catch (\ReflectionException $e) {
if (IssueBuffer::accepts(
new UndefinedTrait('Trait ' . $trait_name . ' has wrong casing', $this->file_name, $trait->getLine()),
new UndefinedTrait(
'Trait ' . $trait_name . ' has wrong casing',
$this->file_name,
$trait->getLine()
),
$this->suppressed_issues
)) {
return false;
@ -520,15 +551,31 @@ abstract class ClassLikeChecker implements StatementsSource
$trait_checker->check(false, $class_context);
FileChecker::addFileInheritanceToClass(Config::getInstance()->getBaseDir() . $this->file_name, $this->parent_class);
FileChecker::addFileInheritanceToClass(
Config::getInstance()->getBaseDir() . $this->file_name,
$this->parent_class
);
$trait_checkers[] = $trait_checker;
}
}
return null;
}
protected function visitPropertyDeclaration(PhpParser\Node\Stmt\Property $stmt, Context $class_context, Config $config, $check_property_types)
{
/**
* @param PhpParser\Node\Stmt\Property $stmt
* @param Context $class_context
* @param Config $config
* @param $check_property_types
* @return void
*/
protected function visitPropertyDeclaration(
PhpParser\Node\Stmt\Property $stmt,
Context $class_context,
Config $config,
$check_property_types
) {
$comment = $stmt->getDocComment();
$type_in_comment = null;
@ -537,7 +584,8 @@ abstract class ClassLikeChecker implements StatementsSource
} elseif (!$comment && $check_property_types) {
if (IssueBuffer::accepts(
new MissingPropertyType(
'Property ' . $this->absolute_class . '::$' . $stmt->props[0]->name . ' does not have a declared type',
'Property ' . $this->absolute_class . '::$' . $stmt->props[0]->name . ' does not have a ' .
'declared type',
$this->file_name,
$stmt->getLine()
),
@ -580,8 +628,17 @@ abstract class ClassLikeChecker implements StatementsSource
}
}
protected function visitClassConstDeclaration(PhpParser\Node\Stmt\ClassConst $stmt, Context $class_context, Config $config)
{
/**
* @param PhpParser\Node\Stmt\ClassConst $stmt
* @param Context $class_context
* @param Config $config
* @return void
*/
protected function visitClassConstDeclaration(
PhpParser\Node\Stmt\ClassConst $stmt,
Context $class_context,
Config $config
) {
$comment = $stmt->getDocComment();
$type_in_comment = null;
@ -950,8 +1007,11 @@ abstract class ClassLikeChecker implements StatementsSource
self::$public_class_properties[$class_name] = self::$public_class_properties[$parent_class_name];
self::$protected_class_properties[$class_name] = self::$protected_class_properties[$parent_class_name];
self::$public_static_class_properties[$class_name] = self::$public_static_class_properties[$parent_class_name];
self::$protected_static_class_properties[$class_name] = self::$protected_static_class_properties[$parent_class_name];
self::$public_static_class_properties[$class_name] =
self::$public_static_class_properties[$parent_class_name];
self::$protected_static_class_properties[$class_name] =
self::$protected_static_class_properties[$parent_class_name];
}
$class_properties = $reflected_class->getProperties();
@ -1036,7 +1096,9 @@ abstract class ClassLikeChecker implements StatementsSource
ClassChecker::getInterfacesForClass($class_name);
}
$reflection_methods = $reflected_class->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED);
$reflection_methods = $reflected_class->getMethods(
ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
);
self::$class_methods[$class_name] = [];
@ -1053,7 +1115,9 @@ abstract class ClassLikeChecker implements StatementsSource
self::$class_methods[$class_name][strtolower((string)$reflection_method->name)] = true;
}
if (!$reflection_method->isAbstract() && $reflection_method->getDeclaringClass()->getName() === $class_name) {
if (!$reflection_method->isAbstract() &&
$reflection_method->getDeclaringClass()->getName() === $class_name
) {
self::$class_methods[$class_name][strtolower((string)$reflection_method->getName())] = true;
}
}

View File

@ -8,7 +8,8 @@ use Psalm\Type;
class CommentChecker
{
const TYPE_REGEX = '(\\\?[A-Za-z0-9_\<,\>\[\]\-\{\}:|\\\]+[A-Za-z0-9_\<,\>\[\]-\{\}:]|\$[a-zA-Z_0-9_\<,\>\|\[\]-\{\}:]+)';
const TYPE_REGEX =
'(\\\?[A-Za-z0-9_\<,\>\[\]\-\{\}:|\\\]+[A-Za-z0-9_\<,\>\[\]-\{\}:]|\$[a-zA-Z_0-9_\<,\>\|\[\]-\{\}:]+)';
/**
* @param string $comment

View File

@ -406,7 +406,13 @@ class FunctionChecker extends FunctionLikeChecker
}
if (substr($call_map_key, 0, 6) === 'array_') {
$array_return_type = self::getArrayReturnType($call_map_key, $call_args, $file_name, $line_number, $suppressed_issues);
$array_return_type = self::getArrayReturnType(
$call_map_key,
$call_args,
$file_name,
$line_number,
$suppressed_issues
);
if ($array_return_type) {
return $array_return_type;
@ -433,8 +439,13 @@ class FunctionChecker extends FunctionLikeChecker
* @param array $suppressed_issues
* @return Type\Union|null
*/
protected static function getArrayReturnType($call_map_key, $call_args, $file_name, $line_number, array $suppressed_issues)
{
protected static function getArrayReturnType(
$call_map_key,
$call_args,
$file_name,
$line_number,
array $suppressed_issues
) {
if ($call_map_key === 'array_map' || $call_map_key === 'array_filter') {
return self::getArrayMapReturnType($call_map_key, $call_args, $file_name, $line_number, $suppressed_issues);
}
@ -481,7 +492,10 @@ class FunctionChecker extends FunctionLikeChecker
}
$inner_key_types = array_merge(array_values($type_part->type_params[0]->types), $inner_key_types);
$inner_value_types = array_merge(array_values($type_part->type_params[1]->types), $inner_value_types);
$inner_value_types = array_merge(
array_values($type_part->type_params[1]->types),
$inner_value_types
);
}
if ($inner_value_types) {
@ -531,6 +545,8 @@ class FunctionChecker extends FunctionLikeChecker
return clone $first_arg_array->type_params[1];
}
return null;
}
/**
@ -541,8 +557,13 @@ class FunctionChecker extends FunctionLikeChecker
* @param array $suppressed_issues
* @return Type\Union
*/
protected static function getArrayMapReturnType($call_map_key, $call_args, $file_name, $line_number, array $suppressed_issues)
{
protected static function getArrayMapReturnType(
$call_map_key,
$call_args,
$file_name,
$line_number,
array $suppressed_issues
) {
$function_index = $call_map_key === 'array_map' ? 0 : 1;
$array_index = $call_map_key === 'array_map' ? 1 : 0;
@ -560,7 +581,11 @@ class FunctionChecker extends FunctionLikeChecker
if ($function_call_arg->value instanceof PhpParser\Node\Expr\Closure) {
$closure_yield_types = [];
$closure_return_types = \Psalm\EffectsAnalyser::getReturnTypes($function_call_arg->value->stmts, $closure_yield_types, true);
$closure_return_types = EffectsAnalyser::getReturnTypes(
$function_call_arg->value->stmts,
$closure_yield_types,
true
);
if (!$closure_return_types) {
IssueBuffer::accepts(

View File

@ -72,7 +72,7 @@ class ProjectChecker
// strip out deleted files
$file_list = array_diff($file_list, $deleted_files);
self::checkDiffFilesWithConfig($file_list, self::$config, $debug);
self::checkDiffFilesWithConfig(self::$config, $debug, $file_list);
}
IssueBuffer::finish(true);
@ -176,12 +176,12 @@ class ProjectChecker
}
/**
* @param array<string> $file_list
* @param Config $config
* @param bool $debug
* @param array<string> $file_list
* @return void
*/
protected static function checkDiffFilesWithConfig(array $file_list = [], Config $config, $debug)
protected static function checkDiffFilesWithConfig(Config $config, $debug, array $file_list = [])
{
$file_extensions = $config->getFileExtensions();
$filetype_handlers = $config->getFiletypeHandlers();

View File

@ -370,10 +370,10 @@ class TypeChecker
self::processFunctionCall(
$conditional->expr,
$if_types,
true,
$this_class_name,
$namespace,
$aliased_classes
$aliased_classes,
true
);
} elseif ($conditional->expr instanceof PhpParser\Node\Expr\Isset_) {
foreach ($conditional->expr->vars as $isset_var) {
@ -431,10 +431,10 @@ class TypeChecker
self::processFunctionCall(
$conditional->left,
$if_types,
true,
$this_class_name,
$namespace,
$aliased_classes
$aliased_classes,
true
);
} else {
$var_name = ExpressionChecker::getArrayVarId(
@ -449,10 +449,10 @@ class TypeChecker
self::processFunctionCall(
$conditional->right,
$if_types,
true,
$this_class_name,
$namespace,
$aliased_classes
$aliased_classes,
true
);
} else {
$var_name = ExpressionChecker::getArrayVarId(
@ -609,10 +609,10 @@ class TypeChecker
self::processFunctionCall(
$conditional->left,
$if_types,
true,
$this_class_name,
$namespace,
$aliased_classes
$aliased_classes,
true
);
}
} elseif ($true_position === self::ASSIGNMENT_TO_LEFT) {
@ -620,10 +620,10 @@ class TypeChecker
self::processFunctionCall(
$conditional->right,
$if_types,
true,
$this_class_name,
$namespace,
$aliased_classes
$aliased_classes,
true
);
}
} else {
@ -631,7 +631,7 @@ class TypeChecker
}
}
} elseif ($conditional instanceof PhpParser\Node\Expr\FuncCall) {
self::processFunctionCall($conditional, $if_types, false, $this_class_name, $namespace, $aliased_classes);
self::processFunctionCall($conditional, $if_types, $this_class_name, $namespace, $aliased_classes, false);
} elseif ($conditional instanceof PhpParser\Node\Expr\Empty_) {
$var_name = ExpressionChecker::getArrayVarId(
$conditional->expr,
@ -664,19 +664,19 @@ class TypeChecker
/**
* @param PhpParser\Node\Expr\FuncCall $expr
* @param array<string> &$if_types
* @param boolean $negate
* @param string $this_class_name
* @param string $namespace
* @param array<string> $aliased_classes]
* @param array<string> $aliased_classes
* @param boolean $negate
* @return void
*/
protected static function processFunctionCall(
PhpParser\Node\Expr\FuncCall $expr,
array &$if_types,
$negate = false,
$this_class_name,
$namespace,
array $aliased_classes
array $aliased_classes,
$negate = false
) {
$prefix = $negate ? '!' : '';