Skip to content

Commit 3743d38

Browse files
committed
Lookup required PHP version from composer.json
1 parent 302460f commit 3743d38

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

src/Checks/CorrectPhpVersionIsInstalled.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22

33
namespace BeyondCode\SelfDiagnosis\Checks;
44

5+
use Illuminate\Filesystem\Filesystem;
6+
57
class CorrectPhpVersionIsInstalled implements Check
68
{
9+
/** @var Filesystem */
10+
private $filesystem;
11+
12+
public function __construct(Filesystem $filesystem)
13+
{
14+
$this->filesystem = $filesystem;
15+
}
16+
717
/**
818
* The name of the check.
919
*
@@ -17,20 +27,34 @@ public function name(): string
1727
/**
1828
* Perform the actual verification of this check.
1929
*
30+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
2031
* @return bool
2132
*/
2233
public function check(): bool
2334
{
24-
return version_compare(phpversion(), '7.1.3', '>=');
35+
return version_compare(phpversion(), $this->getRequiredPhpVersion(), '>=');
2536
}
2637

2738
/**
2839
* The error message to display in case the check does not pass.
2940
*
41+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
3042
* @return string
3143
*/
3244
public function message() : string
3345
{
34-
return 'You do not have the required PHP version installed.';
46+
return 'You do not have the required PHP version installed.'.PHP_EOL.'Required: '.$this->getRequiredPhpVersion().PHP_EOL.'Used: '.phpversion();
47+
}
48+
49+
/**
50+
* @return mixed
51+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
52+
*/
53+
public function getRequiredPhpVersion()
54+
{
55+
$composer = json_decode($this->filesystem->get(base_path('composer.json')), true);
56+
$versionString = array_get($composer, 'require.php');
57+
58+
return str_replace(['^', '~', '<', '>', '='], '', $versionString);
3559
}
3660
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace BeyondCode\SelfDiagnosis\Tests;
4+
5+
use Orchestra\Testbench\TestCase;
6+
use BeyondCode\SelfDiagnosis\Checks\CorrectPhpVersionIsInstalled;
7+
8+
class CorrectPhpVersionIsInstalledTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_detects_the_php_version_from_composer_json()
12+
{
13+
$this->app->setBasePath(__DIR__ . '/fixtures');
14+
15+
$check = app(CorrectPhpVersionIsInstalled::class);
16+
17+
$this->assertSame('7.1.3', $check->getRequiredPhpVersion());
18+
}
19+
}

tests/fixtures/composer.json

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"name": "laravel/laravel",
3+
"description": "The Laravel Framework.",
4+
"keywords": [
5+
"framework",
6+
"laravel"
7+
],
8+
"license": "MIT",
9+
"type": "project",
10+
"require": {
11+
"php": "^7.1.3",
12+
"fideloper/proxy": "^4.0",
13+
"laravel/framework": "5.6.*",
14+
"laravel/tinker": "^1.0"
15+
},
16+
"require-dev": {
17+
"filp/whoops": "^2.0",
18+
"fzaninotto/faker": "^1.4",
19+
"mockery/mockery": "^1.0",
20+
"nunomaduro/collision": "^2.0",
21+
"phpunit/phpunit": "^7.0"
22+
},
23+
"autoload": {
24+
"classmap": [
25+
"database/seeds",
26+
"database/factories"
27+
],
28+
"psr-4": {
29+
"App\\": "app/"
30+
}
31+
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"Tests\\": "tests/"
35+
}
36+
},
37+
"extra": {
38+
"laravel": {
39+
"dont-discover": []
40+
}
41+
},
42+
"scripts": {
43+
"post-root-package-install": [
44+
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
45+
],
46+
"post-create-project-cmd": [
47+
"@php artisan key:generate"
48+
],
49+
"post-autoload-dump": [
50+
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
51+
"@php artisan package:discover"
52+
]
53+
},
54+
"config": {
55+
"preferred-install": "dist",
56+
"sort-packages": true,
57+
"optimize-autoloader": true
58+
},
59+
"minimum-stability": "dev",
60+
"prefer-stable": true
61+
}

0 commit comments

Comments
 (0)