4949use function microtime ;
5050use function min ;
5151use function parse_url ;
52+ use function php_uname ;
5253use function sprintf ;
5354use function unlink ;
5455use const JSON_INVALID_UTF8_IGNORE ;
5556use const PHP_BINARY ;
57+ use const PHP_OS_FAMILY ;
5658use const PHP_URL_PORT ;
5759use const PHP_VERSION_ID ;
5860
@@ -338,37 +340,51 @@ private function downloadPhar(
338340 string $ infoPath ,
339341 ): void
340342 {
341- $ currentVersion = null ;
343+ $ currentUrl = null ;
342344 $ branch = '2.2.x ' ;
345+ // the PHAR bundles a native binary, download-api answers the one for this system
346+ $ os = PHP_OS_FAMILY ;
347+ $ arch = php_uname ('m ' );
343348 if (is_file ($ pharPath ) && is_file ($ infoPath )) {
344- /** @var array{version: string, date: string, branch?: string} $currentInfo */
349+ /** @var array{version: string, date: string, branch?: string, url?: string, os?: string, arch?: string } $currentInfo */
345350 $ currentInfo = Json ::decode (FileReader ::read ($ infoPath ), Json::FORCE_ARRAY );
346- $ currentVersion = $ currentInfo ['version ' ];
347- $ currentBranch = $ currentInfo ['branch ' ] ?? 'master ' ;
348- $ currentDate = DateTime ::createFromFormat (DateTime::ATOM , $ currentInfo ['date ' ]);
349- if ($ currentDate === false ) {
350- throw new ShouldNotHappenException ();
351- }
352- if (
353- $ currentBranch === $ branch
354- && (new DateTimeImmutable ('' , new DateTimeZone ('UTC ' ))) <= $ currentDate ->modify ('+24 hours ' )
355- ) {
356- return ;
351+ if (($ currentInfo ['os ' ] ?? null ) === $ os && ($ currentInfo ['arch ' ] ?? null ) === $ arch ) {
352+ $ currentUrl = $ currentInfo ['url ' ] ?? null ;
357353 }
358354
359- $ output ->writeln ('<fg=green>Checking if there \'s a new PHPStan Pro release...</> ' );
355+ if ($ currentUrl === null ) {
356+ // downloaded on another system (the directory was carried over, or is shared with a Docker container),
357+ // or by an older PHPStan; its native binary might not run here
358+ @unlink ($ pharPath );
359+ @unlink ($ infoPath );
360+ } else {
361+ $ currentBranch = $ currentInfo ['branch ' ] ?? 'master ' ;
362+ $ currentDate = DateTime ::createFromFormat (DateTime::ATOM , $ currentInfo ['date ' ]);
363+ if ($ currentDate === false ) {
364+ throw new ShouldNotHappenException ();
365+ }
366+ if (
367+ $ currentBranch === $ branch
368+ && (new DateTimeImmutable ('' , new DateTimeZone ('UTC ' ))) <= $ currentDate ->modify ('+24 hours ' )
369+ ) {
370+ return ;
371+ }
372+
373+ $ output ->writeln ('<fg=green>Checking if there \'s a new PHPStan Pro release...</> ' );
374+ }
360375 }
361376
362377 $ client = $ this ->httpClientFactory ->createClient ([]);
363378
364- $ latestUrl = sprintf ('https://fixer-download-api.phpstan.com/latest?%s ' , http_build_query (['phpVersion ' => PHP_VERSION_ID , 'branch ' => $ branch ]));
379+ $ latestUrl = sprintf ('https://fixer-download-api.phpstan.com/latest?%s ' , http_build_query (['phpVersion ' => PHP_VERSION_ID , 'branch ' => $ branch, ' os ' => $ os , ' arch ' => $ arch ]));
365380
366381 /**
367382 * @var array{url: string, version: string} $latestInfo
368383 */
369384 $ latestInfo = Json ::decode ($ client ->get ($ latestUrl )->getBody ()->getContents (), Json::FORCE_ARRAY );
370- if ($ currentVersion !== null && $ latestInfo ['version ' ] === $ currentVersion ) {
371- $ this ->writeInfoFile ($ infoPath , $ latestInfo ['version ' ], $ branch );
385+ // the URL changes with the version, and with the PHAR download-api picks for this system
386+ if ($ latestInfo ['url ' ] === $ currentUrl ) {
387+ $ this ->writeInfoFile ($ infoPath , $ latestInfo , $ branch , $ os , $ arch );
372388 $ output ->writeln ('<fg=green>You \'re running the latest PHPStan Pro!</> ' );
373389 return ;
374390 }
@@ -405,7 +421,7 @@ private function downloadPhar(
405421 $ output ->writeln ('' );
406422 $ output ->writeln ('' );
407423
408- $ this ->writeInfoFile ($ infoPath , $ latestInfo[ ' version ' ] , $ branch );
424+ $ this ->writeInfoFile ($ infoPath , $ latestInfo , $ branch, $ os , $ arch );
409425 }
410426
411427 private function printDownloadError (OutputInterface $ output , Throwable $ e ): void
@@ -414,10 +430,16 @@ private function printDownloadError(OutputInterface $output, Throwable $e): void
414430 $ output ->writeln ('' );
415431 }
416432
417- private function writeInfoFile (string $ infoPath , string $ version , string $ branch ): void
433+ /**
434+ * @param array{url: string, version: string} $latestInfo
435+ */
436+ private function writeInfoFile (string $ infoPath , array $ latestInfo , string $ branch , string $ os , string $ arch ): void
418437 {
419438 FileWriter ::write ($ infoPath , Json ::encode ([
420- 'version ' => $ version ,
439+ 'version ' => $ latestInfo ['version ' ],
440+ 'url ' => $ latestInfo ['url ' ],
441+ 'os ' => $ os ,
442+ 'arch ' => $ arch ,
421443 'branch ' => $ branch ,
422444 'date ' => (new DateTimeImmutable ('' , new DateTimeZone ('UTC ' )))->format (DateTime::ATOM ),
423445 ]));
0 commit comments