Skip to content

Commit

Permalink
bug #4 fix generics templates (bendavies)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the main branch.

Discussion
----------

fix generics templates

This PR fixes the templates for `MicroMapperInterface` and `MapperInterface`.
regression tests have been added.

as an aside, is the `$toClass` param in `MapperInterface::populate(...)` redundant?
It seems we will only ever have concrete implementations here and will just new up a new class, which will match the class provided in the `AsMapper` attribute.

Commits
-------

15781f2 fix generics templates
  • Loading branch information
weaverryan committed Oct 18, 2023
2 parents fccf7f1 + 15781f2 commit 81190a2
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 15 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
"symfony/filesystem": "^6.3",
"symfony/framework-bundle": "^6.3",
"symfony/phpunit-bridge": "^6.3",
"phpstan/phpstan": "1.11.x-dev"
"phpstan/phpstan": "^1.10.39"
},
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Symfonycasts\\MicroMapper\\": "src"
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
<server name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="ignoreFile=./tests/deprecations-ignored"/>
</php>

<testsuites>
Expand Down
11 changes: 5 additions & 6 deletions src/MapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
*
* Also add #[AsMapper(from: Foo:class, to: Bar:class)] to each mapper class.
*
* @template TFrom of object
* @template TTo of object
*
* @author Ryan Weaver <[email protected]>
*/
interface MapperInterface
Expand All @@ -27,7 +24,8 @@ interface MapperInterface
* This method should load (e.g. from the database) or instantiate the "to object".
* Avoid populating any properties except for an identifier.
*
* @param TFrom $from
* @template TTo of object
*
* @param class-string<TTo> $toClass
*
* @return TTo
Expand All @@ -39,8 +37,9 @@ public function load(object $from, string $toClass, array $context): object;
*
* Receives the "to object" returned from load().
*
* @param TFrom $from
* @param TTo $to
* @template TTo of object
*
* @param TTo $to
*
* @return TTo
*/
Expand Down
6 changes: 2 additions & 4 deletions src/MicroMapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@

/**
* Maps one object to another using the configured mappers.
*
* @template TFrom of object
* @template TTo of object
*/
interface MicroMapperInterface
{
public const MAX_DEPTH = 'max_depth';

/**
* @param TFrom $from
* @template TTo of object
*
* @param class-string<TTo> $toClass
*
* @return TTo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* file that was distributed with this source code.
*/

namespace Symfonycasts\MicroMapper\Tests;
namespace Symfonycasts\MicroMapper\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfonycasts\MicroMapper\MicroMapperInterface;
Expand Down
35 changes: 35 additions & 0 deletions tests/PHPStan/MicroMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfonycasts\MicroMapper\Tests\PHPStan;

use PHPStan\Testing\TypeInferenceTestCase;

final class MicroMapperTest extends TypeInferenceTestCase
{
/** @return array<string, mixed[]> */
public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__.'/data/micro_mapper.php');
yield from self::gatherAssertTypes(__DIR__.'/data/mapper.php');
}

/**
* @dataProvider dataFileAsserts
*/
public function testFileAsserts(
string $assertType,
string $file,
mixed ...$args,
): void {
$this->assertFileAsserts($assertType, $file, ...$args);
}
}
25 changes: 25 additions & 0 deletions tests/PHPStan/data/mapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\Tests\fixtures\DinosaurDto;

use function PHPStan\Testing\assertType;

function doMapperInterfaceLoad(MapperInterface $microMapper): void
{
assertType(DinosaurDto::class, $microMapper->load(new \stdClass(), DinosaurDto::class));
}

function doMapperInterfacePopulate(MapperInterface $microMapper, DinosaurDto $dto): void
{
assertType(DinosaurDto::class, $microMapper->populate(new \stdClass(), $dto));
}
26 changes: 26 additions & 0 deletions tests/PHPStan/data/micro_mapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/*
* This file is part of the SymfonyCasts MicroMapper package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfonycasts\MicroMapper\MicroMapper;
use Symfonycasts\MicroMapper\MicroMapperInterface;
use Symfonycasts\MicroMapper\Tests\fixtures\DinosaurDto;

use function PHPStan\Testing\assertType;

function doMicroMapperInterfaceMap(MicroMapperInterface $microMapper): void
{
assertType(DinosaurDto::class, $microMapper->map(new \stdClass(), DinosaurDto::class));
}

function doMicroMapperImplementationMap(MicroMapper $microMapper): void
{
assertType(DinosaurDto::class, $microMapper->map(new \stdClass(), DinosaurDto::class));
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* file that was distributed with this source code.
*/

