Skip to content

Commit

Permalink
StubPhpDocProvider - cannot cache methods because of varying $positio…
Browse files Browse the repository at this point in the history
…nalParameterNames
  • Loading branch information
ondrejmirtes committed Feb 16, 2020
1 parent a293802 commit e8bb833
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/PhpDoc/StubPhpDocProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class StubPhpDocProvider
/** @var array<string, array<string, ResolvedPhpDocBlock|null>> */
private $propertyMap = [];

/** @var array<string, array<string, ResolvedPhpDocBlock|null>> */
/** @var array<string, array<string, null>> */
private $methodMap = [];

/** @var array<string, ResolvedPhpDocBlock|null> */
Expand Down Expand Up @@ -162,7 +162,7 @@ public function findMethodPhpDoc(string $className, string $methodName, array $p
$parameterNameMapping[$methodParameterNames[$i]] = $parameterName;
}

return $this->methodMap[$className][$methodName] = $resolvedPhpDoc->changeParameterNamesByMapping($parameterNameMapping);
return $resolvedPhpDoc->changeParameterNamesByMapping($parameterNameMapping);
}

return null;
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,11 @@ public function testRuleWithStaticMethods(): void
]);
}

public function testBug2950(): void
{
$this->reportMaybes = true;
$this->reportStatic = true;
$this->analyse([__DIR__ . '/data/bug-2950.php'], []);
}

}
119 changes: 119 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-2950.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Bug2950;

class BlockFactory
{

/** @var \ArrayObject<int, int> */
private static $fullList = null;

public static function isRegistered(int $id): bool
{
$b = self::$fullList[$id << 4];
return $b !== null;
}
}

class Attribute
{

public function getId(): int
{
return 0;
}

public function isSyncable(): bool
{
return false;
}

public function isDesynchronized(): bool
{
return false;
}

public function getValue(): float
{
return 0.0;
}

public function setValue(float $f) : self
{
return $this;
}
}



/**
* @phpstan-implements \ArrayAccess<int, float>
*/
class AttributeMap implements \ArrayAccess
{

/** @var Attribute[] */
private $attributes = [];

public function addAttribute(Attribute $attribute): void
{
$this->attributes[$attribute->getId()] = $attribute;
}

public function getAttribute(int $id): ?Attribute
{
return $this->attributes[$id] ?? null;
}

/**
* @return Attribute[]
*/
public function getAll(): array
{
return $this->attributes;
}

/**
* @return Attribute[]
*/
public function needSend(): array
{
return array_filter($this->attributes, function(Attribute $attribute){
return $attribute->isSyncable() and $attribute->isDesynchronized();
});
}

/**
* @param int $offset
*/
public function offsetExists($offset): bool
{
return isset($this->attributes[$offset]);
}

/**
* @param int $offset
*/
public function offsetGet($offset): float
{
return $this->attributes[$offset]->getValue();
}

/**
* @param int $offset
* @param float $value
*/
public function offsetSet($offset, $value): void
{
$this->attributes[$offset]->setValue($value);
}

/**
* @param int $offset
*/
public function offsetUnset($offset): void
{
throw new \RuntimeException("Could not unset an attribute from an attribute map");
}

}

0 comments on commit e8bb833

Please sign in to comment.