Skip to content

Commit 1708b37

Browse files
committed
Auto-fix WordPress stubs <-> PHP 7.3 Polyfill collision
1 parent a32269e commit 1708b37

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"phpstan/phpstan": "^0.11.5"
1010
},
1111
"require-dev": {
12+
"composer/composer": "^1.8.6",
1213
"consistence/coding-standard": "^3.8",
1314
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.4",
1415
"jakub-onderka/php-parallel-lint": "^1.0",
@@ -28,5 +29,9 @@
2829
}
2930
},
3031
"minimum-stability": "dev",
31-
"prefer-stable": true
32+
"prefer-stable": true,
33+
"scripts": {
34+
"post-install-cmd": "PHPStan\\WordPress\\Composer\\FixWpStubs::php73Polyfill",
35+
"post-update-cmd": "PHPStan\\WordPress\\Composer\\FixWpStubs::php73Polyfill"
36+
}
3237
}

src/Composer/FixWpStubs.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types = 1);
2+
/**
3+
* Fix WordPress stubs.
4+
*/
5+
6+
namespace PHPStan\WordPress\Composer;
7+
8+
use Composer\Script\Event;
9+
10+
class FixWpStubs
11+
{
12+
const STUBSFILE = '/giacocorsiglia/wordpress-stubs/wordpress-stubs.php';
13+
14+
public static function php73Polyfill(Event $event)
15+
{
16+
if (! class_exists('\Symfony\Polyfill\Php73\Php73')) {
17+
return;
18+
}
19+
20+
echo 'Removing duplicate is_countable() ... ';
21+
$vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
22+
$stubsFile = $vendorDir . self::STUBSFILE;
23+
24+
$stubs = file_get_contents($stubsFile);
25+
if ($stubs === false) {
26+
echo 'WordPress stubs not found.';
27+
return;
28+
}
29+
$fixedStubs = preg_replace('/(\n)(function is_countable)/', '$1// $2', $stubs);
30+
31+
$numberOfBytes = file_put_contents($stubsFile, $fixedStubs);
32+
$message = 'OK.';
33+
if ($numberOfBytes === false) {
34+
$message = 'FAILED.';
35+
}
36+
37+
echo $message . "\n";
38+
}
39+
}

0 commit comments

Comments
 (0)