diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000000000000000000000000000000000000..7cedbff512dd96541e36ef86ddcc84b9e310f53d --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,75 @@ +# Contributing to `sebastian/lines-of-code` + +## Welcome! + +We look forward to your contributions! Here are some examples how you can contribute: + +* [Report a bug](https://github.com/sebastianbergmann/lines-of-code/issues/new) +* [Send a pull request to fix a bug](https://github.com/sebastianbergmann/lines-of-code/pulls) + +Please do not send pull requests that expand the scope of this project (see below). + + +## Any contributions you make will be under the BSD-3-Clause License + +When you submit code changes, your submissions are understood to be under the same [BSD-3-Clause License](https://github.com/sebastianbergmann/lines-of-code/blob/main/LICENSE) that covers the project. By contributing to this project, you agree that your contributions will be licensed under its BSD-3-Clause License. + + +## Write bug reports with detail, background, and sample code + +[This is an example](https://github.com/sebastianbergmann/phpunit/issues/4376) of a bug report I wrote, and I think it's not too bad. + +In your bug report, please provide the following: + +* A quick summary and/or background +* Steps to reproduce + * Be specific! + * Give sample code if you can. +* What you expected would happen +* What actually happens +* Notes (possibly including why you think this might be happening, or stuff you tried that didn't work) + +Please post code and output as text ([using proper markup](https://guides.github.com/features/mastering-markdown/)). Do not post screenshots of code or output. + + +## Workflow for Pull Requests + +1. Fork the repository. +2. Create your branch from the oldest branch that is affected by the bug you plan to fix. +3. Implement your change and add tests for it. +4. Ensure the test suite passes. +5. Ensure the code complies with our coding guidelines (see below). +6. Send that pull request! + +Please make sure you have [set up your username and email address](https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup) for use with Git. Strings such as `silly nick name <root@localhost>` look really stupid in the commit history of a project. + +We encourage you to [sign your Git commits with your GPG key](https://docs.github.com/en/github/authenticating-to-github/signing-commits). + + +## Development + +This project uses [PHPUnit](https://phpunit.de/) for testing: + +```shell +./vendor/bin/phpunit +``` + +This project uses [PHPStan](https://phpstan.org/) for static analysis: + +```shell +./tools/phpstan +``` + +This project uses [PHP-CS-Fixer](https://cs.symfony.com/) to enforce coding guidelines: + +```shell +./tools/php-cs-fixer fix +``` + +The commands shown above require an autoloader script at `vendor/autoload.php`. This can be generated like so: + +```shell +./tools/composer dump-autoload +``` + +Please understand that we will not accept a pull request when its changes violate this project's coding guidelines or break the test suite. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1efe0b0edcbadf9e897f2da6e83a565ddf7ce330..356e2290f1a0b001f5d9788b54aace2ae1bdd1d8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,7 +7,7 @@ on: name: "CI" env: - COMPOSER_ROOT_VERSION: "3.0-dev" + COMPOSER_ROOT_VERSION: "4.0.x-dev" permissions: contents: read @@ -61,9 +61,9 @@ jobs: fail-fast: false matrix: php-version: - - "8.2" - "8.3" - "8.4" + - "8.5" steps: - name: "Checkout" @@ -73,15 +73,21 @@ jobs: uses: "shivammathur/setup-php@v2" with: php-version: "${{ matrix.php-version }}" - coverage: "pcov" + coverage: "xdebug" - name: "Install dependencies with Composer" run: "./tools/composer update --no-ansi --no-interaction --no-progress" - name: "Run tests with PHPUnit" - run: "vendor/bin/phpunit --coverage-clover=coverage.xml" + run: "vendor/bin/phpunit --log-junit junit.xml --coverage-clover=coverage.xml" - - name: "Send code coverage report to Codecov.io" - env: - CODECOV_TOKEN: "${{ secrets.CODECOV_TOKEN }}" - run: "bash <(curl -s https://codecov.io/bash) || true" + - name: Upload test results to Codecov.io + if: ${{ !cancelled() }} + uses: codecov/test-results-action@v1 + with: + token: ${{ secrets.CODECOV_TOKEN }} + + - name: Upload code coverage data to Codecov.io + uses: codecov/codecov-action@v4 + with: + token: ${{ secrets.CODECOV_TOKEN }} diff --git a/.phive/phars.xml b/.phive/phars.xml index 91ac82b19d700618690f300fafc4a0e38140d0f2..5c83e40d15ae1fd16636c419b90fb93d8472061d 100644 --- a/.phive/phars.xml +++ b/.phive/phars.xml @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> <phive xmlns="https://phar.io/phive"> - <phar name="php-cs-fixer" version="^3.59" installed="3.59.3" location="./tools/php-cs-fixer" copy="true"/> - <phar name="infection" version="^0.29" installed="0.29.6" location="./tools/infection" copy="true"/> - <phar name="composer" version="^2.7" installed="2.7.7" location="./tools/composer" copy="true"/> - <phar name="phpstan" version="^1.11" installed="1.11.5" location="./tools/phpstan" copy="true"/> + <phar name="php-cs-fixer" version="^3.64" installed="3.68.0" location="./tools/php-cs-fixer" copy="true"/> + <phar name="infection" version="^0.29" installed="0.29.10" location="./tools/infection" copy="true"/> + <phar name="composer" version="^2.8" installed="2.8.4" location="./tools/composer" copy="true"/> + <phar name="phpstan" version="^2.0" installed="2.1.1" location="./tools/phpstan" copy="true"/> </phive> diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 5690319737a1255cce0d3a4c2a1842ede75b34e1..3d66eed6f58d40ac4ffebb6ca0a99ef23c1fd70e 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -100,7 +100,7 @@ $config->setFinder($finder) 'explicit_string_variable' => true, 'fopen_flag_order' => true, 'full_opening_tag' => true, - 'fully_qualified_strict_types' => true, + 'fully_qualified_strict_types' => ['import_symbols' => true], 'function_declaration' => true, 'function_to_constant' => true, 'get_class_to_class_keyword' => true, @@ -136,7 +136,7 @@ $config->setFinder($finder) 'modernize_types_casting' => true, 'multiline_comment_opening_closing' => true, 'multiline_whitespace_before_semicolons' => true, - 'native_constant_invocation' => false, + 'native_constant_invocation' => true, 'native_function_casing' => false, 'native_function_invocation' => [ 'include' => [ @@ -159,7 +159,23 @@ $config->setFinder($finder) 'no_empty_comment' => true, 'no_empty_phpdoc' => true, 'no_empty_statement' => true, - 'no_extra_blank_lines' => true, + 'no_extra_blank_lines' => [ + 'tokens' => [ + 'attribute', + 'break', + 'case', + 'continue', + 'curly_brace_block', + 'default', + 'extra', + 'parenthesis_brace_block', + 'return', + 'square_brace_block', + 'switch', + 'throw', + 'use', + ], + ], 'no_homoglyph_names' => true, 'no_leading_import_slash' => true, 'no_leading_namespace_whitespace' => true, @@ -198,6 +214,7 @@ $config->setFinder($finder) 'no_whitespace_in_blank_line' => true, 'non_printable_character' => true, 'normalize_index_brace' => true, + 'nullable_type_declaration_for_default_null_value' => true, 'object_operator_without_whitespace' => true, 'octal_notation' => true, 'operator_linebreak' => [ @@ -341,6 +358,8 @@ $config->setFinder($finder) 'whitespace_after_comma_in_array' => true, ]); -$config->setCacheFile(__DIR__ . '/.php-cs-fixer.cache/' . sha1(@trim((string) @shell_exec('git rev-parse --abbrev-ref HEAD')))); +$config->setCacheFile(__DIR__ . '/.php-cs-fixer.cache/' . json_decode((string) @file_get_contents('composer.json'), true)["extra"]["branch-alias"]["dev-main"] ?? 'unknown'); + +$config->setParallelConfig(\PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect()); return $config; diff --git a/ChangeLog.md b/ChangeLog.md index d4e0905b0463c99d5daa63a906bdb750142d6e68..4e724be6f86975406177b358407b6d6cc16c5018 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -2,6 +2,12 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles. +## [4.0.0] - 2025-02-07 + +### Removed + +* This component is no longer supported on PHP 8.2 + ## [3.0.1] - 2024-07-03 ### Changed @@ -59,6 +65,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt * Initial release +[4.0.0]: https://github.com/sebastianbergmann/lines-of-code/compare/3.0...4.0.0 [3.0.1]: https://github.com/sebastianbergmann/lines-of-code/compare/3.0.0...3.0.1 [3.0.0]: https://github.com/sebastianbergmann/lines-of-code/compare/2.0...3.0.0 [2.0.2]: https://github.com/sebastianbergmann/lines-of-code/compare/2.0.1...2.0.2 diff --git a/LICENSE b/LICENSE index edaedf61938c1808f162dc7951866db4b7a79e3d..0d534da34a86a5784ce65a7639d489a0bd530aab 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ BSD 3-Clause License -Copyright (c) 2020-2024, Sebastian Bergmann +Copyright (c) 2020-2025, Sebastian Bergmann All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/README.md b/README.md index d98eab43fce28cde55f4164515a8ae9a64ec32bb..765a0ebb24e753f0a24a1c904418941d8dd393a4 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[](https://packagist.org/packages/sebastian/lines-of-code) +[](https://packagist.org/packages/sebastian/lines-of-code) [](https://github.com/sebastianbergmann/lines-of-code/actions) [](https://codecov.io/gh/sebastianbergmann/lines-of-code) diff --git a/composer.json b/composer.json index 46f1243a3de5832359449ccf182f04af0b93f791..7c428050f6c62efacff1bae95cb66070184b5994 100644 --- a/composer.json +++ b/composer.json @@ -17,15 +17,15 @@ }, "prefer-stable": true, "require": { - "php": ">=8.2", + "php": ">=8.3", "nikic/php-parser": "^5.0" }, "require-dev": { - "phpunit/phpunit": "^11.0" + "phpunit/phpunit": "^12.0" }, "config": { "platform": { - "php": "8.2" + "php": "8.3" }, "optimize-autoloader": true, "sort-packages": true @@ -37,7 +37,7 @@ }, "extra": { "branch-alias": { - "dev-main": "3.0-dev" + "dev-main": "4.0-dev" } } } diff --git a/phpstan.neon b/phpstan.neon index ab7e25ec2893d1dc4d27de5aafe0724013bb3057..062a16174bfd253a9e5351340571fad7407cc230 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,16 +1,6 @@ parameters: - level: 9 + level: 10 paths: - src - tests/unit - tests/integration - ignoreErrors: - - - message: "#^Return type \\(void\\) of method SebastianBergmann\\\\LinesOfCode\\\\LineCountingVisitor\\:\\:enterNode\\(\\) should be compatible with return type \\(array\\<PhpParser\\\\Node\\>\\|int\\|PhpParser\\\\Node\\|null\\) of method PhpParser\\\\NodeVisitor\\:\\:enterNode\\(\\)$#" - count: 1 - path: src/LineCountingVisitor.php - - - - message: "#^Return type \\(void\\) of method SebastianBergmann\\\\LinesOfCode\\\\LineCountingVisitor\\:\\:enterNode\\(\\) should be compatible with return type \\(array\\<PhpParser\\\\Node\\>\\|int\\|PhpParser\\\\Node\\|null\\) of method PhpParser\\\\NodeVisitorAbstract\\:\\:enterNode\\(\\)$#" - count: 1 - path: src/LineCountingVisitor.php diff --git a/phpunit.xml b/phpunit.xml index b6d89b8859e41907714bc7bafa066dee92644ecc..da8ef745319bca9e90439e0f6616598d635d169a 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,12 +1,13 @@ <?xml version="1.0" encoding="UTF-8"?> <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" - bootstrap="vendor/autoload.php" cacheDirectory=".phpunit.cache" executionOrder="depends,defects" requireCoverageMetadata="true" beStrictAboutCoverageMetadata="true" beStrictAboutOutputDuringTests="true" + displayDetailsOnPhpunitDeprecations="true" + failOnPhpunitDeprecation="true" failOnRisky="true" failOnWarning="true" colors="true"> diff --git a/src/Counter.php b/src/Counter.php index 2baed4ba374865676b9d38761c57a377d7473fc3..ed2d3ab0e4083c149a97401952c1255f8afeefa3 100644 --- a/src/Counter.php +++ b/src/Counter.php @@ -48,7 +48,6 @@ final class Counter assert($nodes !== null); return $this->countInAbstractSyntaxTree($linesOfCode, $nodes); - // @codeCoverageIgnoreStart } catch (Error $error) { throw new RuntimeException( diff --git a/src/Exception/NegativeValueException.php b/src/Exception/NegativeValueException.php deleted file mode 100644 index 40d27e1f0f05ef4221fcc9c84e76689eb717c249..0000000000000000000000000000000000000000 --- a/src/Exception/NegativeValueException.php +++ /dev/null @@ -1,16 +0,0 @@ -<?php declare(strict_types=1); -/* - * This file is part of sebastian/lines-of-code. - * - * (c) Sebastian Bergmann <sebastian@phpunit.de> - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace SebastianBergmann\LinesOfCode; - -use InvalidArgumentException; - -final class NegativeValueException extends InvalidArgumentException implements Exception -{ -} diff --git a/src/LineCountingVisitor.php b/src/LineCountingVisitor.php index fd36675cca8d9bed9db8e520bbf8a9a773e76682..a461a98a99928054f5095633469445063be07fdf 100644 --- a/src/LineCountingVisitor.php +++ b/src/LineCountingVisitor.php @@ -43,15 +43,17 @@ final class LineCountingVisitor extends NodeVisitorAbstract $this->linesOfCode = $linesOfCode; } - public function enterNode(Node $node): void + public function enterNode(Node $node): null { $this->comments = array_merge($this->comments, $node->getComments()); if (!$node instanceof Expr) { - return; + return null; } $this->linesWithStatements[] = $node->getStartLine(); + + return null; } public function result(): LinesOfCode diff --git a/src/LinesOfCode.php b/src/LinesOfCode.php index 890d9611f0b7ae41cf10cc8377fe10cdd25b39c4..2aa5b0e64618044d0b36d7b9a8ca750e95004074 100644 --- a/src/LinesOfCode.php +++ b/src/LinesOfCode.php @@ -41,30 +41,9 @@ final readonly class LinesOfCode * @param non-negative-int $logicalLinesOfCode * * @throws IllogicalValuesException - * @throws NegativeValueException */ public function __construct(int $linesOfCode, int $commentLinesOfCode, int $nonCommentLinesOfCode, int $logicalLinesOfCode) { - /** @phpstan-ignore smaller.alwaysFalse */ - if ($linesOfCode < 0) { - throw new NegativeValueException('$linesOfCode must not be negative'); - } - - /** @phpstan-ignore smaller.alwaysFalse */ - if ($commentLinesOfCode < 0) { - throw new NegativeValueException('$commentLinesOfCode must not be negative'); - } - - /** @phpstan-ignore smaller.alwaysFalse */ - if ($nonCommentLinesOfCode < 0) { - throw new NegativeValueException('$nonCommentLinesOfCode must not be negative'); - } - - /** @phpstan-ignore smaller.alwaysFalse */ - if ($logicalLinesOfCode < 0) { - throw new NegativeValueException('$logicalLinesOfCode must not be negative'); - } - if ($linesOfCode - $commentLinesOfCode !== $nonCommentLinesOfCode) { throw new IllogicalValuesException('$linesOfCode !== $commentLinesOfCode + $nonCommentLinesOfCode'); } diff --git a/tests/unit/LinesOfCodeTest.php b/tests/unit/LinesOfCodeTest.php index a5a6c61a0836f42ca14c60ac656698fb6c9606d6..152a9f08c5eec2725aa4268266deb070bbd9fc5d 100644 --- a/tests/unit/LinesOfCodeTest.php +++ b/tests/unit/LinesOfCodeTest.php @@ -42,42 +42,6 @@ final class LinesOfCodeTest extends TestCase $this->assertSame(0, $this->linesOfCode()->logicalLinesOfCode()); } - public function testLinesOfCodeCannotBeNegative(): void - { - $this->expectException(NegativeValueException::class); - $this->expectExceptionMessage('$linesOfCode must not be negative'); - - /** @phpstan-ignore argument.type */ - new LinesOfCode(-1, 0, 0, 0); - } - - public function testCommentLinesOfCodeCannotBeNegative(): void - { - $this->expectException(NegativeValueException::class); - $this->expectExceptionMessage('$commentLinesOfCode must not be negative'); - - /** @phpstan-ignore argument.type */ - new LinesOfCode(0, -1, 0, 0); - } - - public function testNonCommentLinesOfCodeCannotBeNegative(): void - { - $this->expectException(NegativeValueException::class); - $this->expectExceptionMessage('$nonCommentLinesOfCode must not be negative'); - - /** @phpstan-ignore argument.type */ - new LinesOfCode(0, 0, -1, 0); - } - - public function testLogicalLinesOfCodeCannotBeNegative(): void - { - $this->expectException(NegativeValueException::class); - $this->expectExceptionMessage('$logicalLinesOfCode must not be negative'); - - /** @phpstan-ignore argument.type */ - new LinesOfCode(0, 0, 0, -1); - } - #[TestDox('Lines of Code = Comment Lines of Code + Non-Comment Lines of Code')] public function testNumbersHaveToMakeSense(): void {