Skip to content

Commit a0c0ae1

Browse files
authored
Merge pull request #9 from pawel-ledwig/feat/MGS-5456/php-84-compatibility
feat: [MGS-5456] PHP 8.4 compatibility.
2 parents 78067f3 + c3b7d16 commit a0c0ae1

13 files changed

Lines changed: 223 additions & 642 deletions

src/Exception/PatchApplicationFailedException.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,8 @@
66

77
class PatchApplicationFailedException extends PatchingException
88
{
9-
/**
10-
* @var string
11-
*/
12-
private $cmd;
13-
14-
/**
15-
* @var string
16-
*/
17-
private $cmdOutput;
9+
private string $cmd;
10+
private string $cmdOutput;
1811

1912
public function __construct(string $cmd, string $cmdOutput, string $message = "", int $code = 0, ?Throwable $previous = null)
2013
{
@@ -28,19 +21,13 @@ public function __construct(string $cmd, string $cmdOutput, string $message = ""
2821
$this->cmdOutput = $cmdOutput;
2922
}
3023

31-
/**
32-
* @return string
33-
*/
34-
public function getCmd()
24+
public function getCmd(): string
3525
{
3626
return $this->cmd;
3727
}
3828

39-
/**
40-
* @return string
41-
*/
42-
public function getCmdOutput()
29+
public function getCmdOutput(): string
4330
{
4431
return $this->cmdOutput;
4532
}
46-
}
33+
}

src/IOLogger.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class IOLogger extends ConsoleIO
1010
{
11-
const VERBOSITY_LEVEL_MAP = [
11+
public const VERBOSITY_LEVEL_MAP = [
1212
LogLevel::DEBUG => IOInterface::DEBUG,
1313
LogLevel::INFO => IOInterface::NORMAL,
1414
LogLevel::NOTICE => IOInterface::QUIET,
@@ -19,7 +19,7 @@ class IOLogger extends ConsoleIO
1919
LogLevel::EMERGENCY => IOInterface::QUIET
2020
];
2121

22-
const ERROR_LEVELS = [
22+
public const ERROR_LEVELS = [
2323
LogLevel::ERROR,
2424
LogLevel::CRITICAL,
2525
LogLevel::ALERT,
@@ -52,4 +52,4 @@ public function log($level, $message, array $context = []): void
5252
$this->io->write($message, true, $verbosity);
5353
}
5454
}
55-
}
55+
}

src/OperationResolver.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@ class OperationResolver
1414
{
1515
/**
1616
* Returns a repository that reflects the state after operations have been executed on the current one.
17-
*
18-
* @param RepositoryInterface $repository
19-
* @param array $operations
20-
* @return RepositoryInterface
2117
*/
22-
public function resolveState(RepositoryInterface $repository, array $operations)
18+
public function resolveState(RepositoryInterface $repository, array $operations): InstalledArrayRepository
2319
{
2420
$packages = array_map(function($p) { return clone $p; }, $repository->getPackages());
2521
$installed = new InstalledArrayRepository($packages);
@@ -37,4 +33,4 @@ public function resolveState(RepositoryInterface $repository, array $operations)
3733

3834
return $installed;
3935
}
40-
}
36+
}

src/PackageApplicationRepository.php

Lines changed: 48 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,12 @@
1010

1111
class PackageApplicationRepository
1212
{
13-
/**
14-
* @var RepositoryInterface
15-
*/
16-
private $installedRepository;
17-
18-
/**
19-
* @var InstallationManager
20-
*/
21-
private $installationManager;
22-
23-
/**
24-
* @var PathResolver
25-
*/
26-
private $pathResolver;
27-
28-
/**
29-
* @var LoggerInterface
30-
*/
31-
private $logger;
32-
33-
/**
34-
* @param RepositoryInterface $installedRepository
35-
* @param InstallationManager $installationManager
36-
* @param PathResolver $pathResolver
37-
* @param LoggerInterface $logger
38-
*/
3913
public function __construct(
40-
RepositoryInterface $installedRepository,
41-
InstallationManager $installationManager,
42-
PathResolver $pathResolver,
43-
LoggerInterface $logger
44-
) {
45-
$this->installedRepository = $installedRepository;
46-
$this->installationManager = $installationManager;
47-
$this->pathResolver = $pathResolver;
48-
$this->logger = $logger;
49-
}
14+
private RepositoryInterface $installedRepository,
15+
private InstallationManager $installationManager,
16+
private PathResolver $pathResolver,
17+
private LoggerInterface $logger
18+
) {}
5019

