Skip to content

Commit

Permalink
Github Action : PHP (PHPCSFixer / PHPStan / PHPUnit / Roave Backwards…
Browse files Browse the repository at this point in the history
… Compatibility Check)
  • Loading branch information
Progi1984 committed Sep 26, 2023
1 parent fa39e83 commit 087d3e8
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 29 deletions.
116 changes: 116 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
name: PHP
on:
push:
branches:
- master
pull_request:

jobs:
php-cs-fixer:
name: PHP CS Fixer
runs-on: ubuntu-latest
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extensions: xml

- uses: actions/checkout@v4

- name: Validate composer config
run: composer validate --strict

- name: Composer Install
run: composer global require friendsofphp/php-cs-fixer

- name: Add environment path
run: export PATH="$PATH:$HOME/.composer/vendor/bin"

- name: Run PHPCSFixer
run: php-cs-fixer fix --dry-run --diff

phpstan:
name: PHP Static Analysis
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php:
- '7.1'
- '7.2'
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: xml

- uses: actions/checkout@v4

- name: Composer Install
run: composer install --ansi --prefer-dist --no-interaction --no-progress

- name: Run phpstan
run: ./vendor/bin/phpstan analyse -c phpstan.neon.dist

phpunit:
name: PHPUnit
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php:
- '7.1'
- '7.2'
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: xml
coverage: ${{ (matrix.php == '8.1') && 'xdebug' || 'none' }}

- uses: actions/checkout@v4

- name: Install dependencies
run: composer install --ansi --prefer-dist --no-interaction --no-progress

- name: Run PHPUnit
if: matrix.php != '8.1'
run: ./vendor/bin/phpunit -c phpunit.xml.dist

- name: Run PHPUnit (w CodeCoverage)
if: matrix.php == '8.1'
run: XDEBUG_MODE=coverage ./vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover build/clover.xml

- name: Upload coverage results to Coveralls
if: matrix.php == '8.1'
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.4.3/php-coveralls.phar
chmod +x php-coveralls.phar
php php-coveralls.phar --coverage_clover=build/clover.xml --json_path=build/coveralls-upload.json -vvv
#roave-backwards-compatibility-check:
# name: Roave Backwards Compatibility Check
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# with:
# fetch-depth: 0
# - name: "Check for BC breaks"
# run: docker run -u $(id -u) -v $(pwd):/app nyholm/roave-bc-check-ga
45 changes: 45 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

$config = new PhpCsFixer\Config();

$config
->setUsingCache(true)
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'array_indentation' => true,
'cast_spaces' => [
'space' => 'single',
],
'combine_consecutive_issets' => true,
'concat_space' => [
'spacing' => 'one',
],
'error_suppression' => [
'mute_deprecation_error' => false,
'noise_remaining_usages' => false,
'noise_remaining_usages_exclude' => [],
],
'function_to_constant' => false,
'global_namespace_import' => true,
'method_chaining_indentation' => true,
'no_alias_functions' => false,
'no_superfluous_phpdoc_tags' => false,
'non_printable_character' => [
'use_escape_sequences_in_strings' => true,
],
'phpdoc_align' => [
'align' => 'left',
],
'phpdoc_summary' => false,
'protected_to_private' => false,
'self_accessor' => false,
'yoda_style' => false,
'single_line_throw' => false,
'no_alias_language_construct_call' => false,
])
->getFinder()
->in(__DIR__)
->exclude('vendor');

return $config;
16 changes: 12 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "phpoffice/wmf",
"description": "WMF - Manipulate WMF Images",
"keywords": ["PHP", "wmf", "emf", "image"],
"homepage": "https://phpoffice.github.io/WMF/",
"type": "library",
"require-dev": {
"phpunit/phpunit": "10"
},
"license": "MIT",
"autoload": {
"psr-4": {
Expand All @@ -20,5 +20,13 @@
"name": "Progi1984",
"homepage": "https://lefevre.dev"
}
]
],
"require": {
"php": "^7.1|^8.0",
"ext-gd": "*"
},
"require-dev": {
"phpunit/phpunit": "^7.0 || ^9.0",
"phpstan/phpstan": "^0.12.88 || ^1.0.0"
}
}
9 changes: 9 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters:
level: 7
bootstrapFiles:
- vendor/autoload.php
paths:
- src
- tests
reportUnmatchedIgnoredErrors: false
ignoreErrors:
File renamed without changes.
30 changes: 21 additions & 9 deletions src/WMF/Reader/GD.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace PhpOffice\WMF\Reader;

use GdImage;
use PhpOffice\WMF\Exception\WMFException;
use function imagedestroy;

