2019-05-10 00:33:27 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
class PsalmInternalAnnotationTest extends TestCase
|
|
|
|
{
|
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
|
|
|
*/
|
|
|
|
public function providerValidCodeParse()
|
|
|
|
{
|
|
|
|
return [
|
2019-05-11 16:15:50 +02:00
|
|
|
'internalMethodWithCall' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
class Foo {
|
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-11 16:15:50 +02:00
|
|
|
* @psalm-internal A\B
|
|
|
|
*/
|
|
|
|
public static function barBar(): void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace A\B\C {
|
|
|
|
class Bat {
|
|
|
|
public function batBat() : void {
|
|
|
|
\A\B\Foo::barBar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-05-14 23:30:16 +02:00
|
|
|
'internalMethodWithCallWithCaseMisMatched' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
class Foo {
|
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-14 23:30:16 +02:00
|
|
|
* @psalm-internal A\B
|
|
|
|
*/
|
|
|
|
public static function barBar(): void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace a\b\c {
|
|
|
|
class Bat {
|
|
|
|
public function batBat() : void {
|
|
|
|
\A\B\Foo::barBar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'internalToClassMethodWithCall' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
class Foo {
|
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-14 23:30:16 +02:00
|
|
|
* @psalm-internal A\B\Foo
|
|
|
|
*/
|
|
|
|
public static function barBar(): void {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function foo(): void {
|
|
|
|
self::barBar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-05-11 15:58:53 +02:00
|
|
|
'internalClassWithStaticCall' => [
|
|
|
|
'<?php
|
2019-05-11 17:55:37 +02:00
|
|
|
namespace A\B {
|
2019-05-11 15:58:53 +02:00
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-11 17:55:37 +02:00
|
|
|
* @psalm-internal A
|
2019-05-11 15:58:53 +02:00
|
|
|
*/
|
|
|
|
class Foo {
|
|
|
|
public static function barBar(): void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 17:55:37 +02:00
|
|
|
namespace A\B\C {
|
2019-05-11 15:58:53 +02:00
|
|
|
class Bat {
|
|
|
|
public function batBat() : void {
|
2019-05-11 17:55:37 +02:00
|
|
|
\A\B\Foo::barBar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'internalClassExtendingNamespaceWithStaticCall' => [
|
|
|
|
'<?php
|
|
|
|
namespace A {
|
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-11 17:55:37 +02:00
|
|
|
* @psalm-internal A
|
|
|
|
*/
|
|
|
|
class Foo extends \B\Foo {
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
public static function barBar(): void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace B {
|
|
|
|
class Foo {
|
|
|
|
public function __construct() {
|
|
|
|
static::barBar();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function barBar(): void {
|
2019-05-11 15:58:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-05-11 16:23:49 +02:00
|
|
|
'internalClassWithNew' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-11 16:23:49 +02:00
|
|
|
* @psalm-internal A\B
|
|
|
|
*/
|
|
|
|
class Foo { }
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace A\B\C {
|
|
|
|
class Bat {
|
|
|
|
public function batBat() : void {
|
|
|
|
$a = new \A\B\Foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-05-10 00:33:27 +02:00
|
|
|
'internalClassWithExtends' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-10 00:33:27 +02:00
|
|
|
* @psalm-internal A\B
|
|
|
|
*/
|
|
|
|
class Foo { }
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace A\B\C {
|
|
|
|
class Bar extends \A\B\Foo {}
|
|
|
|
}',
|
|
|
|
],
|
2019-05-11 16:45:25 +02:00
|
|
|
'internalPropertyGet' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
class Foo {
|
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-11 16:45:25 +02:00
|
|
|
* @psalm-internal A\B
|
|
|
|
* @var ?int
|
|
|
|
*/
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace A\B\C {
|
|
|
|
class Bat {
|
|
|
|
public function batBat() : void {
|
|
|
|
echo (new \A\B\Foo)->foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-05-11 16:50:11 +02:00
|
|
|
'internalPropertySet' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
class Foo {
|
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-11 16:50:11 +02:00
|
|
|
* @psalm-internal A\B
|
|
|
|
* @var ?int
|
|
|
|
*/
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
namespace A\B\C {
|
|
|
|
class Bat {
|
|
|
|
public function batBat() : void {
|
|
|
|
$a = new \A\B\Foo;
|
|
|
|
$a->foo = 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-05-11 17:55:37 +02:00
|
|
|
'internalMethodInTraitWithCall' => [
|
|
|
|
'<?php
|
|
|
|
namespace A {
|
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-11 17:55:37 +02:00
|
|
|
* @psalm-internal A
|
|
|
|
*/
|
|
|
|
trait T {
|
|
|
|
public static function barBar(): void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Foo {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace B {
|
|
|
|
class Bat {
|
|
|
|
public function batBat() : void {
|
|
|
|
\A\Foo::barBar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-05-10 00:33:27 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
|
|
|
*/
|
|
|
|
public function providerInvalidCodeParse()
|
|
|
|
{
|
|
|
|
return [
|
2019-05-11 16:15:50 +02:00
|
|
|
'internalMethodWithCall' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
class Foo {
|
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-11 16:15:50 +02:00
|
|
|
* @psalm-internal A\B
|
|
|
|
*/
|
|
|
|
public static function barBar(): void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace A\C {
|
|
|
|
class Bat {
|
|
|
|
public function batBat(): void {
|
|
|
|
\A\B\Foo::barBar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'The method A\B\Foo::barBar has been marked as internal to A\B',
|
|
|
|
],
|
2019-05-14 23:30:16 +02:00
|
|
|
'internalToClassMethodWithCall' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
class Foo {
|
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-14 23:30:16 +02:00
|
|
|
* @psalm-internal A\B\Foo
|
|
|
|
*/
|
|
|
|
public static function barBar(): void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace A\C {
|
|
|
|
class Bat {
|
|
|
|
public function batBat(): void {
|
|
|
|
\A\B\Foo::barBar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'The method A\B\Foo::barBar has been marked as internal to A\B\Foo',
|
|
|
|
],
|
2019-05-11 15:58:53 +02:00
|
|
|
'internalClassWithStaticCall' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-11 15:58:53 +02:00
|
|
|
* @psalm-internal A\B
|
|
|
|
*/
|
|
|
|
class Foo {
|
|
|
|
public static function barBar(): void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace A\C {
|
|
|
|
class Bat {
|
|
|
|
public function batBat(): void {
|
|
|
|
\A\B\Foo::barBar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InternalClass',
|
|
|
|
],
|
2019-05-11 16:23:49 +02:00
|
|
|
'internalClassWithNew' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-11 16:23:49 +02:00
|
|
|
* @psalm-internal A\B
|
|
|
|
*/
|
|
|
|
class Foo { }
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace A\C {
|
|
|
|
class Bat {
|
|
|
|
public function batBat(): void {
|
|
|
|
$a = new \A\B\Foo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InternalClass',
|
|
|
|
],
|
2019-05-10 00:33:27 +02:00
|
|
|
'internalClassWithExtends' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-10 00:33:27 +02:00
|
|
|
* @psalm-internal A\B
|
|
|
|
*/
|
|
|
|
class Foo { }
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace A\C {
|
|
|
|
class Bar extends \A\B\Foo {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'A\B\Foo is internal to A\B',
|
|
|
|
],
|
2019-05-11 16:45:25 +02:00
|
|
|
'internalPropertyGet' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
class Foo {
|
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-11 16:45:25 +02:00
|
|
|
* @psalm-internal A\B
|
|
|
|
* @var ?int
|
|
|
|
*/
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace A\C {
|
|
|
|
class Bat {
|
|
|
|
public function batBat() : void {
|
|
|
|
echo (new \A\B\Foo)->foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'A\B\Foo::$foo is marked internal to A\B',
|
|
|
|
],
|
2019-05-11 16:50:11 +02:00
|
|
|
'internalPropertySet' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
class Foo {
|
|
|
|
/**
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-11 16:50:11 +02:00
|
|
|
* @psalm-internal A\B
|
|
|
|
* @var ?int
|
|
|
|
*/
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
namespace A\C {
|
|
|
|
class Bat {
|
|
|
|
public function batBat() : void {
|
|
|
|
$a = new \A\B\Foo;
|
|
|
|
$a->foo = 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'A\B\Foo::$foo is marked internal to A\B',
|
|
|
|
],
|
2019-05-11 17:55:37 +02:00
|
|
|
'internalClassMissingNamespace' => [
|
|
|
|
'<?php
|
|
|
|
|
2019-05-15 00:27:27 +02:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
* @psalm-internal
|
|
|
|
*/
|
2019-05-11 17:55:37 +02:00
|
|
|
class Foo {}
|
|
|
|
|
|
|
|
',
|
|
|
|
'error_message' => 'psalm-internal annotation used without specifying namespace',
|
|
|
|
],
|
|
|
|
'internalPropertyMissingNamespace' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
/**
|
|
|
|
* @var int
|
2019-05-15 00:27:27 +02:00
|
|
|
* @internal
|
2019-05-11 17:55:37 +02:00
|
|
|
* @psalm-internal
|
|
|
|
*/
|
|
|
|
var $bar;
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'psalm-internal annotation used without specifying namespace',
|
|
|
|
],
|
|
|
|
'internalMethodMissingNamespace' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
2019-05-15 00:27:27 +02:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
* @psalm-internal
|
|
|
|
*/
|
2019-05-11 17:55:37 +02:00
|
|
|
function Bar(): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
',
|
|
|
|
'error_message' => 'psalm-internal annotation used without specifying namespace',
|
|
|
|
],
|
2019-05-15 00:27:27 +02:00
|
|
|
'internalClassMissingInternalAnnotation' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
/**
|
|
|
|
* @psalm-internal A\B
|
|
|
|
*/
|
|
|
|
class Foo { }
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'psalm-internal annotation used without @internal',
|
|
|
|
],
|
|
|
|
'internalPropertyMissingInternalAnnotation' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
class Foo {
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
* @psalm-internal A\B
|
|
|
|
*/
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'psalm-internal annotation used without @internal',
|
|
|
|
],
|
|
|
|
'internalFunctionMissingInternalAnnotation' => [
|
|
|
|
'<?php
|
|
|
|
namespace A\B {
|
|
|
|
class Foo {
|
|
|
|
/**
|
|
|
|
* @psalm-internal A\B
|
|
|
|
*/
|
|
|
|
public function foo()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'psalm-internal annotation used without @internal',
|
|
|
|
],
|
2019-05-10 00:33:27 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|