Skip to content

Commit abb89cd

Browse files
committed
Fixup phpstan failures
1 parent 0e0c44a commit abb89cd

File tree

5 files changed

+51
-15
lines changed

5 files changed

+51
-15
lines changed

src/Solution/DirectorySolution.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ private function __construct(string $directory, string $entryPoint, array $exclu
8181
* @param string $directory The directory to search for files.
8282
* @param array<string> $exclusions An array of file names to exclude from the folder.
8383
* @param string $entryPoint The relative path from the directory of the entry point file.
84-
* @return self
84+
* @return SolutionInterface
8585
*/
8686
public static function fromDirectory(
8787
string $directory,
8888
array $exclusions = [],
8989
$entryPoint = 'solution.php'
9090
): SolutionInterface {
91-
return InMemorySolution::fromSolution(
91+
return InTempSolution::fromSolution(
9292
new self($directory, $entryPoint, array_merge($exclusions, ['composer.lock', 'vendor']))
9393
);
9494
}

src/Solution/InMemorySolution.php renamed to src/Solution/InTempSolution.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44

55
namespace PhpSchool\PhpWorkshop\Solution;
66

7+
use PhpSchool\PhpWorkshop\Utils\System;
78
use Symfony\Component\Filesystem\Filesystem;
89

9-
class InMemorySolution implements SolutionInterface
10+
class InTempSolution implements SolutionInterface
1011
{
1112
/**
1213
* @var string
1314
*/
1415
private string $baseDirectory;
1516

17+
/**
18+
* @var string
19+
*/
20+
private string $entryPoint;
21+
1622
/**
1723
* @var SolutionFile[]
1824
*/
@@ -22,21 +28,22 @@ private function __construct(SolutionInterface $solution)
2228
{
2329
$fileSystem = new Filesystem();
2430

25-
$currentPath = explode('/', realpath(__DIR__));
26-
$solutionPath = explode('/', realpath($solution->getBaseDirectory()));
27-
$entryPointPath = explode('/', realpath($solution->getEntryPoint()));
31+
$tempDir = System::tempDir();
32+
$currentPath = explode('/', System::realpath(__DIR__));
33+
$solutionPath = explode('/', System::realpath($solution->getBaseDirectory()));
34+
$entryPointPath = explode('/', System::realpath($solution->getEntryPoint()));
2835

2936
$intersection = array_intersect($currentPath, $solutionPath);
3037

3138
if (count($intersection) <= 1) {
32-
$intersection = explode('/', realpath(sys_get_temp_dir()));
39+
$intersection = explode('/', $tempDir);
3340
}
3441

3542
$basename = implode('/', array_diff($solutionPath, $intersection));
3643
$entrypoint = implode('/', array_diff($entryPointPath, $intersection));
3744

38-
$this->baseDirectory = sprintf('%s/php-school/%s', realpath(sys_get_temp_dir()), $basename);
39-
$this->entryPoint = sprintf('%s/php-school/%s', realpath(sys_get_temp_dir()), $entrypoint);
45+
$this->baseDirectory = sprintf('%s/php-school/%s', $tempDir, $basename);
46+
$this->entryPoint = sprintf('%s/php-school/%s', $tempDir, $entrypoint);
4047

4148
if ($fileSystem->exists($this->baseDirectory)) {
4249
$fileSystem->remove($this->baseDirectory);
@@ -57,14 +64,14 @@ private function __construct(SolutionInterface $solution)
5764
: $fileSystem->copy($file->getPathname(), $target);
5865
}
5966

60-
$this->files = array_map(function (SolutionFile $solutionFile) use ($intersection) {
61-
$filePath = explode('/', realpath($solutionFile->__toString()));
67+
$this->files = array_map(function (SolutionFile $solutionFile) use ($intersection, $tempDir) {
68+
$filePath = explode('/', System::realpath($solutionFile->__toString()));
6269
$file = implode('/', array_diff($filePath, $intersection));
63-
return SolutionFile::fromFile(sprintf('%s/php-school/%s', realpath(sys_get_temp_dir()), $file));
70+
return SolutionFile::fromFile(sprintf('%s/php-school/%s', $tempDir, $file));
6471
}, $solution->getFiles());
6572
}
6673

67-
public static function fromSolution(SolutionInterface $solution)
74+
public static function fromSolution(SolutionInterface $solution): SolutionInterface
6875
{
6976
return new self($solution);
7077
}

src/Solution/SingleFileSolution.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ private function __construct(string $file)
3333
* Static constructor to build an instance from an absolute file path.
3434
*
3535
* @param string $file The absolute path of the reference solution.
36-
* @return self
36+
* @return SolutionInterface
3737
* @throws InvalidArgumentException If the file does not exist.
3838
*/
3939
public static function fromFile(string $file): SolutionInterface
4040
{
41-
return InMemorySolution::fromSolution(new self($file));
41+
return InTempSolution::fromSolution(new self($file));
4242
}
4343

4444
/**

src/TestUtils/SolutionPathTransformer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ class SolutionPathTransformer
99
public static function tempPathToSolutionTempPath(string $tmpFilePath): string
1010
{
1111
$tmpDir = realpath(sys_get_temp_dir());
12+
13+
if (!$tmpDir) {
14+
throw new \RuntimeException();
15+
}
16+
1217
$file = str_replace($tmpDir, '', $tmpFilePath);
1318

1419
return sprintf('%s/php-school/%s', $tmpDir, ltrim($file, '/'));

src/Utils/System.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpSchool\PhpWorkshop\Utils;
6+
7+
class System
8+
{
9+
public static function realpath(string $path): string
10+
{
11+
$realpath = realpath($path);
12+
13+
if (false === $realpath) {
14+
throw new \RuntimeException(sprintf('Failed to get realpath of "%s"', $path));
15+
}
16+
17+
return $realpath;
18+
}
19+
20+
public static function tempDir(): string
21+
{
22+
return self::realpath(sys_get_temp_dir());
23+
}
24+
}

0 commit comments

Comments
 (0)