2016-12-06 19:49:57 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class SwitchTypeTest extends TestCase
|
2016-12-06 19:49:57 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2016-12-06 19:49:57 +01:00
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2016-12-06 19:49:57 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerValidCodeParse()
|
2016-12-06 19:49:57 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'getClassArg' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function fooFoo() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function barBar() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 10) ? new A() : new B();
|
|
|
|
|
|
|
|
switch (get_class($a)) {
|
|
|
|
case "A":
|
|
|
|
$a->fooFoo();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "B":
|
|
|
|
$a->barBar();
|
|
|
|
break;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'getTypeArg' => [
|
|
|
|
'<?php
|
|
|
|
function testInt(int $var) : void {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testString(string $var) : void {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 10) ? 1 : "two";
|
|
|
|
|
|
|
|
switch (gettype($a)) {
|
|
|
|
case "string":
|
|
|
|
testString($a);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "int":
|
|
|
|
testInt($a);
|
|
|
|
break;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2016-12-06 19:49:57 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-01-13 20:07:23 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
2016-12-06 19:49:57 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'getClassArgWrongClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 10) ? new A() : new B();
|
|
|
|
|
|
|
|
switch (get_class($a)) {
|
|
|
|
case "A":
|
|
|
|
$a->barBar();
|
|
|
|
break;
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UndefinedMethod',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'getTypeArgWrongArgs' => [
|
|
|
|
'<?php
|
|
|
|
function testInt(int $var) : void {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testString(string $var) : void {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 10) ? 1 : "two";
|
|
|
|
|
|
|
|
switch (gettype($a)) {
|
|
|
|
case "string":
|
|
|
|
testInt($a);
|
|
|
|
|
|
|
|
case "int":
|
|
|
|
testString($a);
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2016-12-06 19:49:57 +01:00
|
|
|
}
|
|
|
|
}
|