Skip to content

Commit

Permalink
Deprecated BrokerAwareExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 14, 2021
1 parent f2f79f9 commit db2f7fb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 32 deletions.
5 changes: 4 additions & 1 deletion src/Reflection/BrokerAwareExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use PHPStan\Broker\Broker;

/** @api */
/**
* @api
* @deprecated Inject PHPStan\Reflection\ReflectionProvider into the constructor instead
*/
interface BrokerAwareExtension
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,34 @@

namespace PHPStan\Reflection\Php;

use PHPStan\Broker\Broker;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\MixedType;

class UniversalObjectCratesClassReflectionExtension
implements \PHPStan\Reflection\PropertiesClassReflectionExtension, \PHPStan\Reflection\BrokerAwareExtension
implements \PHPStan\Reflection\PropertiesClassReflectionExtension
{

/** @var string[] */
private array $classes;

private \PHPStan\Broker\Broker $broker;
private ReflectionProvider $reflectionProvider;

/**
* @param string[] $classes
*/
public function __construct(array $classes)
public function __construct(ReflectionProvider $reflectionProvider, array $classes)
{
$this->reflectionProvider = $reflectionProvider;
$this->classes = $classes;
}

public function setBroker(Broker $broker): void
{
$this->broker = $broker;
}

public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
{
return self::isUniversalObjectCrate(
$this->broker,
$this->reflectionProvider,
$this->classes,
$classReflection
);
Expand Down
17 changes: 6 additions & 11 deletions src/Type/Php/TypeSpecifyingFunctionsDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,31 @@
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Broker\Broker;
use PHPStan\Reflection\BrokerAwareExtension;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Comparison\ImpossibleCheckTypeHelper;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\Type;

class TypeSpecifyingFunctionsDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension, TypeSpecifierAwareExtension, BrokerAwareExtension
class TypeSpecifyingFunctionsDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension, TypeSpecifierAwareExtension
{

private bool $treatPhpDocTypesAsCertain;

private \PHPStan\Broker\Broker $broker;
private ReflectionProvider $reflectionProvider;

private \PHPStan\Analyser\TypeSpecifier $typeSpecifier;

private ?\PHPStan\Rules\Comparison\ImpossibleCheckTypeHelper $helper = null;

public function __construct(bool $treatPhpDocTypesAsCertain)
public function __construct(ReflectionProvider $reflectionProvider, bool $treatPhpDocTypesAsCertain)
{
$this->reflectionProvider = $reflectionProvider;
$this->treatPhpDocTypesAsCertain = $treatPhpDocTypesAsCertain;
}

public function setBroker(Broker $broker): void
{
$this->broker = $broker;
}

public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
Expand Down Expand Up @@ -89,7 +84,7 @@ public function getTypeFromFunctionCall(
private function getHelper(): ImpossibleCheckTypeHelper
{
if ($this->helper === null) {
$this->helper = new ImpossibleCheckTypeHelper($this->broker, $this->typeSpecifier, $this->broker->getUniversalObjectCratesClasses(), $this->treatPhpDocTypesAsCertain);
$this->helper = new ImpossibleCheckTypeHelper($this->reflectionProvider, $this->typeSpecifier, $this->reflectionProvider->getUniversalObjectCratesClasses(), $this->treatPhpDocTypesAsCertain);
}

return $this->helper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Reflection\Php;

use PHPStan\Broker\Broker;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;

Expand All @@ -11,35 +10,33 @@ class UniversalObjectCratesClassReflectionExtensionTest extends \PHPStan\Testing

public function testNonexistentClass(): void
{
$broker = self::getContainer()->getByType(Broker::class);
$extension = new UniversalObjectCratesClassReflectionExtension([
$reflectionProvider = $this->createReflectionProvider();
$extension = new UniversalObjectCratesClassReflectionExtension($reflectionProvider, [
'NonexistentClass',
'stdClass',
]);
$extension->setBroker($broker);
$this->assertTrue($extension->hasProperty($broker->getClass(\stdClass::class), 'foo'));
$this->assertTrue($extension->hasProperty($reflectionProvider->getClass(\stdClass::class), 'foo'));
}

public function testDifferentGetSetType(): void
{
require_once __DIR__ . '/data/universal-object-crates.php';

$broker = self::getContainer()->getByType(Broker::class);
$extension = new UniversalObjectCratesClassReflectionExtension([
$reflectionProvider = $this->createReflectionProvider();
$extension = new UniversalObjectCratesClassReflectionExtension($reflectionProvider, [
'UniversalObjectCreates\DifferentGetSetTypes',
]);
$extension->setBroker($broker);

$this->assertEquals(
new ObjectType('UniversalObjectCreates\DifferentGetSetTypesValue'),
$extension
->getProperty($broker->getClass('UniversalObjectCreates\DifferentGetSetTypes'), 'foo')
->getProperty($reflectionProvider->getClass('UniversalObjectCreates\DifferentGetSetTypes'), 'foo')
->getReadableType()
);
$this->assertEquals(
new StringType(),
$extension
->getProperty($broker->getClass('UniversalObjectCreates\DifferentGetSetTypes'), 'foo')
->getProperty($reflectionProvider->getClass('UniversalObjectCreates\DifferentGetSetTypes'), 'foo')
->getWritableType()
);
}
Expand Down

0 comments on commit db2f7fb

Please sign in to comment.