Skip to content
Open
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
22 changes: 22 additions & 0 deletions src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
return $fq_class_name;
}

public function analyze(

Check failure on line 163 in src/Psalm/Internal/Analyzer/ClassAnalyzer.php

View workflow job for this annotation

GitHub Actions / build

ComplexMethod

src/Psalm/Internal/Analyzer/ClassAnalyzer.php:163:21: ComplexMethod: This method’s complexity is greater than the project limit (method graph size = 207, average path length = 356) (see https://psalm.dev/260)

Check failure on line 163 in src/Psalm/Internal/Analyzer/ClassAnalyzer.php

View workflow job for this annotation

GitHub Actions / build

ComplexMethod

src/Psalm/Internal/Analyzer/ClassAnalyzer.php:163:21: ComplexMethod: This method’s complexity is greater than the project limit (method graph size = 207, average path length = 356) (see https://psalm.dev/260)
?Context $class_context = null,
?Context $global_context = null,
): void {
Expand Down Expand Up @@ -391,6 +391,28 @@
}

if ($storage->invalid_dependencies) {
foreach ($storage->invalid_dependencies as $dependency_name_lc => $_) {
if (!isset($storage->used_traits[$dependency_name_lc])) {
continue;
}

foreach ($class->stmts as $stmt) {
if (!$stmt instanceof PhpParser\Node\Stmt\TraitUse) {
continue;
}

$this->analyzeTraitUse(
$this->source->getAliases(),
$stmt,
$project_analyzer,
$storage,
$class_context,
$global_context,
$constructor_analyzer,
);
}
}

return;
}

Expand Down
36 changes: 32 additions & 4 deletions src/Psalm/Internal/Codebase/Populator.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ private function populateClassLikeStorage(ClassLikeStorage $storage, array $depe

$dependency->populated = false;
unset($dependency->invalid_dependencies[$fq_classlike_name_lc]);
$this->populateClassLikeStorage($dependency, $dependent_classlikes);
$this->populateClassLikeStorage($dependency, $dependency->dependent_classlikes);
}

unset($this->invalid_class_storages[$fq_classlike_name_lc]);
Expand Down Expand Up @@ -411,6 +411,9 @@ private function populateOverriddenMethods(
}
}

/**
* @param lowercase-string $used_trait_lc
*/
private function populateDataFromTrait(
ClassLikeStorage $storage,
ClassLikeStorageProvider $storage_provider,
Expand All @@ -425,6 +428,12 @@ private function populateDataFromTrait(
);
$trait_storage = $storage_provider->get($used_trait_lc);
} catch (InvalidArgumentException) {
$this->progress->debug('Populator could not find dependency (' . __LINE__ . ")\n");

$storage->invalid_dependencies[$used_trait_lc] = true;

$this->invalid_class_storages[$used_trait_lc][] = $storage;

return;
}

Expand Down Expand Up @@ -589,14 +598,21 @@ private function populateInterfaceData(
$new_parents = array_keys($interface_storage->parent_interfaces);
$new_parents[] = $interface_storage->name;
foreach ($new_parents as $new_parent) {
$new_parent_lc = strtolower($new_parent);
try {
$new_parent = strtolower(
$new_parent_lc = strtolower(
$this->classlikes->getUnAliasedName(
$new_parent,
$new_parent_lc,
),
);
$new_parent_interface_storage = $storage_provider->get($new_parent);
$new_parent_interface_storage = $storage_provider->get($new_parent_lc);
} catch (InvalidArgumentException) {
$this->progress->debug('Populator could not find dependency (' . __LINE__ . ")\n");

$storage->invalid_dependencies[$new_parent_lc] = true;

$this->invalid_class_storages[$new_parent_lc][] = $storage;

continue;
}

Expand Down Expand Up @@ -663,6 +679,9 @@ private static function extendTemplateParams(
}
}

/**
* @param lowercase-string $parent_interface_lc
*/
private function populateInterfaceDataFromParentInterface(
ClassLikeStorage $storage,
ClassLikeStorageProvider $storage_provider,
Expand All @@ -680,6 +699,9 @@ private function populateInterfaceDataFromParentInterface(
$this->progress->debug('Populator could not find dependency (' . __LINE__ . ")\n");

$storage->invalid_dependencies[$parent_interface_lc] = true;

$this->invalid_class_storages[$parent_interface_lc][] = $storage;

return;
}

Expand All @@ -706,6 +728,9 @@ private function populateInterfaceDataFromParentInterface(
}
}

/**
* @param lowercase-string $implemented_interface_lc
*/
private function populateDataFromImplementedInterface(
ClassLikeStorage $storage,
ClassLikeStorageProvider $storage_provider,
Expand All @@ -723,6 +748,9 @@ private function populateDataFromImplementedInterface(
$this->progress->debug('Populator could not find dependency (' . __LINE__ . ")\n");

$storage->invalid_dependencies[$implemented_interface_lc] = true;

$this->invalid_class_storages[$implemented_interface_lc][] = $storage;

return;
}

Expand Down
132 changes: 132 additions & 0 deletions tests/StubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,138 @@ class A extends PartiallyStubbedClass {}
$this->analyzeFile($file_path, new Context());
}

public function testUseOnlyStubbedTrait(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__),
'<?xml version="1.0"?>
<psalm
errorLevel="1"
>
<projectFiles>
<directory name="src" />
</projectFiles>

<stubs>
<file name="tests/fixtures/stubs/trait.phpstub" />
</stubs>
</psalm>',
),
);

$file_path = (string) getcwd() . '/src/somefile.php';

$this->addFile(
$file_path,
'<?php
namespace Foo;

class A {
use StubbedTrait;

public function run(): float {
return $this->run_stubbed_trait(rand(7, 9));
}
}',
);

$this->analyzeFile($file_path, new Context());
}

