2018-05-09 02:25:39 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2018-05-09 02:25:39 +02:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2020-02-02 20:23:30 +01:00
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2021-12-30 11:23:04 +01:00
|
|
|
use Psalm\Exception\CodeException;
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
|
2018-05-09 02:25:39 +02:00
|
|
|
class MagicPropertyTest extends TestCase
|
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2018-05-09 02:25:39 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testPhpDocPropertyWithoutGet(): void
|
2020-02-02 20:23:30 +01:00
|
|
|
{
|
|
|
|
Config::getInstance()->use_phpdoc_property_without_magic_or_parent = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @property string $hello
|
|
|
|
*/
|
|
|
|
class Child {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
2022-12-18 17:15:15 +01:00
|
|
|
$a = $child->hello;',
|
2020-02-02 20:23:30 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2018-05-09 02:25:39 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'propertyDocblock' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
namespace Bar;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property string $foo
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
/** @param string $name */
|
|
|
|
public function __get($name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
2018-07-17 02:32:16 +02:00
|
|
|
|
|
|
|
return null;
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function __set($name, $value): void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
2020-01-03 14:44:52 +01:00
|
|
|
$a->foo = "hello";',
|
2018-05-09 02:25:39 +02:00
|
|
|
],
|
|
|
|
'propertyOfTypeClassDocblock' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
namespace Bar;
|
|
|
|
|
|
|
|
class PropertyType {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property PropertyType $foo
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
/** @param string $name */
|
|
|
|
public function __get($name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
2018-07-17 02:32:16 +02:00
|
|
|
|
|
|
|
return null;
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function __set($name, $value): void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->foo = new PropertyType();',
|
|
|
|
],
|
|
|
|
'propertySealedDocblockDefinedPropertyFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
namespace Bar;
|
|
|
|
/**
|
|
|
|
* @property string $foo
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
class A {
|
2018-07-17 02:32:16 +02:00
|
|
|
public function __get(string $name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $name, $value): void {
|
|
|
|
}
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
echo $a->foo;',
|
|
|
|
],
|
|
|
|
/**
|
|
|
|
* With a magic setter and no annotations specifying properties or types, we can
|
|
|
|
* set anything we want on any variable name. The magic setter is trusted to figure
|
|
|
|
* it out.
|
|
|
|
*/
|
|
|
|
'magicSetterUndefinedPropertyNoAnnotation' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
class A {
|
|
|
|
public function __get(string $name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
2018-07-17 02:32:16 +02:00
|
|
|
|
|
|
|
return null;
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $name, $value): void {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function goodSet(): void {
|
|
|
|
$this->__set("foo", new stdClass());
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
/**
|
|
|
|
* With a magic getter and no annotations specifying properties or types, we can
|
|
|
|
* get anything we want with any variable name. The magic getter is trusted to figure
|
|
|
|
* it out.
|
|
|
|
*/
|
|
|
|
'magicGetterUndefinedPropertyNoAnnotation' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
class A {
|
|
|
|
public function __get(string $name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
2018-07-17 02:32:16 +02:00
|
|
|
|
|
|
|
return null;
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $name, $value): void {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function goodGet(): void {
|
|
|
|
echo $this->__get("foo");
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
/**
|
|
|
|
* The property $foo is defined as a string with the `@property` annotation. We
|
|
|
|
* use the magic setter to set it to a string, so everything is cool.
|
|
|
|
*/
|
|
|
|
'magicSetterValidAssignmentType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
/**
|
|
|
|
* @property string $foo
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public function __get(string $name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
2018-07-17 02:32:16 +02:00
|
|
|
|
|
|
|
return null;
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $name, $value): void {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function goodSet(): void {
|
|
|
|
$this->__set("foo", "value");
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'propertyDocblockAssignmentToMixed' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
/**
|
|
|
|
* @property string $foo
|
|
|
|
*/
|
|
|
|
class A {
|
2018-07-17 02:32:16 +02:00
|
|
|
public function __get(string $name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $name, $value): void {
|
|
|
|
}
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $b */
|
|
|
|
function foo($b) : void {
|
|
|
|
$a = new A();
|
|
|
|
$a->__set("foo", $b);
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-11-05 22:34:42 +01:00
|
|
|
'ignored_issues' => ['MixedAssignment', 'MixedPropertyTypeCoercion'],
|
2018-05-09 02:25:39 +02:00
|
|
|
],
|
2018-05-09 03:21:22 +02:00
|
|
|
'namedPropertyByVariable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 03:21:22 +02:00
|
|
|
class A {
|
|
|
|
/** @var string|null */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public function __get(string $var_name) : ?string {
|
|
|
|
if ($var_name === "foo") {
|
|
|
|
return $this->$var_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-05-09 04:01:05 +02:00
|
|
|
'getPropertyExplicitCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 04:01:05 +02:00
|
|
|
class A {
|
|
|
|
public function __get(string $name) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function __set(string $name, $value) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property string $test
|
|
|
|
*/
|
|
|
|
class B extends A {
|
|
|
|
public function test(): string {
|
|
|
|
return $this->__get("test");
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-05-09 04:13:26 +02:00
|
|
|
'inheritedGetPropertyExplicitCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 04:13:26 +02:00
|
|
|
/**
|
|
|
|
* @property string $test
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public function __get(string $name) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function __set(string $name, $value) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function test(): string {
|
|
|
|
return $this->__get("test");
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-06-08 17:08:57 +02:00
|
|
|
'undefinedThisPropertyFetchWithMagic' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-06-08 17:08:57 +02:00
|
|
|
/**
|
|
|
|
* @property-read string $name
|
|
|
|
* @property string $otherName
|
|
|
|
*/
|
|
|
|
class A {
|
2018-07-17 02:32:16 +02:00
|
|
|
public function __get(string $name): void {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function goodGet(): void {
|
|
|
|
echo $this->name;
|
|
|
|
}
|
|
|
|
public function goodGet2(): void {
|
|
|
|
echo $this->otherName;
|
|
|
|
}
|
2018-06-08 17:08:57 +02:00
|
|
|
}
|
|
|
|
$a = new A();
|
|
|
|
echo $a->name;
|
|
|
|
echo $a->otherName;',
|
|
|
|
],
|
2020-03-24 23:32:57 +01:00
|
|
|
'psalmUndefinedThisPropertyFetchWithMagic' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-24 23:32:57 +01:00
|
|
|
/**
|
|
|
|
* @psalm-property-read string $name
|
|
|
|
* @property string $otherName
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public function __get(string $name): void {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function goodGet(): void {
|
|
|
|
echo $this->name;
|
|
|
|
}
|
|
|
|
public function goodGet2(): void {
|
|
|
|
echo $this->otherName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$a = new A();
|
|
|
|
echo $a->name;
|
|
|
|
echo $a->otherName;',
|
|
|
|
],
|
2018-06-08 17:08:57 +02:00
|
|
|
'directFetchForMagicProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-06-08 17:08:57 +02:00
|
|
|
/**
|
|
|
|
* @property string $test
|
|
|
|
*/
|
|
|
|
class C {
|
|
|
|
public function __get(string $name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function __set(string $name, $value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test(): string
|
|
|
|
{
|
|
|
|
return $this->test;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-06-10 16:02:46 +02:00
|
|
|
'magicPropertyFetchOnProtected' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-06-10 16:02:46 +02:00
|
|
|
class C {
|
|
|
|
/** @var string */
|
|
|
|
protected $foo = "foo";
|
|
|
|
|
|
|
|
public function __get(string $name) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function __set(string $name, $value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$c = new C();
|
|
|
|
$c->foo = "bar";
|
|
|
|
echo $c->foo;',
|
|
|
|
'assertions' => [],
|
2022-11-05 22:34:42 +01:00
|
|
|
'ignored_issues' => ['MixedArgument'],
|
2018-06-10 16:02:46 +02:00
|
|
|
],
|
2019-05-27 15:18:34 +02:00
|
|
|
'dontAssumeNonNullAfterPossibleMagicFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-05-27 15:18:34 +02:00
|
|
|
class C {
|
|
|
|
public function __get(string $name) : string {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(?C $c) : void {
|
|
|
|
echo $c->foo;
|
|
|
|
|
|
|
|
if ($c) {}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-11-05 22:34:42 +01:00
|
|
|
'ignored_issues' => ['PossiblyNullPropertyFetch'],
|
2019-05-27 15:18:34 +02:00
|
|
|
],
|
2018-11-21 22:57:54 +01:00
|
|
|
'accessInMagicGet' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-21 22:57:54 +01:00
|
|
|
class X {
|
|
|
|
public function __get(string $name) : string {
|
|
|
|
switch ($name) {
|
|
|
|
case "a":
|
|
|
|
return $this->other;
|
|
|
|
case "other":
|
|
|
|
return "foo";
|
|
|
|
}
|
|
|
|
return "default";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-11-05 22:34:42 +01:00
|
|
|
'ignored_issues' => ['MixedReturnStatement', 'MixedInferredReturnType'],
|
2018-11-21 22:57:54 +01:00
|
|
|
],
|
2019-01-19 20:26:27 +01:00
|
|
|
'overrideInheritedProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-19 20:26:27 +01:00
|
|
|
interface ServiceInterface {}
|
|
|
|
|
|
|
|
class ConcreteService implements ServiceInterface {
|
|
|
|
public function getById(int $i) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
/** @var ServiceInterface */
|
|
|
|
protected $service;
|
|
|
|
|
|
|
|
public function __construct(ServiceInterface $service)
|
|
|
|
{
|
|
|
|
$this->service = $service;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @property ConcreteService $service */
|
|
|
|
class FooBar extends Foo
|
|
|
|
{
|
|
|
|
public function __construct(ConcreteService $concreteService)
|
|
|
|
{
|
|
|
|
parent::__construct($concreteService);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function doSomething(): void
|
|
|
|
{
|
|
|
|
$this->service->getById(123);
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-01-19 20:26:27 +01:00
|
|
|
],
|
2019-01-23 22:58:50 +01:00
|
|
|
'magicInterfacePropertyRead' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-23 22:58:50 +01:00
|
|
|
/**
|
|
|
|
* @property-read string $foo
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
interface GetterSetter {
|
|
|
|
/** @return mixed */
|
|
|
|
public function __get(string $key);
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $key, $value) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress NoInterfaceProperties */
|
|
|
|
function getFoo(GetterSetter $o) : string {
|
|
|
|
return $o->foo;
|
|
|
|
}',
|
|
|
|
],
|
2020-03-24 23:32:57 +01:00
|
|
|
'phanMagicInterfacePropertyRead' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-24 23:32:57 +01:00
|
|
|
/**
|
|
|
|
* @psalm-property-read string $foo
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
interface GetterSetter {
|
|
|
|
/** @return mixed */
|
|
|
|
public function __get(string $key);
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $key, $value) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress NoInterfaceProperties */
|
|
|
|
function getFoo(GetterSetter $o) : string {
|
|
|
|
return $o->foo;
|
|
|
|
}',
|
|
|
|
],
|
2019-01-23 22:58:50 +01:00
|
|
|
'magicInterfacePropertyWrite' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-23 22:58:50 +01:00
|
|
|
/**
|
|
|
|
* @property-write string $foo
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
interface GetterSetter {
|
|
|
|
/** @return mixed */
|
|
|
|
public function __get(string $key);
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $key, $value) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress NoInterfaceProperties */
|
|
|
|
function getFoo(GetterSetter $o) : void {
|
|
|
|
$o->foo = "hello";
|
|
|
|
}',
|
|
|
|
],
|
2020-03-24 23:32:57 +01:00
|
|
|
'psalmMagicInterfacePropertyWrite' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-24 23:32:57 +01:00
|
|
|
/**
|
|
|
|
* @psalm-property-write string $foo
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
interface GetterSetter {
|
|
|
|
/** @return mixed */
|
|
|
|
public function __get(string $key);
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $key, $value) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress NoInterfaceProperties */
|
|
|
|
function getFoo(GetterSetter $o) : void {
|
|
|
|
$o->foo = "hello";
|
|
|
|
}',
|
|
|
|
],
|
2019-02-23 02:42:36 +01:00
|
|
|
'psalmPropertyDocblock' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-23 02:42:36 +01:00
|
|
|
namespace Bar;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-property string $foo
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
/** @param string $name */
|
|
|
|
public function __get($name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function __set($name, $value): void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
2020-01-03 14:44:52 +01:00
|
|
|
$a->foo = "hello";',
|
2019-02-23 02:42:36 +01:00
|
|
|
],
|
2019-02-23 15:37:37 +01:00
|
|
|
'overridePropertyAnnotations' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-23 15:37:37 +01:00
|
|
|
namespace Bar;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property int $foo
|
|
|
|
* @psalm-property string $foo
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
/** @param string $name */
|
|
|
|
public function __get($name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function __set($name, $value): void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
2020-01-03 14:44:52 +01:00
|
|
|
$a->foo = "hello";',
|
2019-02-23 15:37:37 +01:00
|
|
|
],
|
|
|
|
'overrideWithReadWritePropertyAnnotations' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-23 15:37:37 +01:00
|
|
|
namespace Bar;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-property int $foo
|
|
|
|
* @property-read string $foo
|
|
|
|
* @property-write array $foo
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
/** @param string $name */
|
|
|
|
public function __get($name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function __set($name, $value): void {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function takesString(string $s): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->foo = [];
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->takesString($a->foo);',
|
|
|
|
],
|
2019-07-06 06:00:17 +02:00
|
|
|
'removeAssertionsAfterCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-07-06 06:00:17 +02:00
|
|
|
class C {
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __get(string $name) {
|
|
|
|
return rand();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __set(string $name, $value) {}
|
|
|
|
|
|
|
|
public function main() : void {
|
|
|
|
if (!isset($this->a)) {
|
|
|
|
$this->a = ["something"];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedArrayAccess
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
echo $this->a[0];
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-07-06 06:00:17 +02:00
|
|
|
],
|
2020-06-20 22:53:17 +02:00
|
|
|
'magicPropertyDefinedOnTrait' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-06-20 22:53:17 +02:00
|
|
|
class UserRecord
|
|
|
|
{
|
|
|
|
use UserFields;
|
|
|
|
|
|
|
|
private array $props = [];
|
|
|
|
|
|
|
|
public function __get(string $field)
|
|
|
|
{
|
|
|
|
return $this->props[$field];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function __set(string $field, $value) : void
|
|
|
|
{
|
|
|
|
$this->props[$field] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property mixed $email
|
|
|
|
* @property mixed $password
|
|
|
|
* @property mixed $last_login_at
|
|
|
|
*/
|
|
|
|
trait UserFields {}
|
|
|
|
|
|
|
|
$record = new UserRecord();
|
|
|
|
$record->email;
|
|
|
|
$record->password;
|
2022-12-18 17:15:15 +01:00
|
|
|
$record->last_login_at = new DateTimeImmutable("now");',
|
2020-06-20 22:53:17 +02:00
|
|
|
],
|
2020-07-22 15:55:13 +02:00
|
|
|
'reconcileMagicProperties' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-07-22 15:55:13 +02:00
|
|
|
/**
|
|
|
|
* @property string|null $a A
|
|
|
|
* @property string|null $b B
|
|
|
|
*/
|
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
private array $props = [];
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->props["a"] = "hello";
|
|
|
|
$this->props["b"] = "goodbye";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-mutation-free
|
|
|
|
*/
|
|
|
|
public function __get(string $prop){
|
|
|
|
return $this->props[$prop] ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $b */
|
|
|
|
public function __set(string $a, $b){
|
|
|
|
$this->props[$a] = $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bar(): string {
|
|
|
|
if (is_null($this->a) || is_null($this->b)) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return $this->b;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "hello";
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-07-22 15:55:13 +02:00
|
|
|
],
|
2021-07-18 22:43:33 +02:00
|
|
|
'propertyReadIsExpanded' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-07-18 22:43:33 +02:00
|
|
|
/** @property self::TYPE_* $type */
|
|
|
|
class A {
|
|
|
|
public const TYPE_A = 1;
|
|
|
|
public const TYPE_B = 2;
|
|
|
|
|
|
|
|
public function __get(string $_prop) {}
|
|
|
|
/** @param mixed $_value */
|
|
|
|
public function __set(string $_prop, $_value) {}
|
|
|
|
}
|
|
|
|
$a = (new A)->type;
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$a===' => '1|2',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'propertyWriteIsExpanded' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-07-18 22:43:33 +02:00
|
|
|
/** @property self::TYPE_* $type */
|
|
|
|
class A {
|
|
|
|
public const TYPE_A = 1;
|
|
|
|
public const TYPE_B = 2;
|
|
|
|
|
|
|
|
public function __get(string $_prop) {}
|
|
|
|
/** @param mixed $_value */
|
|
|
|
public function __set(string $_prop, $_value) {}
|
|
|
|
}
|
|
|
|
$a = (new A);
|
|
|
|
$a->type = A::TYPE_B;
|
|
|
|
',
|
|
|
|
],
|
2022-11-24 19:31:53 +01:00
|
|
|
'impureMethodTest' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @property array<string, string> $errors
|
|
|
|
*
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
final class OrganizationObject {
|
|
|
|
|
|
|
|
public function __get(string $key)
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $a
|
|
|
|
*/
|
|
|
|
public function __set(string $key, $a): void
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updateErrors(): void {
|
|
|
|
/** @var array<string, string> */
|
|
|
|
$errors = [];
|
|
|
|
$this->errors = $errors;
|
|
|
|
}
|
|
|
|
/** @return array<string, string> */
|
|
|
|
public function updateStatus(): array {
|
|
|
|
$_ = $this->errors;
|
|
|
|
$this->updateErrors();
|
|
|
|
$errors = $this->errors;
|
|
|
|
return $errors;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
|
|
|
],
|
2018-05-09 02:25:39 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2018-05-09 02:25:39 +02:00
|
|
|
{
|
|
|
|
return [
|
2020-04-16 06:22:00 +02:00
|
|
|
'annotationWithoutGetter' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-04-16 06:22:00 +02:00
|
|
|
/**
|
|
|
|
* @property bool $is_protected
|
|
|
|
*/
|
|
|
|
final class Page {
|
|
|
|
public function isProtected(): bool
|
|
|
|
{
|
|
|
|
return $this->is_protected;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedThisPropertyFetch',
|
|
|
|
],
|
2018-05-09 02:25:39 +02:00
|
|
|
'propertyDocblockInvalidAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
/**
|
|
|
|
* @property string $foo
|
|
|
|
*/
|
|
|
|
class A {
|
2018-07-17 02:32:16 +02:00
|
|
|
public function __get(string $name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $name, $value): void {
|
|
|
|
}
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->foo = 5;',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
|
|
|
],
|
|
|
|
'propertyInvalidClassAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
namespace Bar;
|
|
|
|
|
|
|
|
class PropertyType {}
|
|
|
|
class SomeOtherPropertyType {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property PropertyType $foo
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
/** @param string $name */
|
|
|
|
public function __get($name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
2018-07-17 02:32:16 +02:00
|
|
|
|
|
|
|
return null;
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function __set($name, $value): void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->foo = new SomeOtherPropertyType();',
|
2019-02-27 22:00:44 +01:00
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue - src' . DIRECTORY_SEPARATOR . 'somefile.php:29:31 - $a->foo with declared type'
|
2018-05-09 02:25:39 +02:00
|
|
|
. ' \'Bar\PropertyType\' cannot',
|
|
|
|
],
|
|
|
|
'propertyWriteDocblockInvalidAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
/**
|
|
|
|
* @property-write string $foo
|
|
|
|
*/
|
|
|
|
class A {
|
2018-07-17 02:32:16 +02:00
|
|
|
public function __get(string $name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $name, $value): void {
|
|
|
|
}
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->foo = 5;',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
|
|
|
],
|
2020-03-24 23:32:57 +01:00
|
|
|
'psalmPropertyWriteDocblockInvalidAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-24 23:32:57 +01:00
|
|
|
/**
|
|
|
|
* @psalm-property-write string $foo
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public function __get(string $name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $name, $value): void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->foo = 5;',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
|
|
|
],
|
2018-05-09 02:25:39 +02:00
|
|
|
'propertySealedDocblockUndefinedPropertyAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
/**
|
|
|
|
* @property string $foo
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
class A {
|
2018-07-17 02:32:16 +02:00
|
|
|
public function __get(string $name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $name, $value): void {
|
|
|
|
}
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->bar = 5;',
|
2020-01-06 03:58:18 +01:00
|
|
|
'error_message' => 'UndefinedMagicPropertyAssignment',
|
2018-05-09 02:25:39 +02:00
|
|
|
],
|
|
|
|
'propertySealedDocblockDefinedPropertyAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
/**
|
|
|
|
* @property string $foo
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
class A {
|
2018-07-17 02:32:16 +02:00
|
|
|
public function __get(string $name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $name, $value): void {
|
|
|
|
}
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a->foo = 5;',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
|
|
|
],
|
|
|
|
'propertyReadInvalidFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
/**
|
|
|
|
* @property-read string $foo
|
|
|
|
*/
|
|
|
|
class A {
|
2018-07-17 02:32:16 +02:00
|
|
|
/** @return mixed */
|
|
|
|
public function __get(string $name) {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
echo count($a->foo);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2020-03-24 23:32:57 +01:00
|
|
|
'psalmPropertyReadInvalidFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-24 23:32:57 +01:00
|
|
|
/**
|
|
|
|
* @psalm-property-read string $foo
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
/** @return mixed */
|
|
|
|
public function __get(string $name) {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
echo count($a->foo);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2018-05-09 02:25:39 +02:00
|
|
|
'propertySealedDocblockUndefinedPropertyFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
/**
|
|
|
|
* @property string $foo
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
class A {
|
2018-07-17 02:32:16 +02:00
|
|
|
public function __get(string $name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $name, $value): void {
|
|
|
|
}
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
echo $a->bar;',
|
2020-01-06 03:58:18 +01:00
|
|
|
'error_message' => 'UndefinedMagicPropertyFetch',
|
2018-05-09 02:25:39 +02:00
|
|
|
],
|
|
|
|
/**
|
|
|
|
* The property $foo is not defined on the object, but accessed with the magic setter.
|
|
|
|
* This is an error because `@psalm-seal-properties` is specified on the class block.
|
|
|
|
*/
|
|
|
|
'magicSetterUndefinedProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
/**
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public function __get(string $name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
2018-07-17 02:32:16 +02:00
|
|
|
|
|
|
|
return null;
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $name, $value): void {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function badSet(): void {
|
|
|
|
$this->__set("foo", "value");
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedThisPropertyAssignment',
|
|
|
|
],
|
|
|
|
/**
|
|
|
|
* The property $foo is not defined on the object, but accessed with the magic getter.
|
|
|
|
* This is an error because `@psalm-seal-properties` is specified on the class block.
|
|
|
|
*/
|
|
|
|
'magicGetterUndefinedProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
/**
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public function __get(string $name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
2018-07-17 02:32:16 +02:00
|
|
|
|
|
|
|
return null;
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $name, $value): void {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function badGet(): void {
|
|
|
|
$this->__get("foo");
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedThisPropertyFetch',
|
|
|
|
],
|
|
|
|
/**
|
|
|
|
* The property $foo is defined as a string with the `@property` annotation, but
|
|
|
|
* the magic setter is used to set it to an object.
|
|
|
|
*/
|
|
|
|
'magicSetterInvalidAssignmentType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
/**
|
|
|
|
* @property string $foo
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public function __get(string $name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
2018-07-17 02:32:16 +02:00
|
|
|
|
|
|
|
return null;
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $name, $value): void {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function badSet(): void {
|
|
|
|
$this->__set("foo", new stdClass());
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
|
|
|
],
|
|
|
|
'propertyDocblockAssignmentToMixed' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 02:25:39 +02:00
|
|
|
/**
|
|
|
|
* @property string $foo
|
|
|
|
*/
|
|
|
|
class A {
|
2018-07-17 02:32:16 +02:00
|
|
|
public function __get(string $name): ?string {
|
|
|
|
if ($name === "foo") {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $name, $value): void {
|
|
|
|
}
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @param mixed $b */
|
|
|
|
function foo($b) : void {
|
|
|
|
$a = new A();
|
|
|
|
$a->__set("foo", $b);
|
|
|
|
}',
|
2019-04-26 00:02:19 +02:00
|
|
|
'error_message' => 'MixedPropertyTypeCoercion',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedAssignment'],
|
2018-05-09 02:25:39 +02:00
|
|
|
],
|
2019-01-23 22:58:50 +01:00
|
|
|
'magicInterfacePropertyWrongProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-23 22:58:50 +01:00
|
|
|
/**
|
|
|
|
* @property-read string $foo
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
interface GetterSetter {
|
|
|
|
/** @return mixed */
|
|
|
|
public function __get(string $key);
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $key, $value) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress NoInterfaceProperties */
|
|
|
|
function getBar(GetterSetter $o) : string {
|
|
|
|
return $o->bar;
|
|
|
|
}',
|
2020-01-06 03:07:26 +01:00
|
|
|
'error_message' => 'UndefinedMagicPropertyFetch',
|
2019-01-23 22:58:50 +01:00
|
|
|
],
|
2020-03-24 23:32:57 +01:00
|
|
|
'psalmMagicInterfacePropertyWrongProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-24 23:32:57 +01:00
|
|
|
/**
|
|
|
|
* @psalm-property-read string $foo
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
interface GetterSetter {
|
|
|
|
/** @return mixed */
|
|
|
|
public function __get(string $key);
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $key, $value) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress NoInterfaceProperties */
|
|
|
|
function getBar(GetterSetter $o) : string {
|
|
|
|
return $o->bar;
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedMagicPropertyFetch',
|
|
|
|
],
|
2019-01-23 22:58:50 +01:00
|
|
|
'magicInterfaceWrongPropertyWrite' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-23 22:58:50 +01:00
|
|
|
/**
|
|
|
|
* @property-write string $foo
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
interface GetterSetter {
|
|
|
|
/** @return mixed */
|
|
|
|
public function __get(string $key);
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $key, $value) : void;
|
|
|
|
}
|
|
|
|
|
2020-03-24 23:32:57 +01:00
|
|
|
/** @psalm-suppress NoInterfaceProperties */
|
|
|
|
function getFoo(GetterSetter $o) : void {
|
|
|
|
$o->bar = "hello";
|
|
|
|
}',
|
|
|
|
'error_message' => 'UndefinedMagicPropertyAssignment',
|
|
|
|
],
|
|
|
|
'psalmMagicInterfaceWrongPropertyWrite' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-24 23:32:57 +01:00
|
|
|
/**
|
|
|
|
* @psalm-property-write string $foo
|
|
|
|
* @psalm-seal-properties
|
|
|
|
*/
|
|
|
|
interface GetterSetter {
|
|
|
|
/** @return mixed */
|
|
|
|
public function __get(string $key);
|
|
|
|
/** @param mixed $value */
|
|
|
|
public function __set(string $key, $value) : void;
|
|
|
|
}
|
|
|
|
|
2019-01-23 22:58:50 +01:00
|
|
|
/** @psalm-suppress NoInterfaceProperties */
|
|
|
|
function getFoo(GetterSetter $o) : void {
|
|
|
|
$o->bar = "hello";
|
|
|
|
}',
|
2020-01-06 03:07:26 +01:00
|
|
|
'error_message' => 'UndefinedMagicPropertyAssignment',
|
2019-01-23 22:58:50 +01:00
|
|
|
],
|
2021-08-10 14:57:58 +02:00
|
|
|
'propertyDocblockOnProperty' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-12-04 21:55:53 +01:00
|
|
|
class A {
|
|
|
|
/** @property string[] */
|
|
|
|
public array $arr;
|
2021-08-10 14:57:58 +02:00
|
|
|
}',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'InvalidDocblock',
|
2021-08-10 14:57:58 +02:00
|
|
|
],
|
2018-05-09 02:25:39 +02:00
|
|
|
];
|
|
|
|
}
|
2021-12-30 11:23:04 +01:00
|
|
|
|
|
|
|
public function testSealAllMethodsWithoutFoo(): void
|
|
|
|
{
|
|
|
|
Config::getInstance()->seal_all_properties = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function __get(string $name) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
$b = new B();
|
|
|
|
$result = $b->foo;
|
2022-12-18 17:15:15 +01:00
|
|
|
',
|
2021-12-30 11:23:04 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$error_message = 'UndefinedMagicPropertyFetch';
|
|
|
|
$this->expectException(CodeException::class);
|
|
|
|
$this->expectExceptionMessage($error_message);
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
2018-05-09 02:25:39 +02:00
|
|
|
}
|