5120
/**
5221
* @return PackagePatchApplication[]
@@ -70,6 +39,7 @@ public function getPackageApplications()
7039

7140
/**
7241
* @param PackageInterface $targetPackage
42+
*
7343
* @return PackagePatchApplication
7444
*/
7545
public function getPackageApplication(PackageInterface $targetPackage)
@@ -105,39 +75,50 @@ public function savePackageApplication(PackagePatchApplication $packagePatchAppl
10575
throw new \RuntimeException(sprintf('Package directory is not writable "%s"', dirname($dataFile)));
10676
}
10777

108-
file_put_contents($dataFile,
78+
file_put_contents(
79+
$dataFile,
10980
$this->encodeData($this->transformPackagePatchApplicationToArray($packagePatchApplication))
11081
);
11182
}
11283

11384
/**
11485
* @param array $data
86+
*
11587
* @return string
11688
*/
11789
private function encodeData(array $data)
11890
{
119-
return json_encode($data,
91+
return json_encode(
92+
$data,
12093
JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE
12194
);
12295
}
12396

12497
/**
12598
* @param PackageInterface $targetPackage
12699
* @param array $data
100+
*
127101
* @return PackagePatchApplication
128102
*/
129-
private function createPackagePatchApplication(PackageInterface $targetPackage, array $data)
103+
private function createPackagePatchApplication(PackageInterface $targetPackage, array $data): PackagePatchApplication
130104
{
131-
return new PackagePatchApplication($targetPackage,
132-
array_map([$this, 'createPatchApplication'], $data['patches'])
105+
$applications = [];
106+
107+
foreach ($data['patches'] as $patchData) {
108+
$patchApplication = $this->createPatchApplication($patchData);
109+
110+
if ($patchApplication) {
111+
$applications[] = $patchApplication;
112+
}
113+
}
114+
115+
return new PackagePatchApplication(
116+
$targetPackage,
117+
$applications
133118
);
134119
}
135120

136-
/**
137-
* @param array $data
138-
* @return PatchApplication
139-
*/
140-
private function createPatchApplication(array $data)
121+
private function createPatchApplication(array $data): ?PatchApplication
141122
{
142123
$patch = Patch::createFromArray($data['patch']);
143124

@@ -147,10 +128,15 @@ private function createPatchApplication(array $data)
147128
);
148129

149130
if (!$sourcePackage) {
150-
$this->logger->debug(sprintf('Could not find source package %s (%s) for installed patch, it was removed probably',
151-
$data['source_package']['name'],
152-
$data['source_package']['version']
153-
));
131+
$this->logger->debug(
132+
sprintf(
133+
'Could not find source package %s (%s) for installed patch, it was removed probably',
134+
$data['source_package']['name'],
135+
$data['source_package']['version']
136+
)
137+
);
138+
139+
return null;
154140
}
155141

156142
$targetPackage = $this->installedRepository->findPackage(
@@ -159,35 +145,30 @@ private function createPatchApplication(array $data)
159145
);
160146

161147
if (!$targetPackage) {
162-
throw new \RuntimeException(sprintf('Could not find target package %s (%s) for installed patch',
163-
$data['target_package']['name'],
164-
$data['target_package']['version']
165-
));
148+
throw new \RuntimeException(
149+
sprintf(
150+
'Could not find target package %s (%s) for installed patch',
151+
$data['target_package']['name'],
152+
$data['target_package']['version']
153+
)
154+
);
166155
}
167156

168157
return new PatchApplication($patch, $sourcePackage, $targetPackage, $data['hash']);
169158
}
170159

171-
/**
172-
* @param PackagePatchApplication $packageApplication
173-
* @return array
174-
*/
175-
public function transformPackagePatchApplicationToArray(PackagePatchApplication $packageApplication)
160+
public function transformPackagePatchApplicationToArray(PackagePatchApplication $packageApplication): array
176161
{
177162
return [
178163
'hash' => $packageApplication->getHash(),
179164
'patches' => array_map(
180165
[$this, 'transformPatchApplicationToArray'],
181166
$packageApplication->getApplications()
182-
)
167+
),
183168
];
184169
}
185170

