Skip to content

Commit

Permalink
Merge pull request #9 from dereuromark/bugfix/php7
Browse files Browse the repository at this point in the history
Upgrade to PHP7.2+
  • Loading branch information
dereuromark authored Sep 8, 2021
2 parents e5e21dd + eb93cb9 commit 084072c
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 26 deletions.
105 changes: 105 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
name: CI

on:
pull_request:
push:
branches:
- master
workflow_dispatch:

jobs:
testsuite:
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
php-version: ['7.2', '8.0']

steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl, bcmath
coverage: pcov

- name: Composer install
run: |
composer --version
composer install
- name: Run PHPUnit
run: |
if [[ ${{ matrix.php-version }} == '7.2' ]]; then
vendor/bin/phpunit --coverage-clover=coverage.xml
else
vendor/bin/phpunit
fi
- name: Code Coverage Report
if: success() && matrix.php-version == '7.2'
uses: codecov/codecov-action@v1

validation:
name: Coding Standard & Static Analysis
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extensions: mbstring, intl, bcmath
coverage: none

- name: Composer Install
run: composer install

- name: Run phpstan
run: composer stan-setup && vendor/bin/phpstan analyse --error-format=github

- name: Run phpcs
run: composer cs-check

prefer-lowest:
runs-on: ubuntu-18.04
strategy:
fail-fast: false
matrix:
php-version: [
'7.2'
]

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl, bcmath
coverage: none

- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Composer cache
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Composer prefer-lowest
run: |
composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable
- name: Run PHPUnit
run: |
vendor/bin/phpunit
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: php

php:
- 5.6
- 7.4
- 7.3
- 8.0

