Skip to content

Commit 81190a2

Browse files
committed
bug #4 fix generics templates (bendavies)
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
2 parents fccf7f1 + 15781f2 commit 81190a2

File tree

11 files changed

+109
-15
lines changed

11 files changed

+109
-15
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
"symfony/filesystem": "^6.3",
1818
"symfony/framework-bundle": "^6.3",
1919
"symfony/phpunit-bridge": "^6.3",
20-
"phpstan/phpstan": "1.11.x-dev"
20+
"phpstan/phpstan": "^1.10.39"
2121
},
22-
"minimum-stability": "dev",
2322
"autoload": {
2423
"psr-4": {
2524
"Symfonycasts\\MicroMapper\\": "src"

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<server name="SHELL_VERBOSITY" value="-1" />
1717
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
1818
<server name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
19+
<env name="SYMFONY_DEPRECATIONS_HELPER" value="ignoreFile=./tests/deprecations-ignored"/>
1920
</php>
2021

2122
<testsuites>

src/MapperInterface.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
*
1515
* Also add #[AsMapper(from: Foo:class, to: Bar:class)] to each mapper class.
1616
*
17-
* @template TFrom of object
18-
* @template TTo of object
19-
*
2017
* @author Ryan Weaver <[email protected]>
2118
*/
2219
interface MapperInterface
@@ -27,7 +24,8 @@ interface MapperInterface
2724
* This method should load (e.g. from the database) or instantiate the "to object".
2825
* Avoid populating any properties except for an identifier.
2926
*
30-
* @param TFrom $from
27+
* @template TTo of object
28+
*
3129
* @param class-string<TTo> $toClass
3230
*
3331
* @return TTo
@@ -39,8 +37,9 @@ public function load(object $from, string $toClass, array $context): object;
3937
*
4038
* Receives the "to object" returned from load().
4139
*
42-
* @param TFrom $from
43-
* @param TTo $to
40+
* @template TTo of object
41+
*
42+
* @param TTo $to
4443
*
4544
* @return TTo
4645
*/

src/MicroMapperInterface.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@
1111

1212
/**
1313
* Maps one object to another using the configured mappers.
14-
*
15-
* @template TFrom of object
16-
* @template TTo of object
1714
*/
1815
interface MicroMapperInterface
1916
{
2017
public const MAX_DEPTH = 'max_depth';
2118

2219
/**
23-
* @param TFrom $from
20+
* @template TTo of object
21+
*
2422
* @param class-string<TTo> $toClass
2523
*
2624
* @return TTo

tests/IntegrationTest.php renamed to tests/Functional/IntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* file that was distributed with this source code.
88
*/
99

10-
namespace Symfonycasts\MicroMapper\Tests;
10+
namespace Symfonycasts\MicroMapper\Tests\Functional;
1111

1212
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1313
use Symfonycasts\MicroMapper\MicroMapperInterface;

tests/PHPStan/MicroMapperTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the SymfonyCasts MicroMapper package.
7+
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfonycasts\MicroMapper\Tests\PHPStan;
13+
14+
use PHPStan\Testing\TypeInferenceTestCase;
15+
16+
final class MicroMapperTest extends TypeInferenceTestCase
17+
{
18+
/** @return array<string, mixed[]> */
19+
public static function dataFileAsserts(): iterable
20+
{
21+
yield from self::gatherAssertTypes(__DIR__.'/data/micro_mapper.php');
22+
yield from self::gatherAssertTypes(__DIR__.'/data/mapper.php');
23+
}
24+
25+
/**
26+
* @dataProvider dataFileAsserts
27+
*/
28+
public function testFileAsserts(
29+
string $assertType,
30+
string $file,
31+
mixed ...$args,
32+
): void {
33+
$this->assertFileAsserts($assertType, $file, ...$args);
34+
}
35+
}

tests/PHPStan/data/mapper.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the SymfonyCasts MicroMapper package.
7+
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfonycasts\MicroMapper\MapperInterface;
13+
use Symfonycasts\MicroMapper\Tests\fixtures\DinosaurDto;
14+
15+
use function PHPStan\Testing\assertType;
16+
17+
function doMapperInterfaceLoad(MapperInterface $microMapper): void
18+
{
19+
assertType(DinosaurDto::class, $microMapper->load(new \stdClass(), DinosaurDto::class));
20+
}
21+
22+
function doMapperInterfacePopulate(MapperInterface $microMapper, DinosaurDto $dto): void
23+
{
24+
assertType(DinosaurDto::class, $microMapper->populate(new \stdClass(), $dto));
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the SymfonyCasts MicroMapper package.
7+
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfonycasts\MicroMapper\MicroMapper;
13+
use Symfonycasts\MicroMapper\MicroMapperInterface;
14+
use Symfonycasts\MicroMapper\Tests\fixtures\DinosaurDto;
15+
16+
use function PHPStan\Testing\assertType;
17+
18+
function doMicroMapperInterfaceMap(MicroMapperInterface $microMapper): void
19+
{
20+
assertType(DinosaurDto::class, $microMapper->map(new \stdClass(), DinosaurDto::class));
21+
}
22+
23+
function doMicroMapperImplementationMap(MicroMapper $microMapper): void
24+
{
25+
assertType(DinosaurDto::class, $microMapper->map(new \stdClass(), DinosaurDto::class));
26+
}

tests/MapperConfigTest.php renamed to tests/Unit/MapperConfigTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* file that was distributed with this source code.
88
*/
99

10-
namespace Symfonycasts\MicroMapper\Tests;
10+
namespace Symfonycasts\MicroMapper\Tests\Unit;
1111

1212
use PHPUnit\Framework\TestCase;
1313
use Symfonycasts\MicroMapper\MapperConfig;

tests/MicroMapperTest.php renamed to tests/Unit/MicroMapperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* file that was distributed with this source code.
88
*/
99

10-
namespace Symfonycasts\MicroMapper\Tests;
10+
namespace Symfonycasts\MicroMapper\Tests\Unit;
1111

1212
use PHPUnit\Framework\TestCase;
1313
use Symfonycasts\MicroMapper\MapperConfig;

0 commit comments

Comments
 (0)