Skip to content
This repository was archived by the owner on Mar 6, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Phpactor\CodeBuilder\Domain\TemplatePathResolver;

use Iterator;
use FilterIterator;
use RuntimeException;
use SplFileInfo;

class FilterPhpVersionDirectoryIterator extends FilterIterator
{
/**
* @var string In the form of "major.minor.release[extra]"
* @see https://www.php.net/manual/en/reserved.constants.php#reserved.constants.core
*/
private $phpVersion;

public function __construct(Iterator $iterator, string $phpVersion)
{
parent::__construct($iterator);

$this->phpVersion = $phpVersion;
}

/**
* {@inheritDoc}
*/
public function accept()
{
$file = $this->current();
if (!$file instanceof SplFileInfo) {
throw new RuntimeException(
sprintf(
'Expected instance of "\SplFileInfo", got "%s".',
\is_object($file) ? \get_class($file) : \gettype($file)
)
);
}

$filename = $file->getFilename();

if (!$file->isDir() || // Keep only directy
!preg_match('/^\d+\.\d+/', $filename) || // Should have at leasts major and minor version
!version_compare($filename, $this->phpVersion, '<=') // Should be at maximum equals to the defined version
) {
return false;
}

return true;
}
}
42 changes: 42 additions & 0 deletions lib/Domain/TemplatePathResolver/PhpVersionPathResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Phpactor\CodeBuilder\Domain\TemplatePathResolver;

use FilesystemIterator;

class PhpVersionPathResolver
{
/**
* @var string In the form of "major.minor.release[extra]"
* @see https://www.php.net/manual/en/reserved.constants.php#reserved.constants.core
*/
private $phpVersion;

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

public function resolve(iterable $paths): iterable
{
$resolvedPaths = [];

foreach ($paths as $path) {
if (!file_exists($path)) {
continue;
}

$phpDirectoriesIterator = new FilterPhpVersionDirectoryIterator(
new FilesystemIterator($path),
$this->phpVersion
);
$phpDirectories = array_keys(iterator_to_array($phpDirectoriesIterator));
rsort($phpDirectories, SORT_NATURAL);

$resolvedPaths = array_merge($resolvedPaths, $phpDirectories);
$resolvedPaths[] = $path;
}

return $resolvedPaths;
}
}
5 changes: 5 additions & 0 deletions templates/7.4/Property.php.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{ prototype.visibility }}
{%- if prototype.type.notNone %}
{{ prototype.type }}
{%- endif %}
${{ prototype.name}}{% if prototype.defaultValue.notNone %} = {{ prototype.defaultValue.export }}{% endif %};
14 changes: 14 additions & 0 deletions tests/IntegrationTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Phpactor\CodeBuilder\Tests;

use PHPUnit\Framework\TestCase;
use Phpactor\TestUtils\Workspace;

class IntegrationTestCase extends TestCase
{
public function workspace(): Workspace
{
return Workspace::create(__DIR__ .'/Workspace');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Phpactor\CodeBuilder\Tests\Unit\Domain\TemplatePathResolver;

use ArrayIterator;
use Iterator;
use PHPUnit\Framework\TestCase;
use Phpactor\CodeBuilder\Domain\TemplatePathResolver\FilterPhpVersionDirectoryIterator;
use SplFileInfo;

class FilterPhpVersionDirectoryIteratorTest extends TestCase
{
/**
* @dataProvider provideDirectoriesToFilter
*/
public function testThatItKeepsOnlyDirectoriesOfInferiorOrEqualVersion(
Iterator $iterator,
string $phpVersion,
array $expectedFilteredDirectories
) {
$filteredDirectories = \iterator_to_array(
new FilterPhpVersionDirectoryIterator($iterator, $phpVersion)
);

$this->assertEqualsCanonicalizing(
$expectedFilteredDirectories,
$filteredDirectories
);
}

public function provideDirectoriesToFilter(): iterable
{
$directories = new ArrayIterator([
$this->createFakeFile('a-file'),
$php72 = $this->createFakeDirectory('7.2'),
$this->createFakeDirectory('a-directory'),
$php74 = $this->createFakeDirectory('7.4'),
$php74Special = $this->createFakeDirectory('7.4-special'),
]);

yield 'For PHP 7.3' => [$directories, '7.3.3', [$php72]];
yield 'For PHP 7.4' => [$directories, '7.4.0', [$php72, $php74, $php74Special]];
}

private function createFakeFile(string $filename): SplFileInfo
{
return $this->createFakeSplFileInfo($filename, false);
}

private function createFakeDirectory(string $filename): SplFileInfo
{
return $this->createFakeSplFileInfo($filename, true);
}

private function createFakeSplFileInfo(string $filename, bool $isDir = false): SplFileInfo
{
$file = $this->prophesize(SplFileInfo::class);
$file->getFilename()->willReturn($filename);
$file->isDir()->willReturn($isDir);

return $file->reveal();
}
}
104 changes: 104 additions & 0 deletions tests/Unit/Domain/TemplatePathResolver/PhpVersionPathResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Phpactor\CodeBuilder\Tests\Unit\Domain\TemplatePathResolver;

use Phpactor\CodeBuilder\Domain\TemplatePathResolver\PhpVersionPathResolver;
use Phpactor\CodeBuilder\Tests\IntegrationTestCase;

class PhpVersionPathResolverTest extends IntegrationTestCase
{
protected function setUp(): void
{
$this->workspace()->reset();
}

/**
* @dataProvider provideResolvePaths
*/
public function testResolvePaths(
string $phpVersion,
array $fullTemplatePaths,
array $templatePaths,
array $expectedPaths
) {
foreach ($fullTemplatePaths as $fullTemplatePath) {
$this->workspace()->mkdir($fullTemplatePath, '');
}

$resolver = new PhpVersionPathResolver($phpVersion);
self::assertEquals(array_map(function (string $path) {
return $this->workspace()->path($path);
}, $expectedPaths), $resolver->resolve(array_map(function (string $path) {
return $this->workspace()->path($path);
}, $templatePaths)));
}

public function provideResolvePaths()
{
yield 'none' => [
'5.6',
[],
[],
[]
];

yield 'resolves given path but not the version path' => [
'7.0',
[
'/path1/7.1'
],
[
'/path1'
],
[
'/path1',
]
];

yield 'priorities the version path if the version matches' => [
'7.1',
[
'/path1/7.1'
],
[
'/path1'
],
[
'/path1/7.1',
'/path1',
]
];

yield 'returns previous versions for higher versions' => [
'7.2',
[
'/path1/7.1'
],
[
'/path1'
],
[
'/path1/7.1',
'/path1',
]
];

yield 'returns multiple previous versions' => [
'7.2',
[
'/path1/7.0',
'/path1/7.1',
'/path1/7.2'
],
[
'/path1'
],
[
'/path1/7.2',
'/path1/7.1',
'/path1/7.0',
'/path1',
]
];
}
}