Skip to content

Commit

Permalink
Merge 5701209 into e6783bd
Browse files Browse the repository at this point in the history
  • Loading branch information
c-harris committed Sep 19, 2023
2 parents e6783bd + 5701209 commit 4d674aa
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 60 deletions.
107 changes: 107 additions & 0 deletions .github/actions/php_ver/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
name: 'PHP Releases'
branding:
icon: 'plus'
color: 'yellow'
description: 'Generates an array of the latest, minimum security, and minimum active PHP releases, along with user defined versions'
inputs:
releases:
required: false
description: 'Add specific versions to the matrix array'
outputs:
range:
description: "Releases Array"
value: ${{ steps.return-response.outputs.releases-array }}
runs:
using: "composite"
steps:
- uses: actions/checkout@v3

- name: Fetch Cache
id: fetch-cache
uses: actions/cache@v3
with:
path: .temp
key: temp

- name: Store user input
id: store-user-input
shell: bash
run: |
echo "user-input=$(echo ${{ inputs.releases }})" >> $GITHUB_OUTPUT
- name: Make Latest Request
id: make-latest-request
uses: fjogeleit/http-request-action@v1
if: steps.fetch-cache.outputs.cache-hit != 'true'
with:
url: 'https://phpreleases.com/api/releases/latest'
method: 'GET'

- name: Make Security Request
id: make-security-request
uses: fjogeleit/http-request-action@v1
if: steps.fetch-cache.outputs.cache-hit != 'true'
with:
url: 'https://phpreleases.com/api/releases/minimum-supported/security'
method: 'GET'

- name: Make Active Request
id: make-active-request
uses: fjogeleit/http-request-action@v1
if: steps.fetch-cache.outputs.cache-hit != 'true'
with:
url: 'https://phpreleases.com/api/releases/minimum-supported/active'
method: 'GET'

- name: Parse Latest
id: parse-latest
if: steps.fetch-cache.outputs.cache-hit != 'true'
shell: bash
run: |
latest=${{ steps.make-latest-request.outputs.response }}
IFS='.'
read -ra arr <<< "$latest"
echo "latest-release=$(echo ${arr[0]}.${arr[1]})" >> $GITHUB_OUTPUT
- name: Parse Minimum Supported
id: parse-min
if: steps.fetch-cache.outputs.cache-hit != 'true'
shell: bash
run: |
echo "min-active-release=$(echo ${{ fromJSON(steps.make-active-request.outputs.response).provided.major }}.${{ fromJSON(steps.make-active-request.outputs.response).provided.minor }})" >> $GITHUB_OUTPUT
echo "min-security-release=$(echo ${{ fromJSON(steps.make-security-request.outputs.response).provided.major }}.${{ fromJSON(steps.make-security-request.outputs.response).provided.minor }})" >> $GITHUB_OUTPUT
- name: Return Response
id: return-response
run: |
if [ "${{ steps.fetch-cache.outputs.cache-hit }}" != 'true' ]; then
echo "Data fetched from Api"
latest="${{ steps.parse-latest.outputs.latest-release }}"
minAct="${{ steps.parse-min.outputs.min-active-release }}"
minSec="${{ steps.parse-min.outputs.min-security-release }}"
else
echo "Data fetched from cache"
latest=$(cat .temp/latest.txt)
minAct=$(cat .temp/min-active.txt)
minSec=$(cat .temp/min-security.txt)
fi
if [ -z "${{ steps.store-user-input.outputs.user-input }}" ]; then
echo "releases-array=$(echo [${latest}, ${minAct}, ${minSec}])" >> $GITHUB_OUTPUT
else
userInput="${{ steps.store-user-input.outputs.user-input }}"
IFS=', ' read -r -a array <<< "$userInput"
array+=(${latest} ${minAct} ${minSec})
deduped=($(echo "${array[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
printf -v formatted ',%s' "${deduped[@]}"
formatted=${formatted:1}
echo "releases-array=$(echo [${formatted}])" >> $GITHUB_OUTPUT
fi
shell: bash
36 changes: 28 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,28 @@ on:
- main
- master
jobs:
output_releases:
name: Generate PHP Releases Array
runs-on: ubuntu-latest
outputs:
range: ${{ steps.releases.outputs.range }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Fetch Current Releases
uses: ./.github/actions/php_ver
id: releases
tests:
needs: output_releases
runs-on: ${{ matrix.operating-system }}
strategy:
fail-fast: false
matrix:
operating-system: [ ubuntu-latest, windows-latest ]
php-version: ['7.4', '8.0', '8.4' ]
operating-system: [ ubuntu-latest]
php-version: ${{ fromJSON(needs.output_releases.outputs.range) }}
dependencies: [locked, lowest, highest ]
coverage-driver: [ pcov ]
name: Tests on ${{ matrix.operating-system }}, PHP ${{ matrix.php-version }} (${{ matrix.dependencies }};)
coverage-driver: [ xdebug ]
name: Tests on ${{ matrix.operating-system }}, PHP ${{ matrix.php-version }} (${{ matrix.dependencies }}; ${{ matrix.coverage-driver }})
steps:
- name: Checkout code
uses: actions/checkout@v3
Expand All @@ -26,7 +38,7 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
coverage: ${{ matrix.coverage-driver }}
ini-values: memory_limit=512M, xdebug.mode=off
ini-values: memory_limit=512M, xdebug.mode=coverage
tools: composer
env:
GITHUB_TOKEN: ${{ secrets.COMPOSER_GITHUB_TOKEN }}
Expand Down Expand Up @@ -65,7 +77,12 @@ jobs:

- name: Run unit tests
shell: bash
run: vendor/bin/phpunit
run: vendor/bin/phpunit --coverage-clover=clover.xml --path-coverage
- name: Coveralls Parallel
uses: coverallsapp/github-action@v2
with:
parallel: true
flag-name: "${{ matrix.operating-system }}, PHP ${{ matrix.php-version }} (${{ matrix.dependencies }}; ${{ matrix.coverage-driver }})"

# This is a meta job to avoid to have to constantly change the protection rules
# whenever we touch the matrix.
Expand All @@ -78,11 +95,14 @@ jobs:
- name: Successful run
if: ${{ !(contains(needs.*.result, 'failure')) }}
run: exit 0

- name: Failing run
if: ${{ contains(needs.*.result, 'failure') }}
run: exit 1

- name: Coveralls Finished
uses: coverallsapp/github-action@v2
with:
parallel-finished: true
carryforward: "run-1,run-2"

infection:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ build_failure_conditions:

build:
environment:
php: 7.4
php: 8.2
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@
}
},
"require": {
"php": ">=7.1 | >=8.0",
"php": ">=8.0",
"myclabs/php-enum": "^1.7"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"phpunit/php-code-coverage": "^9.2.28"
},
"config": {
"allow-plugins": {
"infection/extension-installer": true
}
}
}
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 44 additions & 47 deletions src/BitMask.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,41 @@

