Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Psalm to analysis chain #1223

Merged
merged 4 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: phpstan
name: static-analysis

on:
push:
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"composer/package-versions-deprecated": "^1.11",
"friendsofphp/php-cs-fixer": "^2.17 || ^3.0",
"phpstan/phpstan": "^1.6",
"phpunit/phpunit": ">=8"
"phpunit/phpunit": ">=8",
"vimeo/psalm": "^4.23"
},
"autoload-dev": {
"exclude-from-classmap": [
Expand All @@ -85,7 +86,8 @@
"@testlegacy"
],
"analyse": [
"phpstan analyse --memory-limit=2G"
"phpstan analyse --memory-limit=2G",
"psalm"
],
"spectral": "for ff in `find Examples -name '*.yaml'`; do spectral lint $ff; done",
"docs:dev": "cd docs && npm run dev",
Expand Down
8 changes: 8 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.23.0@f1fe6ff483bf325c803df9f510d09a03fd796f88">
<file src="src/Serializer.php">
<ReservedWord occurrences="1">
<code>Yaml::parse($contents)</code>
</ReservedWord>
</file>
</files>
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src" />
Expand All @@ -14,6 +15,5 @@
</projectFiles>

<issueHandlers>
<DuplicateClass errorLevel="suppress" />
</issueHandlers>
</psalm>
11 changes: 9 additions & 2 deletions src/Analysers/TokenAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,13 +516,18 @@ private function parseAttribute(array &$tokens, &$token, Context $parseContext):
}
}

private function php8NamespaceToken()
/**
* @return int[]
*/
private function php8NamespaceToken(): array
{
return defined('T_NAME_QUALIFIED') ? [T_NAME_QUALIFIED, T_NAME_FULLY_QUALIFIED] : [];
}