186-
/**
187-
* @param PatchApplication $application
188-
* @return array
189-
*/
190-
public function transformPatchApplicationToArray(PatchApplication $application)
171+
public function transformPatchApplicationToArray(PatchApplication $application): array
191172
{
192173
return [
193174
'hash' => $application->getHash(),
@@ -201,7 +182,7 @@ public function transformPatchApplicationToArray(PatchApplication $application)
201182
'version' => $application->getSourcePackage()->getVersion(),
202183
'ref' => $application->getSourcePackage()->getSourceReference(),
203184
],
204-
'patch' => $application->getPatch()->toArray()
185+
'patch' => $application->getPatch()->toArray(),
205186
];
206187
}
207-
}
188+
}

src/PackagePatchApplication.php

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,35 @@
99
*/
1010
class PackagePatchApplication
1111
{
12-
/**
13-
* @var PatchApplication[]
14-
*/
15-
private $applications;
16-
17-
/**
18-
* @var PackageInterface
19-
*/
20-
private $targetPackage;
21-
22-
/**
23-
* @var string
24-
*/
25-
private $hash;
12+
private string $hash;
2613

2714
/**
28-
* @param PackageInterface $targetPackage
2915
* @param PatchApplication[] $applications
3016
*/
31-
public function __construct(PackageInterface $targetPackage, array $applications)
17+
public function __construct(private PackageInterface $targetPackage, private array $applications)
3218
{
3319
$this->validate($targetPackage, $applications);
3420

35-
$this->applications = $applications;
36-
$this->targetPackage = $targetPackage;
3721
$this->hash = $this->computeHash($targetPackage, $applications);
3822
}
3923

4024
/**
4125
* @return PatchApplication[]
4226
*/
43-
public function getApplications()
27+
public function getApplications(): array
4428
{
4529
return $this->applications;
4630
}
4731

48-
/**
49-
* @return PackageInterface
50-
*/
51-
public function getTargetPackage()
32+
public function getTargetPackage(): PackageInterface
5233
{
5334
return $this->targetPackage;
5435
}
5536

5637
/**
57-
* @param PackageInterface $targetPackage
5838
* @param PatchApplication[] $applications
5939
*/
60-
private function validate(PackageInterface $targetPackage, array $applications)
40+
private function validate(PackageInterface $targetPackage, array $applications): void
6141
{
6242
foreach ($applications as $application) {
6343
if (!$application->getPatch()->canBeAppliedTo($targetPackage)) {
@@ -66,20 +46,15 @@ private function validate(PackageInterface $targetPackage, array $applications)
6646
}
6747
}
6848

69-
/**
70-
* @return string
71-
*/
72-
public function getHash()
49+
public function getHash(): string
7350
{
7451
return $this->hash;
7552
}
7653

7754
/**
78-
* @param PackageInterface $targetPackage
7955
* @param PatchApplication[] $applications
80-
* @return string
8156
*/
82-
private function computeHash(PackageInterface $targetPackage, array $applications)
57+
private function computeHash(PackageInterface $targetPackage, array $applications): string
8358
{
8459
return sha1(
8560
$targetPackage->getSourceReference() .
@@ -90,4 +65,4 @@ private function computeHash(PackageInterface $targetPackage, array $application
9065
)
9166
);
9267
}
93-
}
68+
}

0 commit comments

Comments
 (0)