class GD implements ReaderInterface
{
/**
* @var resource
* @var GdImage|false
*/
protected $gd;

Check failure on line 15 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.2)

Property PhpOffice\WMF\Reader\GD::$gd has unknown class GdImage as its type.

Check failure on line 15 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.4)

Property PhpOffice\WMF\Reader\GD::$gd has unknown class GdImage as its type.

Check failure on line 15 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.3)

Property PhpOffice\WMF\Reader\GD::$gd has unknown class GdImage as its type.

Check failure on line 15 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.1)

Property PhpOffice\WMF\Reader\GD::$gd has unknown class GdImage as its type.
/**
Expand Down Expand Up @@ -41,6 +41,10 @@ class GD implements ReaderInterface
* @var int
*/
protected $unitPerInch;
/**
* @var array<array<>>
*/
protected $gdiObjects = [];

Check failure on line 47 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

PHPDoc tag @var has invalid value (array<array<>>): Unexpected token ">", expected type at offset 28

Check failure on line 47 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Property PhpOffice\WMF\Reader\GD::$gdiObjects has no type specified.

Check failure on line 47 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

PHPDoc tag @var has invalid value (array<array<>>): Unexpected token ">", expected type at offset 28

Check failure on line 47 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Property PhpOffice\WMF\Reader\GD::$gdiObjects has no type specified.

Check failure on line 47 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.2)

PHPDoc tag @var has invalid value (array<array<>>): Unexpected token ">", expected type at offset 28

Check failure on line 47 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.2)

Property PhpOffice\WMF\Reader\GD::$gdiObjects has no type specified.

Check failure on line 47 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.0)

PHPDoc tag @var has invalid value (array<array<>>): Unexpected token ">", expected type at offset 28

Check failure on line 47 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.0)

Property PhpOffice\WMF\Reader\GD::$gdiObjects has no type specified.

Check failure on line 47 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.4)

PHPDoc tag @var has invalid value (array<array<>>): Unexpected token ">", expected type at offset 28

Check failure on line 47 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.4)

Property PhpOffice\WMF\Reader\GD::$gdiObjects has no type specified.

Check failure on line 47 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.3)

PHPDoc tag @var has invalid value (array<array<>>): Unexpected token ">", expected type at offset 28

Check failure on line 47 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.3)

Property PhpOffice\WMF\Reader\GD::$gdiObjects has no type specified.

Check failure on line 47 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.1)

PHPDoc tag @var has invalid value (array<array<>>): Unexpected token ">", expected type at offset 28

Check failure on line 47 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.1)

Property PhpOffice\WMF\Reader\GD::$gdiObjects has no type specified.

Check failure on line 47 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

PHPDoc tag @var has invalid value (array<array<>>): Unexpected token ">", expected type at offset 28

Check failure on line 47 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

Property PhpOffice\WMF\Reader\GD::$gdiObjects has no type specified.

const META_EOF = 0x0000;
const META_SETPOLYFILLMODE = 0x0106;
Expand All @@ -52,8 +56,6 @@ class GD implements ReaderInterface
const META_CREATEBRUSHINDIRECT = 0x02FC;
const META_POLYGON = 0x0324;

protected $gdiObjects = [];

public function __destruct()
{
if ($this->gd){
Expand Down Expand Up @@ -82,6 +84,8 @@ public function load(string $filename): bool
$recordEnd = false;

$dataFillColor = $dataDrawColor = null;
$nullPen = $nullBrush = false;
$dashArray = [];
$modePolyFill = 0;

while ($this->pos < $contentLen && !$recordEnd) {
Expand All @@ -91,6 +95,7 @@ public function load(string $filename): bool
list(,$recordType) = unpack('S', substr($this->content, $this->pos, 2));

Check failure on line 95 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Cannot use array destructuring on array|false.

Check failure on line 95 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Cannot use array destructuring on array|false.

Check failure on line 95 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.2)

Cannot use array destructuring on array|false.

Check failure on line 95 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.0)

Cannot use array destructuring on array|false.

Check failure on line 95 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.4)

Cannot use array destructuring on array|false.

Check failure on line 95 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.3)

Cannot use array destructuring on array|false.

Check failure on line 95 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.1)

Cannot use array destructuring on array|false.

Check failure on line 95 in src/WMF/Reader/GD.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

Cannot use array destructuring on array|false.
$this->pos += 2;

