Skip to content

Commit

Permalink
Regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 4, 2021
1 parent 4a3ffab commit 3c3ea2f
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,19 @@ public function testBug3297(): void
$this->analyse([__DIR__ . '/data/bug-3297.php'], []);
}

public function testBug4829(): void
{
$this->analyse([__DIR__ . '/data/bug-4829.php'], []);
}

public function testBug3784(): void
{
$this->analyse([__DIR__ . '/data/bug-3784.php'], []);
}

public function testBug3700(): void
{
$this->analyse([__DIR__ . '/data/bug-3700.php'], []);
}

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

namespace Bug3700;

class HelloWorld
{
public function errorWithExtension(): void
{
$filePath = 'somefile.txt';
$pathInfo = pathinfo($filePath);
$extension = empty($pathInfo['extension']) ? '' : '.' . $pathInfo['extension'];
}
}
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-3784.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Bug3784;

class HelloWorld
{
public function sayHello(string $value): void
{
$parsedValue = parse_url($value);

if (false === $parsedValue) {
return;
}

foreach (['host'] as $key) {
if (empty($parsedValue[$key])) {
return;
}
}

var_dump($parsedValue['host']);
}
}
31 changes: 31 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-4829.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Bug4829;

class Test
{
// PHPStan 0.12.83 level 7 error:
// 26 Offset 'cat3' does not exist on array('cat1' => 'blah1', 'cat2' => 'blah2', ?'cat3' => 'blah3').
public function stan() : void
{
$ret = [];

// if $result has 1 element, the error does not occur
// if $result is [], the error occurs
$result = ["val1", "val2"];

foreach ($result as $val) {
// if I replace $val with a string, the error does not occur
//$val = "test";

// if I remove one assignment, the error does not occur
$ret[$val]['cat1'] = "blah1";
$ret[$val]['cat2'] = "blah2";
$ret[$val]['cat3'] = 'blah3';

$t1 = $ret[$val]['cat1'];
$t2 = $ret[$val]['cat2'];
$t3 = $ret[$val]['cat3']; // error occurs here
}
}
}
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Cast/InvalidCastRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public function testRule(): void
]);
}

public function testBug5162(): void
{
$this->analyse([__DIR__ . '/data/bug-5162.php'], []);
}

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

namespace Bug5162;

use function PHPStan\Testing\assertType;

class Foo
{


/**
* @return array<string,string|array<string>>
*/
function Get()
{
switch ( rand(1,3) )
{
case 1:
return ['a' => 'val'];
case 2:
return ['a' => '1'];
case 3:
return ['a' => []];
}
return [];
}

public function doFoo(): void
{
// This variant works
$result1 = $this->Get();
if ( ! array_key_exists('a', $result1))
{
exit(1);
}
if ( ! is_numeric( $result1['a'] ) )
{
exit(1);
}
$val = (float) $result1['a'];
}

public function doBar(): void
{
// This variant doesn't work .. but is logically identical
$result2 = $this->Get();
if ( array_key_exists('a',$result2) && ! is_numeric( $result2['a'] ) )
{
exit(1);
}
if ( ! array_key_exists('a', $result2) )
{
exit(1);
}
$val = (float) $result2['a'];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -456,4 +456,15 @@ public function testBug3357(): void
$this->analyse([__DIR__ . '/data/bug-3357.php'], []);
}

public function testBug4848(): void
{
$this->checkAlwaysTrueStrictComparison = true;
$this->analyse([__DIR__ . '/data/bug-4848.php'], [
[
'Strict comparison using === between \'18446744073709551615\' and \'9223372036854775807\' will always evaluate to false.',
7,
],
]);
}

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

namespace Bug4848;

function (): void {
$maxBigintInMysql = '18446744073709551615';
if ($maxBigintInMysql === (string)(int)$maxBigintInMysql) {
echo 'same: ' . (string)(int)$maxBigintInMysql;
} else {
echo 'different: ' .(string)(int)$maxBigintInMysql;
}
};
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1979,4 +1979,12 @@ public function testBug5253(): void
$this->analyse([__DIR__ . '/data/bug-5253.php'], []);
}

public function testBug4844(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/bug-4844.php'], []);
}

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

namespace Bug4844;

class HelloWorld
{
/**
* Updates a attribute and saves the record.
*
* @param string $name
* @param string $value
*
* @return bool
*/
public function update_attribute($name, $value = '')
{
return $this->update_attributes([
$name => $value,
]);
}

/**
* Updates as an array given attributes and saves the record.
*
* @param non-empty-array<string, mixed> $attributes
*
* @return bool
*/
public function update_attributes($attributes)
{
return true;
}
}

0 comments on commit 3c3ea2f

Please sign in to comment.