Skip to content

Commit

Permalink
Merge pull request #7230 from orklah/cleanup6
Browse files Browse the repository at this point in the history
code grooming
  • Loading branch information
orklah committed Dec 27, 2021
2 parents 3828db5 + 2bf5a71 commit 926a56a
Show file tree
Hide file tree
Showing 27 changed files with 101 additions and 60 deletions.
Expand Up @@ -34,7 +34,7 @@

foreach ($new_local as $name => $data) {
if (!is_array($data)) {
throw new \UnexpectedValueException('bad data for ' . $name);
throw new UnexpectedValueException('bad data for ' . $name);
}
$return_type = array_shift($data);
echo '\'' . str_replace("'", "\'", $name) . '\' => [\'' . str_replace("'", "\'", $return_type) . '\'';
Expand Down
6 changes: 4 additions & 2 deletions examples/TemplateChecker.php
@@ -1,6 +1,7 @@
<?php
namespace Psalm\Examples\Template;

use InvalidArgumentException;
use PhpParser;
use Psalm;
use Psalm\CodeLocation;
Expand All @@ -12,6 +13,7 @@
use Psalm\Internal\Analyzer\ClassLikeNameOptions;
use Psalm\Internal\Analyzer\MethodAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Provider\NodeDataProvider;
use Psalm\Node\Stmt\VirtualClass;
use Psalm\Node\Stmt\VirtualClassMethod;
use Psalm\Storage\MethodStorage;
Expand Down Expand Up @@ -48,7 +50,7 @@ public function analyze(?Context $file_context = null, ?Context $global_context
$matches = [];

if (!preg_match($first_line_regex, $variables_from, $matches)) {
throw new \InvalidArgumentException('Could not interpret doc comment correctly');
throw new InvalidArgumentException('Could not interpret doc comment correctly');
}

/** @psalm-suppress ArgumentTypeCoercion */
Expand Down Expand Up @@ -167,7 +169,7 @@ protected function checkWithViewClass(Context $context, array $stmts): void

$statements_source = new StatementsAnalyzer(
$view_method_analyzer,
new \Psalm\Internal\Provider\NodeDataProvider()
new NodeDataProvider()
);

$statements_source->analyze($pseudo_method_stmts, $context);
Expand Down
3 changes: 2 additions & 1 deletion examples/TemplateScanner.php
@@ -1,6 +1,7 @@
<?php
namespace Psalm\Examples\Template;

use InvalidArgumentException;
use PhpParser;
use Psalm;
use Psalm\Checker\CommentChecker;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function scan(
$matches = [];

if (!preg_match($first_line_regex, $variables_from, $matches)) {
throw new \InvalidArgumentException('Could not interpret doc comment correctly');
throw new InvalidArgumentException('Could not interpret doc comment correctly');
}

[$fq_class_name] = explode('::', $matches[1]);
Expand Down
6 changes: 4 additions & 2 deletions examples/plugins/ClassUnqualifier.php
Expand Up @@ -2,8 +2,10 @@
namespace Psalm\Example\Plugin;

use Psalm\FileManipulation;
use Psalm\Internal\Type\TypeTokenizer;
use Psalm\Plugin\EventHandler\AfterClassLikeExistenceCheckInterface;
use Psalm\Plugin\EventHandler\Event\AfterClassLikeExistenceCheckEvent;
use function strpos;
use function strtolower;
use function implode;
use function array_map;
Expand All @@ -25,8 +27,8 @@ public static function afterClassLikeExistenceCheck(
return;
}

if (\strpos($candidate_type, '\\' . $fq_class_name) !== false) {
$type_tokens = \Psalm\Internal\Type\TypeTokenizer::tokenize($candidate_type, false);
if (strpos($candidate_type, '\\' . $fq_class_name) !== false) {
$type_tokens = TypeTokenizer::tokenize($candidate_type, false);

foreach ($type_tokens as &$type_token) {
if ($type_token[0] === ('\\' . $fq_class_name)
Expand Down
17 changes: 8 additions & 9 deletions examples/plugins/FunctionCasingChecker.php
@@ -1,13 +1,16 @@
<?php
namespace Psalm\Example\Plugin;

use Exception;
use PhpParser;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use Psalm\Checker;
use Psalm\Checker\StatementsChecker;
use Psalm\CodeLocation;
use Psalm\FileManipulation;
use Psalm\Issue\PluginIssue;
use Psalm\IssueBuffer;
use Psalm\Plugin\EventHandler\AfterFunctionCallAnalysisInterface;
use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterFunctionCallAnalysisEvent;
Expand Down Expand Up @@ -49,7 +52,7 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
}

if ($function_storage->cased_name !== (string)$expr->name) {
if (\Psalm\IssueBuffer::accepts(
if (IssueBuffer::accepts(
new IncorrectFunctionCasing(
'Function is incorrectly cased, expecting ' . $function_storage->cased_name,
new CodeLocation($statements_source, $expr->name)
Expand All @@ -59,15 +62,11 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
// fall through
}
}
} catch (\Exception $e) {
} catch (Exception $e) {
// can throw if storage is missing
}
}

/**
* @param non-empty-string $function_id
* @param FileManipulation[] $file_replacements
*/
public static function afterFunctionCallAnalysis(AfterFunctionCallAnalysisEvent $event): void
{
$expr = $event->getExpr();
Expand All @@ -93,7 +92,7 @@ public static function afterFunctionCallAnalysis(AfterFunctionCallAnalysisEvent
$function_name_parts = explode('\\', $function_storage->cased_name);

if (end($function_name_parts) !== end($expr->name->parts)) {
if (\Psalm\IssueBuffer::accepts(
if (IssueBuffer::accepts(
new IncorrectFunctionCasing(
'Function is incorrectly cased, expecting ' . $function_storage->cased_name,
new CodeLocation($statements_source, $expr->name)
Expand All @@ -103,12 +102,12 @@ public static function afterFunctionCallAnalysis(AfterFunctionCallAnalysisEvent
// fall through
}
}
} catch (\Exception $e) {
} catch (Exception $e) {
// can throw if storage is missing
}
}
}

class IncorrectFunctionCasing extends \Psalm\Issue\PluginIssue
class IncorrectFunctionCasing extends PluginIssue
{
}
6 changes: 4 additions & 2 deletions examples/plugins/PreventFloatAssignmentChecker.php
Expand Up @@ -5,6 +5,8 @@
use Psalm\Checker;
use Psalm\CodeLocation;
use Psalm\FileManipulation;
use Psalm\Issue\PluginIssue;
use Psalm\IssueBuffer;
use Psalm\Plugin\EventHandler\AfterExpressionAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterExpressionAnalysisEvent;

Expand All @@ -25,7 +27,7 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve
&& ($expr_type = $statements_source->getNodeTypeProvider()->getType($expr->expr))
&& $expr_type->hasFloat()
) {
if (\Psalm\IssueBuffer::accepts(
if (IssueBuffer::accepts(
new NoFloatAssignment(
'Don’t assign to floats',
new CodeLocation($statements_source, $expr)
Expand All @@ -40,5 +42,5 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve
}
}

class NoFloatAssignment extends \Psalm\Issue\PluginIssue {
class NoFloatAssignment extends PluginIssue {
}
11 changes: 7 additions & 4 deletions examples/plugins/StringChecker.php
Expand Up @@ -6,6 +6,9 @@
use Psalm\Checker\StatementsChecker;
use Psalm\CodeLocation;
use Psalm\FileManipulation;
use Psalm\Issue\InvalidClass;
use Psalm\Issue\UndefinedMethod;
use Psalm\IssueBuffer;
use Psalm\Plugin\EventHandler\AfterExpressionAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterExpressionAnalysisEvent;

Expand All @@ -29,8 +32,8 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve
) {
$absolute_class = preg_split('/[:]/', $expr->value)[0];

if (\Psalm\IssueBuffer::accepts(
new \Psalm\Issue\InvalidClass(
if (IssueBuffer::accepts(
new InvalidClass(
'Use ::class constants when representing class names',
new CodeLocation($statements_source, $expr),
$absolute_class
Expand All @@ -54,8 +57,8 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve
$appearing_method_id = $codebase->getAppearingMethodId($method_id);

if (!$appearing_method_id) {
if (\Psalm\IssueBuffer::accepts(
new \Psalm\Issue\UndefinedMethod(
if (IssueBuffer::accepts(
new UndefinedMethod(
'Method ' . $method_id . ' does not exist',
new CodeLocation($statements_source, $expr),
$method_id
Expand Down
2 changes: 0 additions & 2 deletions examples/plugins/composer-based/echo-checker/EchoChecker.php
Expand Up @@ -17,8 +17,6 @@ class EchoChecker implements AfterStatementAnalysisInterface
/**
* Called after a statement has been checked
*
* @param FileManipulation[] $file_replacements
*
* @return null|false
*/
public static function afterStatementAnalysis(AfterStatementAnalysisEvent $event): ?bool {
Expand Down
5 changes: 4 additions & 1 deletion psalm
@@ -1,4 +1,7 @@
#!/usr/bin/env php
<?php

use Psalm\Internal\Cli\Psalm;

require_once __DIR__ . '/src/Psalm/Internal/Cli/Psalm.php';
\Psalm\Internal\Cli\Psalm::run($argv);
Psalm::run($argv);
5 changes: 4 additions & 1 deletion psalm-language-server
@@ -1,4 +1,7 @@
#!/usr/bin/env php
<?php

use Psalm\Internal\Cli\LanguageServer;

require_once __DIR__ . '/src/Psalm/Internal/Cli/LanguageServer.php';
\Psalm\Internal\Cli\LanguageServer::run($argv);
LanguageServer::run($argv);
5 changes: 4 additions & 1 deletion psalm-plugin
@@ -1,4 +1,7 @@
#!/usr/bin/env php
<?php

use Psalm\Internal\Cli\Plugin;

require_once __DIR__ . '/src/Psalm/Internal/Cli/Plugin.php';
\Psalm\Internal\Cli\Plugin::run();
Plugin::run();
5 changes: 4 additions & 1 deletion psalm-refactor
@@ -1,4 +1,7 @@
#!/usr/bin/env php
<?php

use Psalm\Internal\Cli\Refactor;

require_once __DIR__ . '/src/Psalm/Internal/Cli/Refactor.php';
\Psalm\Internal\Cli\Refactor::run($argv);
Refactor::run($argv);
5 changes: 4 additions & 1 deletion psalter
@@ -1,4 +1,7 @@
#!/usr/bin/env php
<?php

use Psalm\Internal\Cli\Psalter;

require_once __DIR__ . '/src/Psalm/Internal/Cli/Psalter.php';
\Psalm\Internal\Cli\Psalter::run($argv);
Psalter::run($argv);
4 changes: 3 additions & 1 deletion scoper.inc.php
@@ -1,5 +1,7 @@
<?php

use Composer\Autoload\ClassLoader;

return [
'patchers' => [
function ($filePath, $prefix, $contents) {
Expand Down Expand Up @@ -79,7 +81,7 @@ function ($filePath, $prefix, $contents) {
},
],
'whitelist' => [
\Composer\Autoload\ClassLoader::class,
ClassLoader::class,
'Psalm\*',
],
'files-whitelist' => [
Expand Down
Expand Up @@ -658,7 +658,7 @@ private static function checkFunctionLikeTypeMatches(
}

/**
* @param TKeyedArray|TArray|TList|TClassStringMap $unpacked_atomic_array
* @param TKeyedArray|TArray|TList|TClassStringMap|null $unpacked_atomic_array
* @return null|false
* @psalm-suppress ComplexMethod
*/
Expand Down
Expand Up @@ -60,7 +60,7 @@
class AtomicMethodCallAnalyzer extends CallAnalyzer
{
/**
* @param TNamedObject|TTemplateParam $static_type
* @param TNamedObject|TTemplateParam|null $static_type
*
* @psalm-suppress ComplexMethod it's really complex, but unavoidably so
*/
Expand Down
Expand Up @@ -48,7 +48,7 @@
class ExistingAtomicMethodCallAnalyzer extends CallAnalyzer
{
/**
* @param TNamedObject|TTemplateParam $static_type
* @param TNamedObject|TTemplateParam|null $static_type
* @param list<PhpParser\Node\Arg> $args
*/
public static function analyze(
Expand Down
Expand Up @@ -37,7 +37,7 @@
class MethodCallReturnTypeFetcher
{
/**
* @param TNamedObject|TTemplateParam $static_type
* @param TNamedObject|TTemplateParam|null $static_type
* @param list<PhpParser\Node\Arg> $args
*/
public static function fetch(
Expand Down
1 change: 1 addition & 0 deletions src/Psalm/Internal/Cli/LanguageServer.php
Expand Up @@ -219,6 +219,7 @@ function (string $arg) use ($valid_long_options): void {
$include_collector = new IncludeCollector();

$first_autoloader = $include_collector->runAndCollect(
// we ignore the FQN because of a hack in scoper.inc that needs full path
// phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName
function () use ($current_dir, $options, $vendor_dir): ?\Composer\Autoload\ClassLoader {
return CliUtils::requireAutoloaders($current_dir, isset($options['r']), $vendor_dir);
Expand Down
1 change: 1 addition & 0 deletions src/Psalm/Internal/Cli/Psalm.php
Expand Up @@ -211,6 +211,7 @@ public static function run(array $argv): void

$include_collector = new IncludeCollector();
$first_autoloader = $include_collector->runAndCollect(
// we ignore the FQN because of a hack in scoper.inc that needs full path
// phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName
function () use ($current_dir, $options, $vendor_dir): ?\Composer\Autoload\ClassLoader {
return CliUtils::requireAutoloaders($current_dir, isset($options['r']), $vendor_dir);
Expand Down
1 change: 1 addition & 0 deletions src/Psalm/Internal/Cli/Psalter.php
Expand Up @@ -202,6 +202,7 @@ public static function run(array $argv): void

$include_collector = new IncludeCollector();
$first_autoloader = $include_collector->runAndCollect(
// we ignore the FQN because of a hack in scoper.inc that needs full path
// phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName
function () use ($current_dir, $options, $vendor_dir): ?\Composer\Autoload\ClassLoader {
return CliUtils::requireAutoloaders($current_dir, isset($options['r']), $vendor_dir);
Expand Down
1 change: 1 addition & 0 deletions src/Psalm/Internal/Cli/Refactor.php
Expand Up @@ -177,6 +177,7 @@ function (string $arg) use ($valid_long_options): void {

$include_collector = new IncludeCollector();
$first_autoloader = $include_collector->runAndCollect(
// we ignore the FQN because of a hack in scoper.inc that needs full path
// phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName
function () use ($current_dir, $options, $vendor_dir): ?\Composer\Autoload\ClassLoader {
return CliUtils::requireAutoloaders($current_dir, isset($options['r']), $vendor_dir);
Expand Down

0 comments on commit 926a56a

Please sign in to comment.