Skip to content

Commit

Permalink
Merge pull request #1498 from scyzoryck/phpstan-lvl-3
Browse files Browse the repository at this point in the history
refactor(phpstan): Implement PHPStan lvl 3
  • Loading branch information
scyzoryck authored Jul 29, 2023
2 parents 06864dd + a0c7789 commit e8c8124
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 63 deletions.
3 changes: 2 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
includes:
- phpstan/ignore-by-php-version.neon.php
parameters:
level: 2
level: 3
ignoreErrors:
- '~Class Doctrine\\Common\\Persistence\\Proxy not found~'
- '~Class Doctrine\\ODM\\MongoDB\\PersistentCollection not found~'
Expand All @@ -20,6 +20,7 @@ parameters:
- '#^Call to an undefined method JMS\\Serializer\\VisitorInterface\:\:hasData\(\)\.$#'
- '#^Property JMS\\Serializer\\Tests\\Fixtures\\DocBlockType\\Collection\\CollectionOfClassesWithFullNamespacePath\:\:\$productIds has unknown class JMS\\Serializer\\Tests\\Fixtures\\DocBlockType\\Collection\\JMS\\Serializer\\Tests\\Fixtures\\DocBlockType\\Collection\\Product as its type\.$#'
- '#^Property JMS\\Serializer\\Tests\\Fixtures\\DocBlockType\\Collection\\CollectionOfInterfacesWithFullNamespacePath\:\:\$productColors has unknown class JMS\\Serializer\\Tests\\Fixtures\\DocBlockType\\Collection\\JMS\\Serializer\\Tests\\Fixtures\\DocBlockType\\Collection\\Details\\ProductColor as its type\.$#'
- '#^Method JMS\\Serializer\\GraphNavigator\\DeserializationGraphNavigator\:\:resolveMetadata\(\) should return JMS\\Serializer\\Metadata\\ClassMetadata\|null#'
paths:
- %currentWorkingDirectory%/src
- %currentWorkingDirectory%/tests
5 changes: 2 additions & 3 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use JMS\Serializer\Exclusion\VersionExclusionStrategy;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;
use Metadata\MetadataFactory;
use Metadata\MetadataFactoryInterface;

abstract class Context
Expand All @@ -39,11 +38,11 @@ abstract class Context
private $navigator;

/**
* @var MetadataFactory
* @var MetadataFactoryInterface
*/
private $metadataFactory;

/** @var DisjunctExclusionStrategy */
/** @var ExclusionStrategyInterface */
private $exclusionStrategy;

/**
Expand Down
20 changes: 1 addition & 19 deletions src/ContextFactory/CallableContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,9 @@

namespace JMS\Serializer\ContextFactory;

use JMS\Serializer\Context;

/**
* Context Factory using a callable.
* @deprecated
*/
abstract class CallableContextFactory
{
/**
* @var callable
*/
private $callable;

public function __construct(callable $callable)
{
$this->callable = $callable;
}

protected function createContext(): Context
{
$callable = $this->callable;

return $callable();
}
}
22 changes: 17 additions & 5 deletions src/ContextFactory/CallableDeserializationContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@

use JMS\Serializer\DeserializationContext;

/**
* Deserialization Context Factory using a callable.
*/
final class CallableDeserializationContextFactory extends CallableContextFactory implements
final class CallableDeserializationContextFactory implements
DeserializationContextFactoryInterface
{
/**
* @var callable():DeserializationContext
*/
private $callable;

/**
* @param callable():DeserializationContext $callable
*/
public function __construct(callable $callable)
{
$this->callable = $callable;
}

public function createDeserializationContext(): DeserializationContext
{
return $this->createContext();
$callable = $this->callable;

return $callable();
}
}
20 changes: 17 additions & 3 deletions src/ContextFactory/CallableSerializationContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@
/**
* Serialization Context Factory using a callable.
*/
final class CallableSerializationContextFactory extends CallableContextFactory implements
SerializationContextFactoryInterface
final class CallableSerializationContextFactory implements SerializationContextFactoryInterface
{
/**
* @var callable():SerializationContext
*/
private $callable;

/**
* @param callable():SerializationContext $callable
*/
public function __construct(callable $callable)
{
$this->callable = $callable;
}

public function createSerializationContext(): SerializationContext
{
return $this->createContext();
$callable = $this->callable;

return $callable();
}
}
1 change: 1 addition & 0 deletions src/Handler/FormErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ private function getErrorMessage(FormError $error): ?string