/**
* Parse namespaced string.
*
* @param array|string $token
*/
private function parseNamespace(array &$tokens, &$token, Context $parseContext): string
{
Expand All @@ -541,6 +546,8 @@ private function parseNamespace(array &$tokens, &$token, Context $parseContext):

/**
* Parse comma separated list of namespaced strings.
*
* @param array|string $token
*/
private function parseNamespaceList(array &$tokens, &$token, Context $parseContext): array
{
Expand All @@ -560,7 +567,7 @@ private function parseNamespaceList(array &$tokens, &$token, Context $parseConte
*/
private function parseUseStatement(array &$tokens, &$token, Context $parseContext): array
{
$normalizeAlias = function ($alias) {
$normalizeAlias = function ($alias): string {
$alias = ltrim($alias, '\\');
$elements = explode('\\', $alias);

Expand Down
8 changes: 4 additions & 4 deletions src/Analysers/TokenScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function scanTokens(array $tokens): array
$lastToken = null;
$stack = [];

$initUnit = function ($uses) {
$initUnit = function ($uses): array {
return [
'uses' => $uses,
'interfaces' => [],
Expand Down Expand Up @@ -226,7 +226,7 @@ protected function resolveFQN(array $names, string $namespace, array $uses): arr
return array_values(array_map($resolve, $names));
}

protected function skipTo(array &$tokens, $char, bool $prev = false): void
protected function skipTo(array &$tokens, string $char, bool $prev = false): void
{
while (false !== ($token = next($tokens))) {
if (is_string($token) && $token == $char) {
Expand Down Expand Up @@ -265,9 +265,9 @@ protected function nextWord(array &$tokens): string
/**
* Parse a use statement.
*/
protected function parseFQNStatement(array &$tokens, &$token): array
protected function parseFQNStatement(array &$tokens, array &$token): array
{
$normalizeAlias = function ($alias) {
$normalizeAlias = function ($alias): string {
$alias = ltrim($alias, '\\');
$elements = explode('\\', $alias);

Expand Down
13 changes: 4 additions & 9 deletions src/Analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function __construct(array $annotations = [], Context $context = null)
$this->addAnnotations($annotations, $context);
}

public function addAnnotation($annotation, Context $context): void
public function addAnnotation(object $annotation, Context $context): void
{
if ($this->annotations->contains($annotation)) {
return;
Expand Down Expand Up @@ -237,7 +237,7 @@ public function getInterfacesOfClass(string $class, bool $direct = false): array

if (!$direct) {
// expand recursively for interfaces extending other interfaces
$collect = function ($interfaces, $cb) use (&$definitions) {
$collect = function ($interfaces, $cb) use (&$definitions): void {
foreach ($interfaces as $interface) {
if (isset($this->interfaces[$interface]['extends'])) {
$cb($this->interfaces[$interface]['extends'], $cb);
Expand Down Expand Up @@ -283,7 +283,7 @@ public function getTraitsOfClass(string $source, bool $direct = false): array

if (!$direct) {
// expand recursively for traits using other traits
$collect = function ($traits, $cb) use (&$definitions) {
$collect = function ($traits, $cb) use (&$definitions): void {
foreach ($traits as $trait) {
if (isset($this->traits[$trait]['traits'])) {
$cb($this->traits[$trait]['traits'], $cb);
Expand Down Expand Up @@ -352,12 +352,7 @@ public function getSchemaForSource(string $fqdn): ?AnnotationSchema
return null;
}

/**
* @param object $annotation
*
* @return \OpenApi\Context
*/
public function getContext($annotation): Context
public function getContext(object $annotation): ?Context
{
if ($annotation instanceof AbstractAnnotation) {
return $annotation->_context;
Expand Down
4 changes: 2 additions & 2 deletions src/Annotations/AbstractAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public function toYaml($flags = null): string
/**
* Generate the documentation in JSON format.
*/
public function toJson($flags = null): string
public function toJson(?int $flags = null): string
{
if ($flags === null) {
$flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE;
Expand Down Expand Up @@ -510,7 +510,7 @@ public function validate(array $stack = [], array $skip = [], string $ref = '',
*
* @param array|object $fields
*/
private static function _validate($fields, array $stack, array $skip, string $baseRef, $context): bool
private static function _validate($fields, array $stack, array $skip, string $baseRef, ?object $context): bool
{
$valid = true;
$blacklist = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class Schema extends AbstractAnnotation
* A numeric instance is valid against "multipleOf" if the result of the division of the instance by this
* property's value is an integer.
*
* @var number
* @var int|float
*/
public $multipleOf = Generator::UNDEFINED;

Expand Down
2 changes: 1 addition & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public function removeProcessor(callable $processor, bool $silent = false): Gene
*/
public function updateProcessor(callable $processor, ?callable $matcher = null): Generator
{
$matcher = $matcher ?: function ($other) use ($processor) {
$matcher = $matcher ?: function ($other) use ($processor): bool {
$otherClass = get_class($other);

return $processor instanceof $otherClass;
Expand Down
2 changes: 1 addition & 1 deletion src/Loggers/ConsoleLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(bool $debug = false)
$this->debug = $debug;
}

public function loggedMessageAboveNotice()
public function loggedMessageAboveNotice(): bool
{
return $this->loggedMessageAboveNotice;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Processors/AugmentProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ public function __invoke(Analysis $analysis)
}
}

protected function toRefKey(Context $context, $name)
protected function toRefKey(Context $context, ?string $name): string
{
$fqn = strtolower($context->fullyQualifiedName($name));

return ltrim($fqn, '\\');
}

protected function augmentType(Analysis $analysis, Property $property, Context $context, array $refs, array $varMatches)
protected function augmentType(Analysis $analysis, Property $property, Context $context, array $refs, array $varMatches): void
{
// docblock typehints
if (isset($varMatches['type'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Processors/CleanUnusedComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected function cleanup(Analysis $analysis): bool
}
}

$detachNested = function (Analysis $analysis, AbstractAnnotation $annotation, callable $detachNested) {
$detachNested = function (Analysis $analysis, AbstractAnnotation $annotation, callable $detachNested): void {
foreach ($annotation::$_nested as $nested) {
$nestedKey = ((array) $nested)[0];
if (!Generator::isDefault($annotation->{$nestedKey})) {
Expand Down
2 changes: 1 addition & 1 deletion src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Serializer
OA\XmlContent::class,
];

protected static function isValidAnnotationClass($className): bool
protected static function isValidAnnotationClass(string $className): bool
{
return in_array($className, self::$VALID_ANNOTATIONS);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Util
'object' => 'object',
];

public static function mapNativeType(Schema $schema, $type): bool
public static function mapNativeType(Schema $schema, string $type): bool
{
if (!array_key_exists($type, self::$NATIVE_TYPE_MAP)) {
return false;
Expand Down