Skip to content

Commit 2dcdebf

Browse files
authored
Merge pull request #584 from asgrim/532-enable-safe-functions
532: enable Safe functions and phpstan rule
2 parents 4d5fec3 + db7a08a commit 2dcdebf

File tree

67 files changed

+395
-552
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+395
-552
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ jobs:
192192
- name: Setup PHP
193193
uses: shivammathur/setup-php@v2
194194
with:
195-
php-version: 8.1
195+
php-version: 8.5
196196
extensions: intl, sodium, zip
197197
env:
198198
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
"phpstan/phpstan": "^2.1.46",
5050
"phpstan/phpstan-phpunit": "^2.0.16",
5151
"phpstan/phpstan-webmozart-assert": "^2.0",
52-
"phpunit/phpunit": "^10.5.63"
52+
"phpunit/phpunit": "^10.5.63",
53+
"thecodingmachine/phpstan-safe-rule": "^1.4"
5354
},
5455
"replace": {
5556
"symfony/polyfill-php81": "*",

composer.lock

Lines changed: 59 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan-baseline.neon

Lines changed: 0 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,18 @@ parameters:
1818
count: 1
1919
path: src/Command/InstallExtensionsForProjectCommand.php
2020

21-
-
22-
message: '#^Parameter \#1 \$directory of function chdir expects string, string\|false given\.$#'
23-
identifier: argument.type
24-
count: 1
25-
path: src/Command/InstallExtensionsForProjectCommand.php
26-
27-
-
28-
message: '#^Parameter \#1 \$path of function realpath expects string, string\|false given\.$#'
29-
identifier: argument.type
30-
count: 1
31-
path: src/Command/InstallExtensionsForProjectCommand.php
32-
33-
-
34-
message: '#^Strict comparison using \=\=\= between non\-empty\-string and '''' will always evaluate to false\.$#'
35-
identifier: identical.alwaysFalse
36-
count: 1
37-
path: src/Command/InstallExtensionsForProjectCommand.php
38-
3921
-
4022
message: '#^Cannot cast mixed to string\.$#'
4123
identifier: cast.string
4224
count: 2
4325
path: src/Command/RepositoryAddCommand.php
4426

45-
-
46-
message: '#^Parameter \#1 \$haystack of function str_contains expects string, string\|false given\.$#'
47-
identifier: argument.type
48-
count: 1
49-
path: src/Command/RepositoryAddCommand.php
50-
5127
-
5228
message: '#^Cannot cast mixed to string\.$#'
5329
identifier: cast.string
5430
count: 1
5531
path: src/Command/RepositoryRemoveCommand.php
5632

57-
-
58-
message: '#^Parameter \#2 \$content of static method Php\\Pie\\File\\SudoFilePut\:\:contents\(\) expects string, string\|false given\.$#'
59-
identifier: argument.type
60-
count: 1
61-
path: src/Command/SelfUpdateCommand.php
62-
6333
-
6434
message: '#^Cannot cast mixed to string\.$#'
6535
identifier: cast.string
@@ -90,36 +60,6 @@ parameters:
9060
count: 1
9161
path: src/ComposerIntegration/PieComposerInstaller.php
9262

93-
-
94-
message: '#^Method Php\\Pie\\ComposerIntegration\\PieJsonEditor\:\:addRepository\(\) should return string but returns string\|false\.$#'
95-
identifier: return.type
96-
count: 1
97-
path: src/ComposerIntegration/PieJsonEditor.php
98-
99-
-
100-
message: '#^Method Php\\Pie\\ComposerIntegration\\PieJsonEditor\:\:addRequire\(\) should return string but returns string\|false\.$#'
101-
identifier: return.type
102-
count: 1
103-
path: src/ComposerIntegration/PieJsonEditor.php
104-
105-
-
106-
message: '#^Method Php\\Pie\\ComposerIntegration\\PieJsonEditor\:\:excludePackagistOrg\(\) should return string but returns string\|false\.$#'
107-
identifier: return.type
108-
count: 1
109-
path: src/ComposerIntegration/PieJsonEditor.php
110-
111-
-
112-
message: '#^Method Php\\Pie\\ComposerIntegration\\PieJsonEditor\:\:removeRepository\(\) should return string but returns string\|false\.$#'
113-
identifier: return.type
114-
count: 1
115-
path: src/ComposerIntegration/PieJsonEditor.php
116-
117-
-
118-
message: '#^Method Php\\Pie\\ComposerIntegration\\PieJsonEditor\:\:removeRequire\(\) should return string but returns string\|false\.$#'
119-
identifier: return.type
120-
count: 1
121-
path: src/ComposerIntegration/PieJsonEditor.php
122-
12363
-
12464
message: '#^Call to function assert\(\) with true will always evaluate to true\.$#'
12565
identifier: function.alreadyNarrowedType
@@ -138,12 +78,6 @@ parameters:
13878
count: 1
13979
path: src/ComposerIntegration/VendorCleanup.php
14080

141-
-
142-
message: '#^Parameter \#1 \$implementation of method Illuminate\\Contracts\\Container\\ContextualBindingBuilder\:\:give\(\) expects array\|Closure\|string, non\-empty\-string\|false given\.$#'
143-
identifier: argument.type
144-
count: 1
145-
path: src/Container.php
146-
14781
-
14882
message: '#^Call to static method Php\\Pie\\DependencyResolver\\DetermineMinimumStability\:\:assertValidStabilityString\(\) with ''alpha''\|''beta''\|''dev''\|''RC''\|''stable'' will always evaluate to true\.$#'
14983
identifier: staticMethod.alreadyNarrowedType
@@ -216,48 +150,18 @@ parameters:
216150
count: 1
217151
path: src/ExtensionName.php
218152

219-
-
220-
message: '#^Parameter \#2 \$checksum of class Php\\Pie\\File\\BinaryFile constructor expects non\-empty\-string, non\-falsy\-string\|false given\.$#'
221-
identifier: argument.type
222-
count: 1
223-
path: src/File/BinaryFile.php
224-
225153
-
226154
message: '#^Negated boolean expression is always false\.$#'
227155
identifier: booleanNot.alwaysFalse
228156
count: 1
229157
path: src/File/SudoUnlink.php
230158

231-
-
232-
message: '#^Right side of \|\| is always true\.$#'
233-
identifier: booleanOr.rightAlwaysTrue
234-
count: 1
235-
path: src/File/SudoUnlink.php
236-
237-
-
238-
message: '#^Unreachable statement \- code above always terminates\.$#'
239-
identifier: deadCode.unreachable
240-
count: 1
241-
path: src/File/SudoUnlink.php
242-
243159
-
244160
message: '#^If condition is always true\.$#'
245161
identifier: if.alwaysTrue
246162
count: 1
247163
path: src/File/WindowsDelete.php
248164

249-
-
250-
message: '#^Argument of an invalid type list\<string\>\|false supplied for foreach, only iterables are supported\.$#'
251-
identifier: foreach.nonIterable
252-
count: 1
253-
path: src/Installing/Ini/IsExtensionAlreadyInTheIniFile.php
254-
255-
-
256-
message: '#^Parameter \#2 \$array of function array_key_exists expects array, array\|false given\.$#'
257-
identifier: argument.type
258-
count: 2
259-
path: src/Installing/Ini/IsExtensionAlreadyInTheIniFile.php
260-
261165
-
262166
message: '#^Call to function array_key_exists\(\) with ''downloads'' and array\{name\: string, description\: string\|null, abandoned\?\: string\|true, url\?\: string\} will always evaluate to false\.$#'
263167
identifier: function.impossibleType
@@ -354,12 +258,6 @@ parameters:
354258
count: 1
355259
path: test/integration/Installing/WindowsInstallTest.php
356260

357-
-
358-
message: '#^Parameter \#2 \$extractedSourcePath of static method Php\\Pie\\Downloading\\DownloadedPackage\:\:fromPackageAndExtractedPath\(\) expects string, string\|false given\.$#'
359-
identifier: argument.type
360-
count: 2
361-
path: test/unit/ComposerIntegration/BundledPhpExtensionsRepositoryTest.php
362-
363261
-
364262
message: '#^Match expression does not handle remaining value\: string$#'
365263
identifier: match.unhandled
@@ -372,12 +270,6 @@ parameters:
372270
count: 1
373271
path: test/unit/ComposerIntegration/VendorCleanupTest.php
374272

375-
-
376-
message: '#^Parameter \#2 \$extractedSourcePath of static method Php\\Pie\\Downloading\\DownloadedPackage\:\:fromPackageAndExtractedPath\(\) expects string, string\|false given\.$#'
377-
identifier: argument.type
378-
count: 3
379-
path: test/unit/Downloading/DownloadedPackageTest.php
380-
381273
-
382274
message: '#^Parameter \#1 \$phpExt of method Composer\\Package\\Package\:\:setPhpExt\(\) expects array\{extension\-name\?\: string, priority\?\: int, support\-zts\?\: bool, support\-nts\?\: bool, build\-path\?\: string\|null, download\-url\-method\?\: list\<string\>\|string, os\-families\?\: non\-empty\-list\<non\-empty\-string\>, os\-families\-exclude\?\: non\-empty\-list\<non\-empty\-string\>, \.\.\.\}\|null, array\{extension\-name\: null\} given\.$#'
383275
identifier: argument.type
@@ -437,69 +329,3 @@ parameters:
437329
identifier: property.notFound
438330
count: 4
439331
path: test/unit/SelfManage/BuildTools/PhpizeBuildToolFinderTest.php
440-
441-
-
442-
message: '#^Method Php\\PieUnitTest\\SelfManage\\Verify\\FallbackVerificationUsingOpenSslTest\:\:prepareCertificateAndSignature\(\) should return array\{string, string\} but returns array\{mixed, mixed\}\.$#'
443-
identifier: return.type
444-
count: 1
445-
path: test/unit/SelfManage/Verify/FallbackVerificationUsingOpenSslTest.php
446-
447-
-
448-
message: '#^Parameter \#1 \$certificate of function openssl_x509_export expects OpenSSLCertificate\|string, OpenSSLCertificate\|false given\.$#'
449-
identifier: argument.type
450-
count: 2
451-
path: test/unit/SelfManage/Verify/FallbackVerificationUsingOpenSslTest.php
452-
453-
-
454-
message: '#^Parameter \#1 \$csr of function openssl_csr_sign expects OpenSSLCertificateSigningRequest\|string, OpenSSLCertificateSigningRequest\|false given\.$#'
455-
identifier: argument.type
456-
count: 2
457-
path: test/unit/SelfManage/Verify/FallbackVerificationUsingOpenSslTest.php
458-
459-
-
460-
message: '#^Parameter \#1 \$dsseEnvelopePayload of method Php\\PieUnitTest\\SelfManage\\Verify\\FallbackVerificationUsingOpenSslTest\:\:prepareCertificateAndSignature\(\) expects string, string\|false given\.$#'
461-
identifier: argument.type
462-
count: 3
463-
path: test/unit/SelfManage/Verify/FallbackVerificationUsingOpenSslTest.php
464-
465-
-
466-
message: '#^Parameter \#1 \$string of function trim expects string, array\<string\>\|string given\.$#'
467-
identifier: argument.type
468-
count: 1
469-
path: test/unit/SelfManage/Verify/FallbackVerificationUsingOpenSslTest.php
470-
471-
-
472-
message: '#^Parameter \#2 \$ca_certificate of function openssl_csr_sign expects OpenSSLCertificate\|string\|null, OpenSSLCertificate\|false given\.$#'
473-
identifier: argument.type
474-
count: 1
475-
path: test/unit/SelfManage/Verify/FallbackVerificationUsingOpenSslTest.php
476-
477-
-
478-
message: '#^Parameter \#2 \$dsseEnvelopePayload of method Php\\PieUnitTest\\SelfManage\\Verify\\FallbackVerificationUsingOpenSslTest\:\:mockAttestationResponse\(\) expects string, string\|false given\.$#'
479-
identifier: argument.type
480-
count: 3
481-
path: test/unit/SelfManage/Verify/FallbackVerificationUsingOpenSslTest.php
482-
483-
-
484-
message: '#^Parameter \#3 \$private_key of function openssl_csr_sign expects array\|OpenSSLAsymmetricKey\|OpenSSLCertificate\|string, mixed given\.$#'
485-
identifier: argument.type
486-
count: 2
487-
path: test/unit/SelfManage/Verify/FallbackVerificationUsingOpenSslTest.php
488-
489-
-
490-
message: '#^Parameter \#3 \$private_key of function openssl_sign expects array\|OpenSSLAsymmetricKey\|OpenSSLCertificate\|string, mixed given\.$#'
491-
identifier: argument.type
492-
count: 1
493-
path: test/unit/SelfManage/Verify/FallbackVerificationUsingOpenSslTest.php
494-
495-
-
496-
message: '#^Parameter \#3 \$subject of function str_replace expects array\<string\>\|string, mixed given\.$#'
497-
identifier: argument.type
498-
count: 1
499-
path: test/unit/SelfManage/Verify/FallbackVerificationUsingOpenSslTest.php
500-
501-
-
502-
message: '#^Parameter \#4 \$body of class Composer\\Util\\Http\\Response constructor expects string\|null, string\|false given\.$#'
503-
identifier: argument.type
504-
count: 1
505-
path: test/unit/SelfManage/Verify/FallbackVerificationUsingOpenSslTest.php

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ includes:
33
- vendor/bnf/phpstan-psr-container/extension.neon
44
- vendor/phpstan/phpstan-webmozart-assert/extension.neon
55
- vendor/phpstan/phpstan-phpunit/extension.neon
6+
- vendor/thecodingmachine/phpstan-safe-rule/phpstan-safe-rule.neon
67

78
parameters:
89
level: 10

src/Building/UnixBuild.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use function count;
2121
use function file_exists;
2222
use function implode;
23-
use function rename;
23+
use function Safe\rename;
2424
use function sprintf;
2525

2626
use const DIRECTORY_SEPARATOR;

src/Command/InstallExtensionsForProjectCommand.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use Php\Pie\Platform\InstalledPiePackages;
2525
use Php\Pie\Util\Emoji;
2626
use Psr\Container\ContainerInterface;
27+
use Safe\Exceptions\DirException;
28+
use Safe\Exceptions\FilesystemException;
2729
use Symfony\Component\Console\Attribute\AsCommand;
2830
use Symfony\Component\Console\Command\Command;
2931
use Symfony\Component\Console\Input\InputInterface;
@@ -38,14 +40,13 @@
3840
use function array_merge;
3941
use function array_walk;
4042
use function assert;
41-
use function chdir;
4243
use function count;
43-
use function getcwd;
4444
use function implode;
4545
use function in_array;
4646
use function is_dir;
47-
use function is_string;
48-
use function realpath;
47+
use function Safe\chdir;
48+
use function Safe\getcwd;
49+
use function Safe\realpath;
4950
use function sprintf;
5051
use function strtolower;
5152

@@ -104,9 +105,13 @@ public function execute(InputInterface $input, OutputInterface $output): int
104105
$rootPackage = $this->composerFactoryForProject->rootPackage($this->io);
105106

106107
if (ExtensionType::isValid($rootPackage->getType())) {
107-
$cwd = realpath(getcwd());
108-
if (! is_string($cwd) || $cwd === '') {
109-
$this->io->writeError('<error>Failed to determine current working directory.</error>');
108+
try {
109+
$cwd = realpath(getcwd());
110+
} catch (FilesystemException | DirException $e) {
111+
$this->io->writeError(sprintf(
112+
'<error>Failed to determine current working directory: %s</error>',
113+
$e->getMessage(),
114+
));
110115

111116
$restoreWorkingDir();
112117

0 commit comments

Comments
 (0)