Skip to content

Commit

Permalink
Add support for PHP8 + various cleaning (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecourtial authored Feb 10, 2021
1 parent 13ddeca commit e57146a
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 27 deletions.
24 changes: 23 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ executors:
- image: php:7.4-cli
working_directory: ~/repo

php80:
docker:
- image: php:8.0-cli
working_directory: ~/repo

## COMMANDS
commands:

Expand Down Expand Up @@ -52,7 +57,7 @@ commands:
name: composer
command: |
if [[ ! -f vendor/autoload.php ]]; then
curl https://getcomposer.org/composer-1.phar --location --silent --output /usr/bin/composer; \
curl https://getcomposer.org/composer-stable.phar --location --silent --output /usr/bin/composer; \
chmod +x /usr/bin/composer; \
composer install --no-progress --no-interaction; \
fi
Expand Down Expand Up @@ -128,6 +133,17 @@ jobs:
- restore-composer-cache
- infection

composer80:
executor: php80
steps:
- store-composer-cache

phpunit80:
executor: php80
steps:
- restore-composer-cache
- phpunit

## WORKFLOWS
workflows:
Code quality PHP 7.4:
Expand All @@ -145,3 +161,9 @@ workflows:
- infection74:
requires:
- phpunit74
Code quality PHP 8.0:
jobs:
- composer80
- phpunit80:
requires:
- composer80
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ test:
vendor/bin/phpunit --testdox

infection:
vendor/bin/infection --min-msi=89
vendor/bin/infection --min-msi=85
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://GitHub.com/ecourtial/php-bmp-parser/graphs/commit-activity)
[![Ask Me Anything !](https://img.shields.io/badge/Ask%20me-anything-1abc9c.svg)](https://GitHub.com/ecourtial/php-bmp-parser)
[![GitHub license](https://img.shields.io/github/license/ecourtial/php-bmp-parser)](https://github.com/ecourtial/php-bmp-parser/blob/master/LICENSE)
![PHP Version](https://img.shields.io/packagist/php-v/ecourtial/php-bmp-parser)

## Description

* This small library allows you to parse a standard Windows BMP file (generic information + pixel by pixel).
* No third party libraries required. It is only based on native PHP functions (with the _gd_ extension required).
* It also provides a basic feature to edit an existing BMP file.
* No third party libraries required. It is only based on native PHP functions.
* It also provides a basic feature to edit an existing BMP file (with the _gd_ extension required).

## Limitations

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
],
"require": {
"php": ">=7.4",
"ext-gd": "*"
"ext-gd": "*",
"ondram/ci-detector": "^3.5"
},
"autoload": {
"psr-4": {
Expand All @@ -30,7 +31,6 @@
"phpstan/phpstan-phpunit": "^0.12.17",
"phpstan/phpstan-strict-rules": "^0.12.9",
"infection/infection": "^0.21.0",
"wizaplace/phpcs": "^1.3",
"ondram/ci-detector": "^3.5"
"wizaplace/phpcs": "^1.3"
}
}
94 changes: 75 additions & 19 deletions src/Core/PhpCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@

namespace Ecourtial\PhpBmpParser\Core;

/**
* This class has the purpose to mock native PHP function.
* It mus be compatible with both PHP 7.4 and 8.
*/
class PhpCore
{
public function getFileSize(string $filePath): int
{
$fileSize = @\filesize($filePath);
// This code must be compatible with both PHP7.4 and 8.
try {
$msg = '';
$fileSize = @\filesize($filePath);
} catch (\Throwable $exception) {
$msg = $exception->getMessage();
$fileSize = false;
}

if (false === is_int($fileSize)) {
throw new \Exception('Impossible to assert the size of the given file!');
throw new \Exception("Impossible to assert the size of the given file! $msg");
}

return $fileSize;
Expand All @@ -20,10 +31,17 @@ public function getFileSize(string $filePath): int
/** @phpstan-ignore-next-line */
public function fopen($filePath)
{
$file = @\fopen($filePath, "rb");
// This code must be compatible with both PHP7.4 and 8.
try {
$msg = '';
$file = @\fopen($filePath, "rb");
} catch (\Throwable $exception) {
$msg = $exception->getMessage();
$file = false;
}

if (false === is_resource($file)) {
throw new \Exception('Impossible to open the given file!');
throw new \Exception("Impossible to open the given file! $msg");
}

return $file;
Expand All @@ -32,10 +50,17 @@ public function fopen($filePath)
/** @phpstan-ignore-next-line */
public function fread($resource, $fileLength): string
{
$raw = @\fread($resource, $fileLength);
// This code must be compatible with both PHP7.4 and 8.
try {
$msg = '';
$raw = @\fread($resource, $fileLength);
} catch (\Throwable $exception) {
$msg = $exception->getMessage();
$raw = false;
}

if (false === is_string($raw)) {
throw new \Exception('Impossible to read the given file!');
throw new \Exception("Impossible to read the given file! $msg");
}

return $raw;
Expand All @@ -44,10 +69,17 @@ public function fread($resource, $fileLength): string
/** @return string[] */
public function unpack(string $format, string $string): array
{
$data = @\unpack($format, $string);
// This code must be compatible with both PHP7.4 and 8.
try {
$msg = '';
$data = @\unpack($format, $string);
} catch (\Throwable $exception) {
$msg = $exception->getMessage();
$data = false;
}

if (false === is_array($data)) {
throw new \Exception('Impossible to unpack the header of the file!');
throw new \Exception("Impossible to unpack the header of the file! $msg");
}

return $data;
Expand All @@ -56,13 +88,21 @@ public function unpack(string $format, string $string): array
/** @return string[] */
public function strSplit(string $string, int $length, string $what, string $extra = ''): array
{
$data = @\str_split($string, $length);
// This code must be compatible with both PHP7.4 and 8.
try {
$msg = '';
$data = @\str_split($string, $length);
} catch (\Throwable $exception) {
$msg = $exception->getMessage();
$data = false;
}

if (false === is_array($data) || 0 === count($data)) {
$msg = "Impossible to splint the given string of type: '$what'";
$msg = $extra === '' ? $msg : $msg . ' at ' . $extra;
$error = "Impossible to splint the given string of type: '$what'";
$error = $extra === '' ? $error : $error . ' at ' . $extra;
$error .= " $msg";

throw new \Exception($msg);
throw new \Exception($error);
}

return $data;
Expand All @@ -71,10 +111,18 @@ public function strSplit(string $string, int $length, string $what, string $extr
/** @phpstan-ignore-next-line */
public function imageCreateTrueColor(int $width, int $height)
{
$imageFile = @\imagecreatetruecolor($width, $height);
// This code must be compatible with both PHP7.4 and 8.
try {
$msg = '';
$imageFile = @\imagecreatetruecolor($width, $height);
} catch (\Throwable $exception) {
$msg = $exception->getMessage();
$imageFile = false;
}

if (false === $imageFile) {
throw new \Exception("Impossible to create the image!");
/** @phpstan-ignore-next-line */
if (false === is_resource($imageFile) && false === is_object($imageFile)) {
throw new \Exception("Impossible to create the image! $msg");
}

return $imageFile;
Expand All @@ -83,14 +131,22 @@ public function imageCreateTrueColor(int $width, int $height)
/** @phpstan-ignore-next-line */
public function imageColorAllocate($image, int $r, int $g, int $b): int
{
if (false === is_resource($image)) {
throw new \Exception('Image must be a resource!');
// This code must be compatible with both PHP7.4 and 8.
if (false === is_resource($image) && false === is_object($image)) {
throw new \Exception('Image must be a resource!' . gettype($image) . ' given.');
}

$color = @\imagecolorallocate($image, $r, $g, $b);
try {
$msg = '';
/** @phpstan-ignore-next-line */
$color = @\imagecolorallocate($image, $r, $g, $b);
} catch (\Throwable $exception) {
$msg = $exception->getMessage();
$color = false;
}

if (false === $color) {
throw new \Exception("Impossible to create the color with RGB $r, $g, $b!");
throw new \Exception("Impossible to create the color with RGB $r, $g, $b! $msg");
}

return $color;
Expand Down
6 changes: 5 additions & 1 deletion tests/Core/PhpCoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ public function testStrSplitWithFailure(): void

public function testImageCreateTrueColorWithSuccess(): void
{
static::assertTrue(is_resource(static::$core->imageCreateTrueColor(10, 10)));
if (PHP_VERSION <= 7.4) {
static::assertTrue(is_resource(static::$core->imageCreateTrueColor(10, 10)));
} else {
static::assertTrue(is_object(static::$core->imageCreateTrueColor(10, 10)));
}
}

public function testImageCreateTrueColorWithFailure(): void
Expand Down

0 comments on commit e57146a

Please sign in to comment.