Skip to content

Commit

Permalink
Fix inaccurate types in phpunit detected by etsy/phan
Browse files Browse the repository at this point in the history
- Fix incorrect phpdoc types which stand out.
- Fix bug in arrayHasKey shorthand
- nit: preg_match returns an int in normal case.
  (Safe to change, this method is private)
- getBooleanAnnotationSetting returns null(void) if there's no match
  (`return;`)
- A few exception types weren't being imported, so they wouldn't be caught.
- Document getBoolean, seems to sometime allow string defaults.
  • Loading branch information
TysonAndre authored and sebastianbergmann committed Apr 18, 2017
1 parent 3282308 commit 48f76fc
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Framework/Assert/Functions.php
Expand Up @@ -72,7 +72,7 @@ function anything()
*/
function arrayHasKey($key)
{
Assert::arrayHasKey(...\func_get_args());
return Assert::arrayHasKey(...\func_get_args());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Framework/Constraint/Count.php
Expand Up @@ -44,9 +44,9 @@ protected function matches($other)
}

/**
* @param mixed $other
* @param \Countable|\Traversable\|array $other
*
* @return bool
* @return int
*/
protected function getCountOf($other)
{
Expand Down
Expand Up @@ -14,7 +14,7 @@
class ExceptionMessageRegularExpression extends Constraint
{
/**
* @var int
* @var string
*/
protected $expectedMessageRegExp;

Expand Down
2 changes: 1 addition & 1 deletion src/Framework/Constraint/SameSize.php
Expand Up @@ -17,7 +17,7 @@ class SameSize extends Count
protected $expectedCount;

/**
* @param int $expected
* @param \Countable|\Traversable\|array $expected
*/
public function __construct($expected)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Framework/TestResult.php
Expand Up @@ -14,8 +14,10 @@
use Countable;
use Error;
use PHP_Invoker;
use PHP_Invoker_TimeoutException;
use PHP_Timer;
use PHPUnit_Framework_MockObject_Exception;
use PHPUnit_Framework_RiskyTestError;
use PHPUnit\Util\Blacklist;
use PHPUnit\Util\InvalidArgumentHelper;
use PHPUnit\Util\Printer;
Expand Down
11 changes: 7 additions & 4 deletions src/Util/Configuration.php
Expand Up @@ -1016,10 +1016,13 @@ protected function getTestSuite(DOMElement $testSuiteNode, $testSuiteFilter = nu
}

/**
* if $value is 'false' or 'true', this returns the value that $value represents.
* Otherwise, returns $default, which may be a string in rare cases.
* See PHPUnit\Util\ConfigurationTest::testPHPConfigurationIsReadCorrectly
* @param string $value
* @param bool $default
* @param string|bool $default
*
* @return bool
* @return string|bool
*/
protected function getBoolean($value, $default)
{
Expand All @@ -1036,9 +1039,9 @@ protected function getBoolean($value, $default)

/**
* @param string $value
* @param bool $default
* @param int $default
*
* @return bool
* @return int
*/
protected function getInteger($value, $default)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Util/Filter.php
Expand Up @@ -20,7 +20,7 @@ class Filter
/**
* Filters stack frames from PHPUnit classes.
*
* @param Exception $e
* @param \Throwable $e
* @param bool $asString
*
* @return string
Expand Down
2 changes: 1 addition & 1 deletion src/Util/Log/JUnit.php
Expand Up @@ -94,7 +94,7 @@ class JUnit extends Printer implements TestListener
protected $testSuiteLevel = 0;

/**
* @var DOMElement
* @var ?DOMElement
*/
protected $currentTestCase;

Expand Down
2 changes: 1 addition & 1 deletion src/Util/Log/TeamCity.php
Expand Up @@ -42,7 +42,7 @@ class TeamCity extends ResultPrinter
private $startedTestName;

/**
* @var string
* @var int|false
*/
private $flowId;

Expand Down
14 changes: 7 additions & 7 deletions src/Util/Test.php
Expand Up @@ -347,7 +347,7 @@ public static function getMissingRequirements($className, $methodName)
* @param string $className
* @param string $methodName
*
* @return array
* @return array|false
*/
public static function getExpectedException($className, $methodName)
{
Expand Down Expand Up @@ -742,7 +742,7 @@ public static function getDependencies($className, $methodName)
* @param string $className
* @param string $methodName
*
* @return bool
* @return ?bool
*/
public static function getErrorHandlerSettings($className, $methodName)
{
Expand Down Expand Up @@ -862,7 +862,7 @@ public static function getProcessIsolationSettings($className, $methodName)
* @param string $className
* @param string $methodName
*
* @return bool
* @return ?bool
*/
public static function getPreserveGlobalStateSettings($className, $methodName)
{
Expand Down Expand Up @@ -938,7 +938,7 @@ private static function emptyHookMethodsArray()
* @param string $methodName
* @param string $settingName
*
* @return bool
* @return ?bool
*/
private static function getBooleanAnnotationSetting($className, $methodName, $settingName)
{
Expand Down Expand Up @@ -967,7 +967,7 @@ private static function getBooleanAnnotationSetting($className, $methodName, $se
}
}

return;
return null;
}

/**
Expand Down Expand Up @@ -1131,7 +1131,7 @@ private static function isBeforeClassMethod(ReflectionMethod $method)
*/
private static function isBeforeMethod(ReflectionMethod $method)
{
return \preg_match('/@before\b/', $method->getDocComment());
return \preg_match('/@before\b/', $method->getDocComment()) > 0;
}

/**
Expand All @@ -1151,7 +1151,7 @@ private static function isAfterClassMethod(ReflectionMethod $method)
*/
private static function isAfterMethod(ReflectionMethod $method)
{
return \preg_match('/@after\b/', $method->getDocComment());
return \preg_match('/@after\b/', $method->getDocComment()) > 0;
}

/**
Expand Down

0 comments on commit 48f76fc

Please sign in to comment.