Fix code pointed by Psalm

This commit is contained in:
Meritoo
2019-05-05 09:49:03 +02:00
parent 421d19ff10
commit dd5ac0f7e6
33 changed files with 1085 additions and 524 deletions

View File

@@ -10,11 +10,13 @@ namespace Meritoo\Common\Traits\Test\Base;
use DateTime;
use Generator;
use Meritoo\Common\Exception\Reflection\ClassWithoutConstructorException;
use Meritoo\Common\Exception\Type\UnknownOopVisibilityTypeException;
use Meritoo\Common\Type\OopVisibilityType;
use Meritoo\Common\Utilities\Miscellaneous;
use ReflectionClass;
use ReflectionMethod;
use RuntimeException;
use stdClass;
/**
@@ -37,7 +39,7 @@ trait BaseTestCaseTrait
*
* @return Generator
*/
public function provideEmptyValue()
public function provideEmptyValue(): ?Generator
{
yield[''];
yield[' '];
@@ -52,7 +54,7 @@ trait BaseTestCaseTrait
*
* @return Generator
*/
public function provideEmptyScalarValue()
public function provideEmptyScalarValue(): ?Generator
{
yield[''];
yield[' '];
@@ -66,7 +68,7 @@ trait BaseTestCaseTrait
*
* @return Generator
*/
public function provideBooleanValue()
public function provideBooleanValue(): ?Generator
{
yield[false];
yield[true];
@@ -77,7 +79,7 @@ trait BaseTestCaseTrait
*
* @return Generator
*/
public function provideDateTimeInstance()
public function provideDateTimeInstance(): ?Generator
{
yield[new DateTime()];
yield[new DateTime('yesterday')];
@@ -90,7 +92,7 @@ trait BaseTestCaseTrait
*
* @return Generator
*/
public function provideDateTimeRelativeFormat()
public function provideDateTimeRelativeFormat(): ?Generator
{
yield['now'];
yield['yesterday'];
@@ -110,7 +112,7 @@ trait BaseTestCaseTrait
*
* @return Generator
*/
public function provideNotExistingFilePath()
public function provideNotExistingFilePath(): ?Generator
{
yield['lets-test.doc'];
yield['lorem/ipsum.jpg'];
@@ -122,7 +124,7 @@ trait BaseTestCaseTrait
*
* @return Generator
*/
public function provideNonScalarValue()
public function provideNonScalarValue(): ?Generator
{
yield[
[],
@@ -145,7 +147,7 @@ trait BaseTestCaseTrait
* @param string $directoryPath (optional) Path of directory containing the file
* @return string
*/
public function getFilePathForTesting($fileName, $directoryPath = '')
public function getFilePathForTesting(string $fileName, string $directoryPath = ''): string
{
$rootPath = Miscellaneous::getProjectRootPath();
@@ -162,33 +164,34 @@ trait BaseTestCaseTrait
/**
* Verifies visibility and arguments of method
*
* @param string $classNamespace Namespace of class that contains method to verify
* @param ReflectionMethod|string $method Name of method or just the method to verify
* @param string $visibilityType Expected visibility of verified method. One of
* OopVisibilityType class constants.
* @param int $argumentsCount (optional) Expected count/amount of arguments of the
* verified method
* @param int $requiredArgumentsCount (optional) Expected count/amount of required arguments
* of the verified method
* @param string $className Fully-qualified name of class that contains method to verify
* @param ReflectionMethod $method Name of method or just the method to verify
* @param string $visibilityType Expected visibility of verified method. One of OopVisibilityType
* class constants.
* @param int $argumentsCount (optional) Expected count/amount of arguments of the verified
* method
* @param int $requiredArgumentsCount (optional) Expected count/amount of required arguments of the
* verified method
* @throws UnknownOopVisibilityTypeException
* @throws RuntimeException
*
* Attention. 2nd argument, the $method, may be:
* - string - name of the method
* - instance of ReflectionMethod - just the method (provided by ReflectionClass::getMethod() method)
*/
protected static function assertMethodVisibilityAndArguments(
$classNamespace,
$method,
$visibilityType,
$argumentsCount = 0,
$requiredArgumentsCount = 0
) {
string $className,
ReflectionMethod $method,
string $visibilityType,
int $argumentsCount = 0,
int $requiredArgumentsCount = 0
): void {
// Type of visibility is not correct?
if (!(new OopVisibilityType())->isCorrectType($visibilityType)) {
throw new UnknownOopVisibilityTypeException($visibilityType);
throw UnknownOopVisibilityTypeException::createException($visibilityType);
}
$reflection = new ReflectionClass($classNamespace);
$reflection = new ReflectionClass($className);
// Name of method provided only?
// Let's find instance of the method (based on reflection)
@@ -218,24 +221,29 @@ trait BaseTestCaseTrait
/**
* Verifies visibility and arguments of class constructor
*
* @param string $classNamespace Namespace of class that contains constructor to verify
* @param string $className Fully-qualified name of class that contains constructor to verify
* @param string $visibilityType Expected visibility of verified method. One of OopVisibilityType class
* constants.
* @param int $argumentsCount (optional) Expected count/amount of arguments of the verified method
* @param int $requiredArgumentsCount (optional) Expected count/amount of required arguments of the verified
* method
* @throws ClassWithoutConstructorException
*/
protected static function assertConstructorVisibilityAndArguments(
$classNamespace,
$visibilityType,
$argumentsCount = 0,
$requiredArgumentsCount = 0
) {
$reflection = new ReflectionClass($classNamespace);
string $className,
string $visibilityType,
int $argumentsCount = 0,
int $requiredArgumentsCount = 0
): void {
$reflection = new ReflectionClass($className);
$method = $reflection->getConstructor();
if (null === $method) {
throw ClassWithoutConstructorException::create($className);
}
static::assertMethodVisibilityAndArguments(
$classNamespace,
$className,
$method,
$visibilityType,
$argumentsCount,
@@ -246,11 +254,11 @@ trait BaseTestCaseTrait
/**
* Asserts that class with given namespace has no constructor
*
* @param string $classNamespace Namespace of class that contains constructor to verify
* @param string $className Fully-qualified name of class that contains constructor to verify
*/
protected static function assertHasNoConstructor($classNamespace)
protected static function assertHasNoConstructor(string $className): void
{
$reflection = new ReflectionClass($classNamespace);
$reflection = new ReflectionClass($className);
$constructor = $reflection->getConstructor();
static::assertNull($constructor);
@@ -261,7 +269,7 @@ trait BaseTestCaseTrait
*
* @param string $testsDataDirPath Path of directory with data used by test cases
*/
protected static function setTestsDataDirPath($testsDataDirPath)
protected static function setTestsDataDirPath(string $testsDataDirPath): void
{
static::$testsDataDirPath = $testsDataDirPath;
}

View File

@@ -22,7 +22,7 @@ trait BaseTypeTestCaseTrait
/**
* Verifies availability of all types
*/
public function testAvailabilityOfAllTypes()
public function testAvailabilityOfAllTypes(): void
{
$available = $this->getTestedTypeInstance()->getAll();
$all = $this->getAllExpectedTypes();
@@ -33,12 +33,12 @@ trait BaseTypeTestCaseTrait
/**
* Verifies whether given type is correct or not
*
* @param string $type Type to verify
* @param bool $expected Information if given type is correct or not
* @param null|string $type Type to verify
* @param bool $expected Information if given type is correct or not
*
* @dataProvider provideTypeToVerify
*/
public function testIfGivenTypeIsCorrect($type, $expected)
public function testIfGivenTypeIsCorrect(?string $type, bool $expected): void
{
static::assertEquals($expected, $this->getTestedTypeInstance()->isCorrectType($type));
}
@@ -48,19 +48,19 @@ trait BaseTypeTestCaseTrait
*
* @return Generator
*/
abstract public function provideTypeToVerify();
abstract public function provideTypeToVerify(): Generator;
/**
* Returns instance of the tested type
*
* @return BaseType
*/
abstract protected function getTestedTypeInstance();
abstract protected function getTestedTypeInstance(): BaseType;
/**
* Returns all expected types of the tested type
*
* @return array
*/
abstract protected function getAllExpectedTypes();
abstract protected function getAllExpectedTypes(): array;
}

View File

@@ -8,6 +8,8 @@
namespace Meritoo\Common\Traits\ValueObject;
use DateTime;
/**
* Methods and properties related to human
*
@@ -33,26 +35,26 @@ trait HumanTrait
/**
* Email address
*
* @var string
* @var null|string
*/
protected $email;
/**
* Birth date
*
* @var \DateTime
* @var null|DateTime
*/
protected $birthDate;
/**
* Class constructor
*
* @param string $firstName First name
* @param string $lastName Last name
* @param string $email (optional) Email address
* @param \DateTime $birthDate (optional) Birth date
* @param string $firstName First name
* @param string $lastName Last name
* @param null|string $email (optional) Email address. Default: null.
* @param null|DateTime $birthDate (optional) Birth date. Default: null.
*/
public function __construct($firstName, $lastName, $email = null, \DateTime $birthDate = null)
public function __construct(string $firstName, string $lastName, ?string $email = null, ?DateTime $birthDate = null)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
@@ -68,12 +70,14 @@ trait HumanTrait
public function __toString()
{
$template = '%s';
$email = '';
if ('' !== $this->email && null !== $this->email) {
$template .= ' <%s>';
$email = $this->email;
}
return sprintf($template, $this->getFullName(), $this->email);
return sprintf($template, $this->getFullName(), $email);
}
/**
@@ -81,7 +85,7 @@ trait HumanTrait
*
* @return string
*/
public function getFirstName()
public function getFirstName(): string
{
return $this->firstName;
}
@@ -91,7 +95,7 @@ trait HumanTrait
*
* @return string
*/
public function getLastName()
public function getLastName(): string
{
return $this->lastName;
}
@@ -101,7 +105,7 @@ trait HumanTrait
*
* @return null|string
*/
public function getEmail()
public function getEmail(): ?string
{
return $this->email;
}
@@ -109,9 +113,9 @@ trait HumanTrait
/**
* Returns birth date
*
* @return null|\DateTime
* @return null|DateTime
*/
public function getBirthDate()
public function getBirthDate(): ?DateTime
{
return $this->birthDate;
}
@@ -119,10 +123,11 @@ trait HumanTrait
/**
* Returns the full name
*
* @param bool $firstNameFirst (optional) If is set to true, first name is the first part. Otherwise - last name.
* @param bool $firstNameFirst (optional) If is set to true, first name is the first part (default behaviour).
* Otherwise - name.
* @return string
*/
public function getFullName($firstNameFirst = true)
public function getFullName(bool $firstNameFirst = true): string
{
$beginning = $this->lastName;
$finish = $this->firstName;