Skip to content

Commit

Permalink
Lookup required PHP version from composer.json
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Jul 4, 2018
1 parent 302460f commit 3743d38
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/Checks/CorrectPhpVersionIsInstalled.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@

namespace BeyondCode\SelfDiagnosis\Checks;

use Illuminate\Filesystem\Filesystem;

class CorrectPhpVersionIsInstalled implements Check
{
/** @var Filesystem */
private $filesystem;

public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
}

/**
* The name of the check.
*
Expand All @@ -17,20 +27,34 @@ public function name(): string
/**
* Perform the actual verification of this check.
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
* @return bool
*/
public function check(): bool
{
return version_compare(phpversion(), '7.1.3', '>=');
return version_compare(phpversion(), $this->getRequiredPhpVersion(), '>=');
}

/**
* The error message to display in case the check does not pass.
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
* @return string
*/
public function message() : string
{
return 'You do not have the required PHP version installed.';
return 'You do not have the required PHP version installed.'.PHP_EOL.'Required: '.$this->getRequiredPhpVersion().PHP_EOL.'Used: '.phpversion();
}

/**
* @return mixed
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function getRequiredPhpVersion()
{
$composer = json_decode($this->filesystem->get(base_path('composer.json')), true);
$versionString = array_get($composer, 'require.php');

return str_replace(['^', '~', '<', '>', '='], '', $versionString);
}
}
19 changes: 19 additions & 0 deletions tests/CorrectPhpVersionIsInstalledTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace BeyondCode\SelfDiagnosis\Tests;

use Orchestra\Testbench\TestCase;
use BeyondCode\SelfDiagnosis\Checks\CorrectPhpVersionIsInstalled;

class CorrectPhpVersionIsInstalledTest extends TestCase
{
/** @test */
public function it_detects_the_php_version_from_composer_json()
{
$this->app->setBasePath(__DIR__ . '/fixtures');

$check = app(CorrectPhpVersionIsInstalled::class);

$this->assertSame('7.1.3', $check->getRequiredPhpVersion());
}
}
61 changes: 61 additions & 0 deletions tests/fixtures/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"type": "project",
"require": {
"php": "^7.1.3",
"fideloper/proxy": "^4.0",
"laravel/framework": "5.6.*",
"laravel/tinker": "^1.0"
},
"require-dev": {
"filp/whoops": "^2.0",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^2.0",
"phpunit/phpunit": "^7.0"
},
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"scripts": {
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate"
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
},
"minimum-stability": "dev",
"prefer-stable": true
}

0 comments on commit 3743d38

Please sign in to comment.