Skip to content

Commit 8911a64

Browse files
authored
Fix lsp renaming bug where contructor dont follow typephp type annotation enforement (#35)
1 parent 55a144b commit 8911a64

4 files changed

Lines changed: 67 additions & 3 deletions

File tree

src/Contract/ContractParser.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ private static function parseMethodHierarchyDocs(
559559
$baseParamNames = [];
560560
$baseParamSet = [];
561561
$baseParamVariadic = [];
562+
$isConstructor = ($ref->getName() === '__construct');
562563

563564
foreach ($baseParams as $idx => $p) {
564565
$baseParamNames[$idx] = $p->getName();
@@ -597,7 +598,13 @@ private static function parseMethodHierarchyDocs(
597598
$paramTags = DocblockExtractor::getParamTags($phpDocNode);
598599

599600
foreach ($paramTags as $paramName => $paramTag) {
600-
$targetParamName = self::resolveTargetParamName($paramName, $baseParamSet, $baseParamNames, $hierNameToIndex);
601+
$targetParamName = self::resolveTargetParamName(
602+
$paramName,
603+
$baseParamSet,
604+
$baseParamNames,
605+
$hierNameToIndex,
606+
$isConstructor
607+
);
601608

602609
if ($targetParamName !== null && ! isset($types[$targetParamName])) {
603610
$type = $paramTag->type;
@@ -631,12 +638,17 @@ private static function resolveTargetParamName(
631638
string $paramName,
632639
array $baseParamSet,
633640
array $baseParamNames,
634-
array $hierNameToIndex
641+
array $hierNameToIndex,
642+
bool $isConstructor = false
635643
): ?string {
636644
if (isset($baseParamSet[$paramName])) {
637645
return $paramName;
638646
}
639647

648+
if ($isConstructor) {
649+
return null;
650+
}
651+
640652
$paramIndex = $hierNameToIndex[$paramName] ?? null;
641653
if ($paramIndex === null || ! isset($baseParamNames[$paramIndex])) {
642654
return null;
@@ -782,4 +794,4 @@ public static function substituteAliases(TypeNode $node, array $aliases): TypeNo
782794

783795
return $node;
784796
}
785-
}
797+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\Fixtures\Shopware\Exception;
6+
7+
abstract class BaseShopwareExceptionFixture extends \Exception
8+
{
9+
/**
10+
* @param string $message
11+
* @param array<string, mixed> $parameters
12+
* @param \Throwable|null $e
13+
*/
14+
public function __construct(
15+
string $message,
16+
array $parameters = [],
17+
?\Throwable $e = null
18+
) {
19+
parent::__construct($message, 0, $e);
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\Fixtures\Shopware\Exception;
6+
7+
class TableHelperExceptionFixture extends BaseShopwareExceptionFixture
8+
{
9+
public function __construct(
10+
string $message,
11+
?\Throwable $previousException = null
12+
) {
13+
parent::__construct($message, [], $previousException);
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use TypePHP\Tests\Fixtures\Shopware\Exception\TableHelperExceptionFixture;
6+
7+
describe('Constructor Parameter Inheritance Mismatch (Shopware TableHelperException)', function () {
8+
test('child constructor with different parameter names does not inherit parent constructor docblocks by positional index', function () {
9+
$previous = new \Exception('Underlying DB error');
10+
11+
$exception = new TableHelperExceptionFixture('Table missing', $previous);
12+
13+
expect($exception)->toBeInstanceOf(TableHelperExceptionFixture::class)
14+
->and($exception->getPrevious())->toBe($previous);
15+
});
16+
});

0 commit comments

Comments
 (0)