namespace Symfonycasts\MicroMapper\Tests;
namespace Symfonycasts\MicroMapper\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Symfonycasts\MicroMapper\MapperConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* file that was distributed with this source code.
*/

namespace Symfonycasts\MicroMapper\Tests;
namespace Symfonycasts\MicroMapper\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Symfonycasts\MicroMapper\MapperConfig;
Expand Down
11 changes: 11 additions & 0 deletions tests/deprecations-ignored
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
%_PHPStan_d5c599c96%
%^Method "PhpParser\\NodeVisitor\:\:leaveNode\(\)" might add "int\|Node\|array\|null" as a native return type declaration in the future\. Do the same in implementation "PHPStan\\BetterReflection\\SourceLocator\\SourceStubber\\PhpStormStubs\\CachingVisitor" now to avoid errors or add an explicit @return annotation to suppress this message\.$%
%^Method "PhpParser\\NodeVisitor\:\:enterNode\(\)" might add "int\|Node\|null" as a native return type declaration in the future\. Do the same in implementation "PhpParser\\NodeVisitorAbstract@anonymous" now to avoid errors or add an explicit @return annotation to suppress this message\.$%
%^Method "PhpParser\\NodeVisitor\:\:leaveNode\(\)" might add "int\|Node\|array\|null" as a native return type declaration in the future\. Do the same in implementation "PhpParser\\NodeVisitorAbstract@anonymous" now to avoid errors or add an explicit @return annotation to suppress this message\.$%
%^Method "ReflectionFunction\:\:invokeArgs\(\)" might add "mixed" as a native return type declaration in the future\. Do the same in child class "PHPStan\\BetterReflection\\Reflection\\Adapter\\ReflectionFunction" now to avoid errors or add an explicit @return annotation to suppress this message\.$%
%^Method "ReflectionClass\:\:getFileName\(\)" might add "string\|false" as a native return type declaration in the future. Do the same in child class "PHPStan\\BetterReflection\\Reflection\\Adapter\\ReflectionClass" now to avoid errors or add an explicit @return annotation to suppress this message\.$%
%^Method "ReflectionClass\:\:getStartLine\(\)" might add "int\|false" as a native return type declaration in the future. Do the same in child class "PHPStan\\BetterReflection\\Reflection\\Adapter\\ReflectionClass" now to avoid errors or add an explicit @return annotation to suppress this message\.$%
%^Method "ReflectionClass\:\:getEndLine\(\)" might add "int\|false" as a native return type declaration in the future. Do the same in child class "PHPStan\\BetterReflection\\Reflection\\Adapter\\ReflectionClass" now to avoid errors or add an explicit @return annotation to suppress this message\.$%
%^Method "ReflectionClass\:\:getDocComment\(\)" might add "string\|false" as a native return type declaration in the future. Do the same in child class "PHPStan\\BetterReflection\\Reflection\\Adapter\\ReflectionClass" now to avoid errors or add an explicit @return annotation to suppress this message\.$%
%^Method "ReflectionClass\:\:getStaticPropertyValue\(\)" might add "mixed" as a native return type declaration in the future. Do the same in child class "PHPStan\\BetterReflection\\Reflection\\Adapter\\ReflectionClass" now to avoid errors or add an explicit @return annotation to suppress this message\.$%
%^Method "ReflectionClass\:\:getExtensionName\(\)" might add "string\|false" as a native return type declaration in the future. Do the same in child class "PHPStan\\BetterReflection\\Reflection\\Adapter\\ReflectionClass" now to avoid errors or add an explicit @return annotation to suppress this message\.$%

0 comments on commit 81190a2

Please sign in to comment.