From 3c2646a3bbce21e23cbbae26592ceffb17f94ead Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Fri, 31 May 2024 19:27:00 +0100 Subject: [PATCH 1/2] Improved code style and added stricter type hints This commit adds strict type declarations to all files and modified exceptions thrown in test classes to use `Throwable`. It also updates the dependency list and configurations for coding style checks. Furthermore, it added new scripts and plugins to facilitate and ensure code quality. --- composer.json | 12 ++- composer.lock | 192 +++++++++++++++++++++++++++++++++++- phpcs.xml | 93 +++++++++++++++++ src/BadgeComposer.php | 34 ++++--- tests/BadgeComposerTest.php | 17 ++-- 5 files changed, 323 insertions(+), 25 deletions(-) create mode 100644 phpcs.xml diff --git a/composer.json b/composer.json index 76868f8..5c777e6 100644 --- a/composer.json +++ b/composer.json @@ -25,11 +25,17 @@ ], "require-dev": { "squizlabs/php_codesniffer": "^3.10", - "phpunit/phpunit": "^11.1" + "phpunit/phpunit": "^11.1", + "slevomat/coding-standard": "^8.15" }, "scripts": { - "lint": "phpcs --ignore=/vendor/* --standard=PSR12 .", - "lint:fix": "phpcbf --ignore=/vendor/* --standard=PSR12 .", + "lint": "phpcs", + "lint:fix": "phpcbf", "test": "phpunit --testdox --coverage-clover coverage/clover.xml --coverage-html coverage --coverage-filter src/" + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/composer.lock b/composer.lock index ae567d9..b1306d5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,9 +4,87 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ad2bdffdb02f50623a18f5e7585870e5", + "content-hash": "8209864891f223ee4ec954d4b0179154", "packages": [], "packages-dev": [ + { + "name": "dealerdirect/phpcodesniffer-composer-installer", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/PHPCSStandards/composer-installer.git", + "reference": "4be43904336affa5c2f70744a348312336afd0da" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCSStandards/composer-installer/zipball/4be43904336affa5c2f70744a348312336afd0da", + "reference": "4be43904336affa5c2f70744a348312336afd0da", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^1.0 || ^2.0", + "php": ">=5.4", + "squizlabs/php_codesniffer": "^2.0 || ^3.1.0 || ^4.0" + }, + "require-dev": { + "composer/composer": "*", + "ext-json": "*", + "ext-zip": "*", + "php-parallel-lint/php-parallel-lint": "^1.3.1", + "phpcompatibility/php-compatibility": "^9.0", + "yoast/phpunit-polyfills": "^1.0" + }, + "type": "composer-plugin", + "extra": { + "class": "PHPCSStandards\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin" + }, + "autoload": { + "psr-4": { + "PHPCSStandards\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Franck Nijhof", + "email": "franck.nijhof@dealerdirect.com", + "homepage": "http://www.frenck.nl", + "role": "Developer / IT Manager" + }, + { + "name": "Contributors", + "homepage": "https://github.com/PHPCSStandards/composer-installer/graphs/contributors" + } + ], + "description": "PHP_CodeSniffer Standards Composer Installer Plugin", + "homepage": "http://www.dealerdirect.com", + "keywords": [ + "PHPCodeSniffer", + "PHP_CodeSniffer", + "code quality", + "codesniffer", + "composer", + "installer", + "phpcbf", + "phpcs", + "plugin", + "qa", + "quality", + "standard", + "standards", + "style guide", + "stylecheck", + "tests" + ], + "support": { + "issues": "https://github.com/PHPCSStandards/composer-installer/issues", + "source": "https://github.com/PHPCSStandards/composer-installer" + }, + "time": "2023-01-05T11:28:13+00:00" + }, { "name": "myclabs/deep-copy", "version": "1.11.1", @@ -242,6 +320,53 @@ }, "time": "2022-02-21T01:04:05+00:00" }, + { + "name": "phpstan/phpdoc-parser", + "version": "1.29.1", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpdoc-parser.git", + "reference": "fcaefacf2d5c417e928405b71b400d4ce10daaf4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/fcaefacf2d5c417e928405b71b400d4ce10daaf4", + "reference": "fcaefacf2d5c417e928405b71b400d4ce10daaf4", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0" + }, + "require-dev": { + "doctrine/annotations": "^2.0", + "nikic/php-parser": "^4.15", + "php-parallel-lint/php-parallel-lint": "^1.2", + "phpstan/extension-installer": "^1.0", + "phpstan/phpstan": "^1.5", + "phpstan/phpstan-phpunit": "^1.1", + "phpstan/phpstan-strict-rules": "^1.0", + "phpunit/phpunit": "^9.5", + "symfony/process": "^5.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "PHPStan\\PhpDocParser\\": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPDoc parser with support for nullable, intersection and generic types", + "support": { + "issues": "https://github.com/phpstan/phpdoc-parser/issues", + "source": "https://github.com/phpstan/phpdoc-parser/tree/1.29.1" + }, + "time": "2024-05-31T08:52:43+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "11.0.3", @@ -1588,6 +1713,71 @@ ], "time": "2024-02-02T06:10:47+00:00" }, + { + "name": "slevomat/coding-standard", + "version": "8.15.0", + "source": { + "type": "git", + "url": "https://github.com/slevomat/coding-standard.git", + "reference": "7d1d957421618a3803b593ec31ace470177d7817" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/slevomat/coding-standard/zipball/7d1d957421618a3803b593ec31ace470177d7817", + "reference": "7d1d957421618a3803b593ec31ace470177d7817", + "shasum": "" + }, + "require": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.6.2 || ^0.7 || ^1.0", + "php": "^7.2 || ^8.0", + "phpstan/phpdoc-parser": "^1.23.1", + "squizlabs/php_codesniffer": "^3.9.0" + }, + "require-dev": { + "phing/phing": "2.17.4", + "php-parallel-lint/php-parallel-lint": "1.3.2", + "phpstan/phpstan": "1.10.60", + "phpstan/phpstan-deprecation-rules": "1.1.4", + "phpstan/phpstan-phpunit": "1.3.16", + "phpstan/phpstan-strict-rules": "1.5.2", + "phpunit/phpunit": "8.5.21|9.6.8|10.5.11" + }, + "type": "phpcodesniffer-standard", + "extra": { + "branch-alias": { + "dev-master": "8.x-dev" + } + }, + "autoload": { + "psr-4": { + "SlevomatCodingStandard\\": "SlevomatCodingStandard/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Slevomat Coding Standard for PHP_CodeSniffer complements Consistence Coding Standard by providing sniffs with additional checks.", + "keywords": [ + "dev", + "phpcs" + ], + "support": { + "issues": "https://github.com/slevomat/coding-standard/issues", + "source": "https://github.com/slevomat/coding-standard/tree/8.15.0" + }, + "funding": [ + { + "url": "https://github.com/kukulich", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/slevomat/coding-standard", + "type": "tidelift" + } + ], + "time": "2024-03-09T15:20:58+00:00" + }, { "name": "squizlabs/php_codesniffer", "version": "3.10.1", diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..1c2490c --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,93 @@ + + + + + + + + + src + tests + + + + + + + + + + + + + + + + + + 10 + + + + + + 10 + + + + + + + + 10 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + tests/*/data + \ No newline at end of file diff --git a/src/BadgeComposer.php b/src/BadgeComposer.php index 9cda00b..dfdb244 100644 --- a/src/BadgeComposer.php +++ b/src/BadgeComposer.php @@ -1,8 +1,17 @@ -xpath('//metrics'); foreach ($metrics as $metric) { $this->totalElements += (int) $metric['elements']; $this->checkedElements += (int) $metric['coveredelements']; } - $coverage = round(($this->totalElements === 0) ? 0 : ($this->checkedElements / $this->totalElements) * 100); - $this->totalCoverage += $coverage; - } catch (Exception $e) { - throw new Exception('Error processing file: ' . $inputFile); + $coverageRatio = $this->totalElements ? $this->checkedElements / $this->totalElements : 0; + $this->totalCoverage += (int) round($coverageRatio * 100); + + } catch (Throwable $e) { + throw new Exception('Error processing file: ' . $e->getMessage()); } } /** * Finalize the coverage report by generating a badge with the average coverage across all input files. * - * @return void * @throws Exception If there is an error generating the badge. */ private function finalizeCoverage(): void @@ -120,7 +126,7 @@ private function finalizeCoverage(): void $totalCoverage = $this->totalCoverage / count($this->inputFiles); // Average coverage across all files $template = file_get_contents(__DIR__ . '/../template/badge.svg'); - $template = str_replace('{{ total }}', $totalCoverage, $template); + $template = str_replace('{{ total }}', (string) $totalCoverage, $template); $template = str_replace('{{ coverage }}', $this->coverageName, $template); @@ -137,9 +143,9 @@ private function finalizeCoverage(): void $color = '#4c1'; // Bright Green } - $template = str_replace('{{ total }}', $totalCoverage, $template); $template = str_replace('{{ color }}', $color, $template); file_put_contents($this->outputFile, $template); } + } diff --git a/tests/BadgeComposerTest.php b/tests/BadgeComposerTest.php index b52f818..50bc6ab 100644 --- a/tests/BadgeComposerTest.php +++ b/tests/BadgeComposerTest.php @@ -1,11 +1,13 @@ -badgeComposer))->getMethod('validateFiles'); + $method = (new ReflectionClass($this->badgeComposer))->getMethod('validateFiles'); return $method->invoke($this->badgeComposer, $files, $output); } @@ -40,7 +42,7 @@ public function validateFiles(array $files, string $output) */ public function testErrorIsThrownWhenInputFileDoesNotExist(): void { - $this->expectException(\Exception::class); + $this->expectException(Throwable::class); $this->expectExceptionMessage('input file does not exist: file_does_not_exist.xml'); $this->validateFiles( @@ -55,7 +57,7 @@ public function testErrorIsThrownWhenInputFileDoesNotExist(): void */ public function testErrorIsThrownWhenOutputFileDoesNotExist(): void { - $this->expectException(\Exception::class); + $this->expectException(Throwable::class); $this->expectExceptionMessage('output file name is mandatory'); $this->validateFiles( @@ -69,7 +71,7 @@ public function testErrorIsThrownWhenOutputFileDoesNotExist(): void */ public function processFile(string $inputFile) { - $method = (new \ReflectionClass($this->badgeComposer))->getMethod('processFile'); + $method = (new ReflectionClass($this->badgeComposer))->getMethod('processFile'); return $method->invoke($this->badgeComposer, $inputFile); } @@ -94,4 +96,5 @@ public function testProcessMultipleCloverFilesAndCalculateTheCoverage(): void $this->assertEquals(83, $this->badgeComposer->getTotalCoverage()); } + } From c9ecc66e4864932f67c6b919e167b38741de24dc Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Fri, 31 May 2024 19:30:30 +0100 Subject: [PATCH 2/2] Rename ruleset --- phpcs.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpcs.xml b/phpcs.xml index 1c2490c..2d01eed 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,5 +1,5 @@ - +