public function testUseOnlyStubbedInterface(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__),
'<?xml version="1.0"?>
<psalm
errorLevel="1"
>
<projectFiles>
<directory name="src" />
</projectFiles>

<stubs>
<file name="tests/fixtures/stubs/interface.phpstub" />
</stubs>
</psalm>',
),
);

$file_path = (string) getcwd() . '/src/somefile.php';

$this->addFile(
$file_path,
'<?php
namespace Foo;

class A implements StubbedInterface {
/**
* @param int $x
*/
public function run_stubbed($x): float {
return $x . "";
}
}',
);

$this->expectExceptionMessage('InvalidReturnStatement');
$this->expectException(CodeException::class);

$this->analyzeFile($file_path, new Context());
}

public function testUseOnlyStubbedTraitAndInterface(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__),
'<?xml version="1.0"?>
<psalm
errorLevel="1"
>
<projectFiles>
<directory name="src" />
</projectFiles>

<stubs>
<file name="tests/fixtures/stubs/trait.phpstub" />
<file name="tests/fixtures/stubs/interface.phpstub" />
</stubs>
</psalm>',
),
);

$file_path = (string) getcwd() . '/src/somefile.php';

$this->addFile(
$file_path,
'<?php
namespace Foo;

class A implements StubbedInterface {
use StubbedTrait;

public function run(): float {
return $this->run_stubbed_trait(rand(7, 9));
}

/**
* @param int $x
*/
public function run_stubbed($x): float {
return $x . "";
}
}',
);

$this->expectExceptionMessage('InvalidReturnStatement');
$this->expectException(CodeException::class);

$this->analyzeFile($file_path, new Context());
}

public function testStubFileWithExtendedStubbedClass(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/stubs/interface.phpstub
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Foo;

interface StubbedInterface
{
/**
* @param int $x
*/
public function run_stubbed($x): float;
}
13 changes: 13 additions & 0 deletions tests/fixtures/stubs/trait.phpstub
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Foo;

trait StubbedTrait
{
/**
* @param int $a
*/
public function run_stubbed_trait($a): float
{
return $a * 1.5;
}
}
Loading