private function convertFormToArray(SerializationVisitorInterface $visitor, FormInterface $data): \ArrayObject
{
/** @var \ArrayObject{errors?: string[], children?: array<string, \ArrayObject>} $form */
$form = new \ArrayObject();
$errors = [];
foreach ($data->getErrors() as $error) {
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/HandlerRegistryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function registerHandler(int $direction, string $typeName, string $format
/**
* @param int $direction one of the GraphNavigatorInterface::DIRECTION_??? constants
*
* @return callable|object
* @return callable|object|null
*/
public function getHandler(int $direction, string $typeName, string $format);
}
3 changes: 3 additions & 0 deletions src/Metadata/Driver/DocBlockDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public function __construct(DriverInterface $delegate, ?ParserInterface $typePar
$this->docBlockTypeResolver = new DocBlockTypeResolver();
}

/**
* @return SerializerClassMetadata|null
*/
public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata
{
$classMetadata = $this->delegate->loadMetadataForClass($class);
Expand Down
2 changes: 1 addition & 1 deletion src/SerializerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ final class SerializerBuilder
private $cacheDir;

/**
* @var AnnotationReader
* @var Reader
*/
private $annotationReader;

Expand Down
4 changes: 2 additions & 2 deletions src/Type/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @internal
*/
final class Lexer extends AbstractLexer implements ParserInterface
final class Lexer extends AbstractLexer
{
public const T_UNKNOWN = 0;
public const T_INTEGER = 1;
Expand All @@ -24,7 +24,7 @@ final class Lexer extends AbstractLexer implements ParserInterface
public const T_IDENTIFIER = 9;
public const T_NULL = 10;

public function parse(string $type): array
public function parse(string $type)
{
try {
return $this->getType($type);
Expand Down
4 changes: 2 additions & 2 deletions src/Visitor/SerializationVisitorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function visitDouble(float $data, array $type);
public function visitInteger(int $data, array $type);

/**
* @return array|\ArrayObject
* @return array|\ArrayObject|void
*/
public function visitArray(array $data, array $type);

Expand All @@ -64,7 +64,7 @@ public function visitProperty(PropertyMetadata $metadata, $data): void;
/**
* Called after all properties of the object have been visited.
*
* @return array|\ArrayObject
* @return array|\ArrayObject|void
*/
public function endVisitingObject(ClassMetadata $metadata, object $data, array $type);
}
11 changes: 3 additions & 8 deletions tests/Fixtures/Doctrine/PersistendCollection/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,10 @@ class App
#[Serializer\Type(name: SmartPhone::class)]
private $smartPhone;

/**
* @param string $id
* @param string $name
* @param string $smartPhone
*/
public function __construct(
$id,
$name,
$smartPhone
string $id,
string $name,
SmartPhone $smartPhone
) {
$this->id = $id;
$this->name = $name;
Expand Down
11 changes: 7 additions & 4 deletions tests/Fixtures/NamedDateTimeImmutableArraysObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,26 @@
class NamedDateTimeImmutableArraysObject
{
/**
* @var \DateTime[]
* @var array<string, \DateTimeImmutable>
* @Type("array<string,DateTimeImmutable<'d.m.Y H:i:s'>>")
* @XmlKeyValuePairs
*/
#[Type(name: 'array<string,DateTimeImmutable<"d.m.Y H:i:s">>')]
#[XmlKeyValuePairs]
private $namedArrayWithFormattedDate;

public function __construct($namedArrayWithFormattedDate)
/**
* @param array<string, \DateTimeImmutable> $namedArrayWithFormattedDate
*/
public function __construct(array $namedArrayWithFormattedDate)
{
$this->namedArrayWithFormattedDate = $namedArrayWithFormattedDate;
}

/**
* @return \DateTimeImmutable[]
* @return array<string, \DateTimeImmutable>
*/
public function getNamedArrayWithFormattedDate()
public function getNamedArrayWithFormattedDate(): array
{
return $this->namedArrayWithFormattedDate;
}
Expand Down
12 changes: 2 additions & 10 deletions tests/Handler/FormErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace JMS\Serializer\Tests\Handler;

use JMS\Serializer\Handler\FormErrorHandler;
use JMS\Serializer\JsonSerializationVisitor;
use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory;
use JMS\Serializer\Visitor\SerializationVisitorInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
Expand Down Expand Up @@ -34,7 +34,7 @@ class FormErrorHandlerTest extends TestCase
protected $handler;

/**
* @var JsonSerializationVisitor
* @var SerializationVisitorInterface
*/
protected $visitor;

Expand All @@ -56,14 +56,6 @@ protected function setUp(): void
$this->factory = $this->getMockBuilder(FormFactoryInterface::class)->getMock();
}

protected function tearDown(): void
{
$this->handler = null;
$this->visitor = null;
$this->dispatcher = null;
$this->factory = null;
}

public function testSerializeEmptyFormError()
{
$form = $this->createForm();
Expand Down
4 changes: 2 additions & 2 deletions tests/Metadata/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function testSerializationClass()
{
$meta = new ClassMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder');
$meta->xmlRootPrefix = 'foo';
$meta->xmlDiscriminatorCData = 'foo';
$meta->xmlDiscriminatorAttribute = 'foo';
$meta->xmlDiscriminatorCData = true;
$meta->xmlDiscriminatorAttribute = false;
$meta->xmlRootName = 'foo';

$restoredMeta = unserialize(serialize($meta));
Expand Down
2 changes: 1 addition & 1 deletion tests/Serializer/Doctrine/ObjectConstructorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ static function ($id) use ($connection, $entityManager) {
}
}
);
$driver = null;

$this->driver = &$driver;
$this->serializer = SerializerBuilder::create()
->setMetadataDriverFactory(new CallbackDriverFactory(
Expand Down
2 changes: 1 addition & 1 deletion tests/Serializer/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ public static function getTypeHintedArraysAndStdClass()
*
* @dataProvider getTypeHintedArraysAndStdClass
*/
public function testTypeHintedArrayAndStdClassSerialization(array $array, $expected, $context = null)
public function testTypeHintedArrayAncdtdClassSerialization(array $array, $expected, $context = null)
{
self::assertEquals($expected, $this->serialize($array, $context));
}
Expand Down

0 comments on commit e8c8124

Please sign in to comment.