env:
global:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Composer Prefer Lowest Validator
[![Build Status](https://api.travis-ci.org/dereuromark/composer-prefer-lowest.svg?branch=master)](https://travis-ci.org/dereuromark/composer-prefer-lowest)
[![Latest Stable Version](https://poser.pugx.org/dereuromark/composer-prefer-lowest/v/stable.svg)](https://packagist.org/packages/dereuromark/composer-prefer-lowest)
[![Minimum PHP Version](http://img.shields.io/badge/php-%3E%3D%205.6-8892BF.svg)](https://php.net/)
[![Minimum PHP Version](http://img.shields.io/badge/php-%3E%3D%207.2-8892BF.svg)](https://php.net/)
[![License](https://poser.pugx.org/dereuromark/composer-prefer-lowest/license.svg)](https://packagist.org/packages/dereuromark/composer-prefer-lowest)
[![Coding Standards](https://img.shields.io/badge/cs-PSR--2--R-yellow.svg)](https://github.com/php-fig-rectified/fig-rectified-standards)
[![Total Downloads](https://poser.pugx.org/dereuromark/composer-prefer-lowest/d/total.svg)](https://packagist.org/packages/dereuromark/composer-prefer-lowest)
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
}
],
"require": {
"php": ">=5.6",
"php": ">=7.2",
"ext-json": "*",
"composer/semver": "^1.4"
},
"require-dev": {
"phpunit/phpunit": "^5.7 || ^6.0 || ^7.0",
"phpunit/phpunit": "^8.5 || ^9.5",
"fig-r/psr2r-sniffer": "dev-master"
},
"autoload": {
Expand All @@ -34,8 +34,8 @@
"prefer-stable": true,
"scripts": {
"test": "phpunit",
"phpstan": "phpstan analyse -c tests/phpstan.neon -l 5 src/",
"phpstan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan && mv composer.backup composer.json",
"stan": "phpstan analyse",
"stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan && mv composer.backup composer.json",
"cs-check": "phpcs -p --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --extensions=php src/",
"cs-fix": "phpcbf -v --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --extensions=php src/"
},
Expand Down
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters:
level: 8
paths:
- src/

bootstrapFiles:
- tests/bootstrap.php

checkMissingIterableValueType: false
39 changes: 26 additions & 13 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,39 @@
use Composer\Semver\Comparator;
use Composer\Semver\Semver;
use Composer\Semver\VersionParser;
use RuntimeException;

class Validator {

const CODE_SUCCESS = 0;
const CODE_ERROR = 1;
public const CODE_SUCCESS = 0;
public const CODE_ERROR = 1;

const MAJORS_ONLY = 'majors-only';
const MAJORS_ONLY_SHORT = 'm';
public const MAJORS_ONLY = 'majors-only';
public const MAJORS_ONLY_SHORT = 'm';

/**
* @param string $path
* @param string[] $options
* @return int Returns 0 on success, otherwise error code.
*/
public function validate($path, array $options = []) {
public function validate(string $path, array $options = []): int {
if (!$path) {
echo 'Path to composer.lock file not found' . PHP_EOL;

return static::CODE_ERROR;
}

$lockFilePath = $path . 'composer.lock';
if (!file_exists($lockFilePath)) {
echo 'composer.lock file not found: ' . $lockFilePath . PHP_EOL;

return static::CODE_ERROR;
}

$jsonFilePath = $path . 'composer.json';
if (!file_exists($jsonFilePath)) {
echo 'composer.json file not found: ' . $jsonFilePath . PHP_EOL;

return static::CODE_ERROR;
}

Expand All @@ -49,11 +53,12 @@ public function validate($path, array $options = []) {
*
* @return int
*/
protected function compare($lockFile, $jsonFile, array $options) {
protected function compare(string $lockFile, string $jsonFile, array $options): int {
$jsonInfo = $this->parseJsonFromFile($jsonFile);
$lockInfo = $jsonInfo !== null ? $this->parseLockFromFile($lockFile, $jsonInfo) : null;
if ($jsonInfo === null || $lockInfo === null) {
echo 'Make sure composer.json and composer.lock files are valid and that you have at least one dependency in require.';

return static::CODE_ERROR;
}

Expand Down Expand Up @@ -103,7 +108,7 @@ protected function compare($lockFile, $jsonFile, array $options) {
*
* @return string
*/
protected function definedMinimum(array $jsonInfo, $package) {
protected function definedMinimum(array $jsonInfo, string $package): string {
$constraints = $jsonInfo[$package]['version'];
// We only need the first
$constraint = (new MinimumVersionParser())->parseConstraints($constraints);
Expand All @@ -113,10 +118,14 @@ protected function definedMinimum(array $jsonInfo, $package) {

/**
* @param string $jsonFile
* @throws \RuntimeException
* @return array|null
*/
protected function parseJsonFromFile($jsonFile) {
protected function parseJsonFromFile(string $jsonFile): ?array {
$content = file_get_contents($jsonFile);
if ($content === false) {
throw new RuntimeException('Cannot read file: ' . $jsonFile);
}
$json = json_decode($content, true);

if (!$json || empty($json['require'])) {
Expand Down Expand Up @@ -154,7 +163,7 @@ protected function parseJsonFromFile($jsonFile) {
*
* @return string
*/
protected function stripVersion($version) {
protected function stripVersion(string $version): string {
$from = [
'>=',
'^',
Expand All @@ -167,10 +176,14 @@ protected function stripVersion($version) {
/**
* @param string $lockFile
* @param array $jsonInfo
* @throws \RuntimeException
* @return array|null
*/
protected function parseLockFromFile($lockFile, array $jsonInfo) {
protected function parseLockFromFile(string $lockFile, array $jsonInfo): ?array {
$content = file_get_contents($lockFile);
if ($content === false) {
throw new RuntimeException('Cannot read file: ' . $lockFile);
}
$json = json_decode($content, true);
if (!$json) {
return null;
Expand Down Expand Up @@ -201,7 +214,7 @@ protected function parseLockFromFile($lockFile, array $jsonInfo) {
* @param string $version
* @return string
*/
protected function normalizeVersion($version) {
protected function normalizeVersion(string $version): string {
$version = (new VersionParser())->normalize($version);

if (strpos($version, '-') !== false) {
Expand All @@ -216,7 +229,7 @@ protected function normalizeVersion($version) {
*
* @return array
*/
protected function parseOptions(array $options) {
protected function parseOptions(array $options): array {
$result = [
static::MAJORS_ONLY => false,
];
Expand All @@ -234,7 +247,7 @@ protected function parseOptions(array $options) {
*
* @return bool
*/
protected function isAllowedNonMajor($definedMinimum, $version, array $options) {
protected function isAllowedNonMajor(string $definedMinimum, string $version, array $options): bool {
if (!$options[static::MAJORS_ONLY]) {
return false;
}
Expand Down
9 changes: 6 additions & 3 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

define('DS', DIRECTORY_SEPARATOR);
define('TESTS', __DIR__ . DS);
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
if (!defined('TESTS')) {
define('TESTS', __DIR__ . DS);
}
3 changes: 0 additions & 3 deletions tests/phpstan.neon

This file was deleted.

0 comments on commit 084072c

Please sign in to comment.