$params = [];
if ($size > 3) {
$params = substr($this->content, $this->pos, 2 * ($size - 3));
$this->pos += 2 * ($size - 3);
Expand Down Expand Up @@ -171,7 +176,6 @@ public function load(string $filename): bool
break;
case self::META_SETWINDOWEXT:
// Do not allow window extent to be changed after drawing has begun
var_dump('META_SETWINDOWEXT');
if (!$this->windowWidth) {
$windowExtent = array_reverse(unpack('s2', $params));
$this->windowWidth = (int) $windowExtent[0];
Expand Down Expand Up @@ -219,7 +223,11 @@ public function load(string $filename): bool
$op = 'n';
} else {
// Fill
\imagefilledpolygon($this->gd, $points, $dataFillColor);
if (\PHP_VERSION_ID < 80000) {
imagefilledpolygon($this->gd, $points, $numpoints, $dataFillColor);
} else {
imagefilledpolygon($this->gd, $points, $dataFillColor);
}
}
} else {
if ($nullBrush) {
Expand All @@ -228,7 +236,11 @@ public function load(string $filename): bool
} else {
// Stroke and Fill
\imagepolygon($this->gd, $points, $numpoints, $dataDrawColor);
\imagefilledpolygon($this->gd, $points, $dataFillColor);
if (\PHP_VERSION_ID < 80000) {
imagefilledpolygon($this->gd, $points, $numpoints, $dataFillColor);
} else {
imagefilledpolygon($this->gd, $points, $dataFillColor);
}
}
}
if ($modePolyFill == 1 && (($nullPen && !$nullBrush) || (!$nullPen && $nullBrush))) {
Expand Down Expand Up @@ -284,8 +296,8 @@ public function getResource()

$this->gd = imagescale(
$this->gd,
ceil(($this->windowWidth/$this->unitPerInch) * $inchToPoint),
ceil(($this->windowHeight/$this->unitPerInch) * $inchToPoint)
(int) ceil(($this->windowWidth/$this->unitPerInch) * $inchToPoint),
(int) ceil(($this->windowHeight/$this->unitPerInch) * $inchToPoint)
);
imagesavealpha($this->gd, true);
return $this->gd;
Expand Down
1 change: 1 addition & 0 deletions src/WMF/Reader/Imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpOffice\WMF\Reader;

use Imagick as ImagickBase;
use PhpOffice\WMF\Exception\WMFException;

class Imagick implements ReaderInterface
{
Expand Down
8 changes: 5 additions & 3 deletions src/WMF/Reader/Magic.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ class Magic implements ReaderInterface

public function __construct()
{
$reader = null;
if (extension_loaded('imagick') && in_array('WMF', ImagickBase::queryformats())) {
$this->reader = new ImagickReader();
$reader = new ImagickReader();
}
if (!$this->reader && extension_loaded('gd')) {
$this->reader = new GD();
if (!$reader && extension_loaded('gd')) {
$reader = new GD();
}
$this->reader = $reader;
}

public function load(string $filename): bool
Expand Down
3 changes: 3 additions & 0 deletions tests/WMF/Reader/AbstractTestReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function assertImageCompare(string $expectedFile, string $outputFile, flo
$this->assertLessThanOrEqual($threshold, $result[1]);
}

/**
* @return array<array<string>>
*/
public static function dataProviderFiles(): array
{
return [
Expand Down
11 changes: 6 additions & 5 deletions tests/WMF/Reader/GDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use GdImage;
use PhpOffice\WMF\Reader\GD;

class GDTest extends AbstractTestReader
class GDTest extends AbstractTestReader
{
/**
* @dataProvider dataProviderFiles
Expand All @@ -27,13 +27,14 @@ public function testGetResource(string $file): void
$reader->load($this->getResourceDir() . $file);
$this->assertInstanceOf(GdImage::class, $reader->getResource());
}

/**
* @dataProvider dataProviderFiles
*/
public function testOutput(string $file): void
{
$outputFile = $this->getResourceDir() . 'output_'.pathinfo($file, PATHINFO_FILENAME).'.png';
$similarFile = $this->getResourceDir() . pathinfo($file, PATHINFO_FILENAME).'.png';
$outputFile = $this->getResourceDir() . 'output_' . pathinfo($file, PATHINFO_FILENAME) . '.png';
$similarFile = $this->getResourceDir() . pathinfo($file, PATHINFO_FILENAME) . '.png';

$reader = new GD();
$reader->load($this->getResourceDir() . $file);
Expand All @@ -50,6 +51,6 @@ public function testOutput(string $file): void
public function testIsWMF(string $file): void
{
$reader = new GD();
$this->assertTrue($reader->isWMF($this->getResourceDir() .$file));
$this->assertTrue($reader->isWMF($this->getResourceDir() . $file));
}
}
}
Loading

0 comments on commit 087d3e8

Please sign in to comment.