Skip to content

Commit

Permalink
replace fully qualified names by imports
Browse files Browse the repository at this point in the history
  • Loading branch information
orklah committed Dec 27, 2021
1 parent 3e29a3d commit 65fffd2
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 45 deletions.
6 changes: 4 additions & 2 deletions examples/TemplateChecker.php
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
13 changes: 8 additions & 5 deletions examples/plugins/FunctionCasingChecker.php
Original file line number Diff line number Diff line change
@@ -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,7 +62,7 @@ public static function afterMethodCallAnalysis(AfterMethodCallAnalysisEvent $eve
// fall through
}
}
} catch (\Exception $e) {
} catch (Exception $e) {
// can throw if storage is missing
}
}
Expand Down Expand Up @@ -93,7 +96,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 +106,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
5 changes: 4 additions & 1 deletion psalm
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions src/Psalm/Internal/Cli/LanguageServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Psalm\Internal\Cli;

use Composer\Autoload\ClassLoader;
use Psalm\Config;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\CliUtils;
Expand Down Expand Up @@ -219,6 +220,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
3 changes: 2 additions & 1 deletion src/Psalm/Internal/Cli/Psalm.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ 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): ?ClassLoader {
function () use ($current_dir, $options, $vendor_dir): ?\Composer\Autoload\ClassLoader {
return CliUtils::requireAutoloaders($current_dir, isset($options['r']), $vendor_dir);
}
);
Expand Down
2 changes: 2 additions & 0 deletions src/Psalm/Internal/Cli/Psalter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Psalm\Internal\Cli;

use Composer\Autoload\ClassLoader;
use Composer\XdebugHandler\XdebugHandler;
use Psalm\Config;
use Psalm\Exception\UnsupportedIssueToFixException;
Expand Down Expand Up @@ -202,6 +203,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
2 changes: 2 additions & 0 deletions src/Psalm/Internal/Cli/Refactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Psalm\Internal\Cli;

use Composer\Autoload\ClassLoader;
use Composer\XdebugHandler\XdebugHandler;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\CliUtils;
Expand Down Expand Up @@ -177,6 +178,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 65fffd2

Please sign in to comment.