use BadMethodCallException;
use MyCLabs\Enum\Enum;
use ReflectionException;
use UnexpectedValueException;

abstract class BitMask extends Enum
{
// Because 0 is the identity element when ORing a bitmask, we have to special-case it when ANDing bitmasks
// (akin to ordinary division by 0, the addition identity element on integers/rationals/reals)

protected function isFlag(int $flag): bool
protected static function assertValidValueReturningKey($value): string
{
return 0 === $flag ? 0 === $this->value : (($this->value & $flag) == $flag);
}
if (!static::isValid($value)) {
throw new UnexpectedValueException("Value '$value' is not part of the enum ".static::class);
}

protected function isComponentOfFlag(int $flag): bool
{
return 0 === $flag ? $flag === $this->value : (($this->value & $flag) == $this->value);
return implode('|', self::getKeyArray($value));
}

protected function setFlag(int $flag, bool $value)
/**
* @param $value
*
* @return bool
*/
public static function isValid($value): bool
{
if ($value) {
$this->value = 0 === $flag ? 0 : $this->value | $flag;
} else {
$this->value = 0 === $flag ? $this->value : $this->value & ~$flag;
}
$min = min(static::toArray());
$max = max(static::toArray()) * 2 - 1;

return $this;
return $value >= $min && $value <= $max;
}

/**
* @param $name
* @param $arguments
*
* @throws \ReflectionException
* @throws ReflectionException
*
* @return bool|self
*/
Expand All @@ -56,32 +58,15 @@ public function __call($name, $arguments)
}

/**
* @param $value
*
* @throws \ReflectionException
*
* @return bool
*/
public static function isValid($value): bool
{
$min = min(static::toArray());
$max = max(static::toArray()) * 2 - 1;

return $value >= $min && $value <= $max;
}

/**
* @throws \ReflectionException
*
* @return array
*/
public static function toArray(): array
{
$firstTime = !isset(static::$cache[static::class]);
$array = array_filter(parent::toArray(), function ($value): bool {
$array = array_filter(parent::toArray(), static function ($value): bool {
return is_scalar($value);
});
$firstTime && array_walk($array, function ($item): void {
$firstTime && array_walk($array, static function ($item): void {
if (!is_int($item)) {
throw new UnexpectedValueException(
sprintf('All defined Const on Enum %s should be integers', static::class)
Expand All @@ -95,36 +80,27 @@ public static function toArray(): array
/**
* @return string
*/
public function getKey()
public function getKey(): string
{
return implode('|', $this->getKeyArray($this->value));
return implode('|', self::getKeyArray($this->value));
}

/**
* @param mixed $value
*
* @return array
*/
public static function getKeyArray($value)
public static function getKeyArray(mixed $value): array
{
$f = array_filter(static::toArray(), function ($key) use (&$value) {
$f = array_filter(static::toArray(), static function ($key) use (&$value) {
return $value & $key;
});

return array_keys($f);
}

protected static function assertValidValueReturningKey($value): string
{
if (!static::isValid($value)) {
throw new \UnexpectedValueException("Value '$value' is not part of the enum ".static::class);
}

return implode('|', self::getKeyArray($value));
}

/**
* @throws \ReflectionException
* @throws ReflectionException
*
* @return string
*/
Expand All @@ -148,4 +124,25 @@ public function getName(): string

return array_pop($path);
}

protected function isFlag(int $flag): bool
{
return 0 === $flag ? 0 === $this->value : (($this->value & $flag) === $flag);
}

protected function isComponentOfFlag(int $flag): bool
{
return 0 === $flag ? $flag === $this->value : (($this->value & $flag) === $this->value);
}

protected function setFlag(int $flag, bool $value): static
{
if ($value) {
$this->value = 0 === $flag ? 0 : $this->value | $flag;
} else {
$this->value = 0 === $flag ? $this->value : $this->value & ~$flag;
}

return $this;
}
}

0 comments on commit 4d674aa

Please sign in to comment.