pax_global_header 0000666 0000000 0000000 00000000064 13764430207 0014520 g ustar 00root root 0000000 0000000 52 comment=d520b6988eb3bbe6ca572af920a4adbcf2de8a20
phpunit-lines-of-code-1.0.3/ 0000775 0000000 0000000 00000000000 13764430207 0015632 5 ustar 00root root 0000000 0000000 phpunit-lines-of-code-1.0.3/.github/ 0000775 0000000 0000000 00000000000 13764430207 0017172 5 ustar 00root root 0000000 0000000 phpunit-lines-of-code-1.0.3/.github/FUNDING.yml 0000664 0000000 0000000 00000000032 13764430207 0021002 0 ustar 00root root 0000000 0000000 github: sebastianbergmann
phpunit-lines-of-code-1.0.3/.github/workflows/ 0000775 0000000 0000000 00000000000 13764430207 0021227 5 ustar 00root root 0000000 0000000 phpunit-lines-of-code-1.0.3/.github/workflows/ci.yml 0000664 0000000 0000000 00000005051 13764430207 0022346 0 ustar 00root root 0000000 0000000 # https://help.github.com/en/categories/automating-your-workflow-with-github-actions
on:
- "pull_request"
- "push"
name: "CI"
jobs:
coding-guidelines:
name: "Coding Guidelines"
runs-on: "ubuntu-latest"
steps:
- name: "Checkout"
uses: "actions/checkout@v2"
- name: "Run friendsofphp/php-cs-fixer"
run: "php7.4 ./tools/php-cs-fixer fix --diff-format=udiff --dry-run --show-progress=dots --using-cache=no --verbose"
type-checker:
name: "Type Checker"
runs-on: "ubuntu-latest"
steps:
- name: "Checkout"
uses: "actions/checkout@v2"
- name: "Update dependencies with composer"
run: "php7.4 ./tools/composer update --no-ansi --no-interaction --no-progress"
- name: "Run vimeo/psalm"
run: "php7.4 ./tools/psalm --config=.psalm/config.xml --no-progress --shepherd --show-info=false --stats"
backward-compatibility:
name: Backward Compatibility
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Fetch tags
run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- name: Install PHP with extensions
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
coverage: none
extensions: intl
tests:
name: "Tests"
runs-on: "ubuntu-latest"
strategy:
fail-fast: false
matrix:
php-version:
- "7.3"
- "7.4"
- "8.0"
- "8.1"
steps:
- name: "Checkout"
uses: "actions/checkout@v2"
- name: "Install PHP with extensions"
uses: "shivammathur/setup-php@v2"
with:
php-version: "${{ matrix.php-version }}"
coverage: "pcov"
- name: "Cache dependencies installed with composer"
uses: "actions/cache@v1"
with:
path: "~/.composer/cache"
key: "php${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('**/composer.json') }}"
restore-keys: "php${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"
- name: "Install dependencies with composer"
run: "./tools/composer update --no-ansi --no-interaction --no-progress"
- name: "Run tests with phpunit/phpunit"
run: "vendor/bin/phpunit --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"
phpunit-lines-of-code-1.0.3/.gitignore 0000664 0000000 0000000 00000000115 13764430207 0017617 0 ustar 00root root 0000000 0000000 /.idea
/.php_cs
/.php_cs.cache
/.phpunit.result.cache
/composer.lock
/vendor
phpunit-lines-of-code-1.0.3/.phive/ 0000775 0000000 0000000 00000000000 13764430207 0017023 5 ustar 00root root 0000000 0000000 phpunit-lines-of-code-1.0.3/.phive/phars.xml 0000664 0000000 0000000 00000001201 13764430207 0020654 0 ustar 00root root 0000000 0000000
phpunit-lines-of-code-1.0.3/.php_cs.dist 0000664 0000000 0000000 00000020706 13764430207 0020056 0 ustar 00root root 0000000 0000000
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF;
$finder = PhpCsFixer\Finder::create()
->files()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests/integration')
->in(__DIR__ . '/tests/unit');
return PhpCsFixer\Config::create()
->setFinder($finder)
->setRiskyAllowed(true)
->setRules([
'align_multiline_comment' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => [
'operators' => [
'=' => 'align_single_space_minimal',
'=>' => 'align_single_space_minimal',
],
],
'blank_line_after_namespace' => true,
'blank_line_before_statement' => [
'statements' => [
'break',
'continue',
'declare',
'default',
'die',
'do',
'exit',
'for',
'foreach',
'goto',
'if',
'include',
'include_once',
'require',
'require_once',
'return',
'switch',
'throw',
'try',
'while',
'yield',
],
],
'braces' => true,
'cast_spaces' => true,
'class_attributes_separation' => ['elements' => ['const', 'method', 'property']],
'combine_consecutive_issets' => true,
'combine_consecutive_unsets' => true,
'compact_nullable_typehint' => true,
'concat_space' => ['spacing' => 'one'],
'constant_case' => true,
'declare_equal_normalize' => ['space' => 'none'],
'declare_strict_types' => true,
'dir_constant' => true,
'elseif' => true,
'encoding' => true,
'explicit_indirect_variable' => true,
'explicit_string_variable' => true,
'full_opening_tag' => true,
'fully_qualified_strict_types' => true,
'function_declaration' => true,
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
'import_functions' => true,
],
'header_comment' => ['header' => $header, 'separate' => 'none'],
'heredoc_to_nowdoc' => true,
'increment_style' => [
'style' => PhpCsFixer\Fixer\Operator\IncrementStyleFixer::STYLE_POST,
],
'indentation_type' => true,
'is_null' => true,
'line_ending' => true,
'list_syntax' => ['syntax' => 'short'],
'logical_operators' => true,
'lowercase_keywords' => true,
'lowercase_static_reference' => true,
'magic_constant_casing' => true,
'magic_method_casing' => true,
'method_argument_space' => ['ensure_fully_multiline' => true],
'modernize_types_casting' => true,
'multiline_comment_opening_closing' => true,
'multiline_whitespace_before_semicolons' => true,
'native_constant_invocation' => false,
'native_function_casing' => false,
'native_function_invocation' => false,
'native_function_type_declaration_casing' => true,
'new_with_braces' => false,
'no_alias_functions' => true,
'no_alternative_syntax' => true,
'no_blank_lines_after_class_opening' => true,
'no_blank_lines_after_phpdoc' => true,
'no_blank_lines_before_namespace' => true,
'no_closing_tag' => true,
'no_empty_comment' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_blank_lines' => true,
'no_homoglyph_names' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_mixed_echo_print' => ['use' => 'print'],
'no_multiline_whitespace_around_double_arrow' => true,
'no_null_property_initialization' => true,
'no_php4_constructor' => true,
'no_short_bool_cast' => true,
'no_short_echo_tag' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_spaces_after_function_name' => true,
'no_spaces_around_offset' => true,
'no_spaces_inside_parenthesis' => true,
'no_superfluous_elseif' => true,
'no_superfluous_phpdoc_tags' => [
'allow_mixed' => true,
],
'no_trailing_comma_in_list_call' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_trailing_whitespace' => true,
'no_trailing_whitespace_in_comment' => true,
'no_unneeded_control_parentheses' => true,
'no_unneeded_curly_braces' => true,
'no_unneeded_final_method' => true,
'no_unreachable_default_argument_value' => true,
'no_unset_on_property' => true,
'no_unused_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'no_whitespace_before_comma_in_array' => true,
'no_whitespace_in_blank_line' => true,
'non_printable_character' => true,
'normalize_index_brace' => true,
'object_operator_without_whitespace' => true,
'ordered_class_elements' => [
'order' => [
'use_trait',
'constant_public',
'constant_protected',
'constant_private',
'property_public_static',
'property_protected_static',
'property_private_static',
'property_public',
'property_protected',
'property_private',
'method_public_static',
'construct',
'destruct',
'magic',
'phpunit',
'method_public',
'method_protected',
'method_private',
'method_protected_static',
'method_private_static',
],
],
'ordered_imports' => [
'imports_order' => [
PhpCsFixer\Fixer\Import\OrderedImportsFixer::IMPORT_TYPE_CONST,
PhpCsFixer\Fixer\Import\OrderedImportsFixer::IMPORT_TYPE_FUNCTION,
PhpCsFixer\Fixer\Import\OrderedImportsFixer::IMPORT_TYPE_CLASS,
]
],
'ordered_interfaces' => [
'direction' => 'ascend',
'order' => 'alpha',
],
'phpdoc_add_missing_param_annotation' => false,
'phpdoc_align' => true,
'phpdoc_annotation_without_dot' => true,
'phpdoc_indent' => true,
'phpdoc_no_access' => true,
'phpdoc_no_empty_return' => true,
'phpdoc_no_package' => true,
'phpdoc_order' => true,
'phpdoc_return_self_reference' => true,
'phpdoc_scalar' => true,
'phpdoc_separation' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_summary' => true,
'phpdoc_to_comment' => true,
'phpdoc_trim' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'phpdoc_types' => ['groups' => ['simple', 'meta']],
'phpdoc_types_order' => true,
'phpdoc_var_without_name' => true,
'pow_to_exponentiation' => true,
'protected_to_private' => true,
'return_assignment' => true,
'return_type_declaration' => ['space_before' => 'none'],
'self_accessor' => true,
'self_static_accessor' => true,
'semicolon_after_instruction' => true,
'set_type_to_cast' => true,
'short_scalar_cast' => true,
'simple_to_complex_string_variable' => true,
'simplified_null_return' => false,
'single_blank_line_at_eof' => true,
'single_import_per_statement' => true,
'single_line_after_imports' => true,
'single_quote' => true,
'standardize_not_equals' => true,
'strict_param' => true,
'ternary_to_null_coalescing' => true,
'trailing_comma_in_multiline_array' => true,
'trim_array_spaces' => true,
'unary_operator_spaces' => true,
'visibility_required' => [
'elements' => [
'const',
'method',
'property',
],
],
'void_return' => true,
'whitespace_after_comma_in_array' => true,
]);
phpunit-lines-of-code-1.0.3/.psalm/ 0000775 0000000 0000000 00000000000 13764430207 0017024 5 ustar 00root root 0000000 0000000 phpunit-lines-of-code-1.0.3/.psalm/baseline.xml 0000664 0000000 0000000 00000000157 13764430207 0021333 0 ustar 00root root 0000000 0000000
phpunit-lines-of-code-1.0.3/.psalm/config.xml 0000664 0000000 0000000 00000000752 13764430207 0021017 0 ustar 00root root 0000000 0000000
phpunit-lines-of-code-1.0.3/ChangeLog.md 0000664 0000000 0000000 00000001751 13764430207 0020007 0 ustar 00root root 0000000 0000000 # ChangeLog
All notable changes are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.
## [1.0.3] - 2020-11-28
### Fixed
* Files that do not contain a newline were not handled correctly
### Changed
* A line of code is no longer considered to be a Logical Line of Code if it does not contain an `Expr` node
## [1.0.2] - 2020-10-26
### Fixed
* `SebastianBergmann\LinesOfCode\Exception` now correctly extends `\Throwable`
## [1.0.1] - 2020-09-28
### Changed
* Changed PHP version constraint in `composer.json` from `^7.3 || ^8.0` to `>=7.3`
## [1.0.0] - 2020-07-22
* Initial release
[1.0.3]: https://github.com/sebastianbergmann/lines-of-code/compare/1.0.2...1.0.3
[1.0.2]: https://github.com/sebastianbergmann/lines-of-code/compare/1.0.1...1.0.2
[1.0.1]: https://github.com/sebastianbergmann/lines-of-code/compare/1.0.0...1.0.1
[1.0.0]: https://github.com/sebastianbergmann/lines-of-code/compare/f959e71f00e591288acc024afe9cb966c6cf9bd6...1.0.0
phpunit-lines-of-code-1.0.3/LICENSE 0000664 0000000 0000000 00000003021 13764430207 0016633 0 ustar 00root root 0000000 0000000 sebastian/lines-of-code
Copyright (c) 2020, Sebastian Bergmann .
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Sebastian Bergmann nor the names of his
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
phpunit-lines-of-code-1.0.3/README.md 0000664 0000000 0000000 00000002065 13764430207 0017114 0 ustar 00root root 0000000 0000000 # sebastian/lines-of-code
Library for counting the lines of code in PHP source code.
[](https://packagist.org/packages/sebastian/lines-of-code)
[](https://php.net/)
[](https://phpunit.de/build-status.html)
[](https://shepherd.dev/github/sebastianbergmann/lines-of-code)
## Installation
You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):
```
composer require sebastian/lines-of-code
```
If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:
```
composer require --dev sebastian/lines-of-code
```
phpunit-lines-of-code-1.0.3/build.xml 0000664 0000000 0000000 00000001625 13764430207 0017457 0 ustar 00root root 0000000 0000000
phpunit-lines-of-code-1.0.3/composer.json 0000664 0000000 0000000 00000001746 13764430207 0020364 0 ustar 00root root 0000000 0000000 {
"name": "sebastian/lines-of-code",
"description": "Library for counting the lines of code in PHP source code",
"type": "library",
"homepage": "https://github.com/sebastianbergmann/lines-of-code",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Sebastian Bergmann",
"email": "sebastian@phpunit.de",
"role": "lead"
}
],
"support": {
"issues": "https://github.com/sebastianbergmann/lines-of-code/issues"
},
"prefer-stable": true,
"require": {
"php": ">=7.3",
"nikic/php-parser": "^4.6"
},
"require-dev": {
"phpunit/phpunit": "^9.3"
},
"config": {
"platform": {
"php": "7.3.0"
},
"optimize-autoloader": true,
"sort-packages": true
},
"autoload": {
"classmap": [
"src/"
]
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}
phpunit-lines-of-code-1.0.3/infection.json 0000664 0000000 0000000 00000000256 13764430207 0020506 0 ustar 00root root 0000000 0000000 {
"source": {
"directories": [
"src"
]
},
"mutators": {
"@default": true
},
"minMsi": 100,
"minCoveredMsi": 100
}
phpunit-lines-of-code-1.0.3/phpunit.xml 0000664 0000000 0000000 00000001512 13764430207 0020042 0 ustar 00root root 0000000 0000000
tests
src
phpunit-lines-of-code-1.0.3/src/ 0000775 0000000 0000000 00000000000 13764430207 0016421 5 ustar 00root root 0000000 0000000 phpunit-lines-of-code-1.0.3/src/Counter.php 0000664 0000000 0000000 00000004443 13764430207 0020556 0 ustar 00root root 0000000 0000000
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\LinesOfCode;
use function substr_count;
use PhpParser\Error;
use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use PhpParser\ParserFactory;
final class Counter
{
/**
* @throws RuntimeException
*/
public function countInSourceFile(string $sourceFile): LinesOfCode
{
return $this->countInSourceString(file_get_contents($sourceFile));
}
/**
* @throws RuntimeException
*/
public function countInSourceString(string $source): LinesOfCode
{
$linesOfCode = substr_count($source, "\n");
if ($linesOfCode === 0 && !empty($source)) {
$linesOfCode = 1;
}
try {
$nodes = $this->parser()->parse($source);
assert($nodes !== null);
return $this->countInAbstractSyntaxTree($linesOfCode, $nodes);
// @codeCoverageIgnoreStart
} catch (Error $error) {
throw new RuntimeException(
$error->getMessage(),
(int) $error->getCode(),
$error
);
}
// @codeCoverageIgnoreEnd
}
/**
* @param Node[] $nodes
*
* @throws RuntimeException
*/
public function countInAbstractSyntaxTree(int $linesOfCode, array $nodes): LinesOfCode
{
$traverser = new NodeTraverser;
$visitor = new LineCountingVisitor($linesOfCode);
$traverser->addVisitor($visitor);
try {
/* @noinspection UnusedFunctionResultInspection */
$traverser->traverse($nodes);
// @codeCoverageIgnoreStart
} catch (Error $error) {
throw new RuntimeException(
$error->getMessage(),
(int) $error->getCode(),
$error
);
}
// @codeCoverageIgnoreEnd
return $visitor->result();
}
private function parser(): Parser
{
return (new ParserFactory)->create(ParserFactory::PREFER_PHP7, new Lexer);
}
}
phpunit-lines-of-code-1.0.3/src/Exception/ 0000775 0000000 0000000 00000000000 13764430207 0020357 5 ustar 00root root 0000000 0000000 phpunit-lines-of-code-1.0.3/src/Exception/Exception.php 0000664 0000000 0000000 00000000561 13764430207 0023030 0 ustar 00root root 0000000 0000000
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\LinesOfCode;
use Throwable;
interface Exception extends Throwable
{
}
phpunit-lines-of-code-1.0.3/src/Exception/IllogicalValuesException.php 0000664 0000000 0000000 00000000641 13764430207 0026027 0 ustar 00root root 0000000 0000000
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\LinesOfCode;
use LogicException;
final class IllogicalValuesException extends LogicException implements Exception
{
}
phpunit-lines-of-code-1.0.3/src/Exception/NegativeValueException.php 0000664 0000000 0000000 00000000663 13764430207 0025513 0 ustar 00root root 0000000 0000000
*
* 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
{
}
phpunit-lines-of-code-1.0.3/src/Exception/RuntimeException.php 0000664 0000000 0000000 00000000607 13764430207 0024375 0 ustar 00root root 0000000 0000000
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\LinesOfCode;
final class RuntimeException extends \RuntimeException implements Exception
{
}
phpunit-lines-of-code-1.0.3/src/LineCountingVisitor.php 0000664 0000000 0000000 00000003646 13764430207 0023121 0 ustar 00root root 0000000 0000000
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\LinesOfCode;
use function array_merge;
use function array_unique;
use function count;
use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\NodeVisitorAbstract;
final class LineCountingVisitor extends NodeVisitorAbstract
{
/**
* @var int
*/
private $linesOfCode;
/**
* @var Comment[]
*/
private $comments = [];
/**
* @var int[]
*/
private $linesWithStatements = [];
public function __construct(int $linesOfCode)
{
$this->linesOfCode = $linesOfCode;
}
public function enterNode(Node $node): void
{
$this->comments = array_merge($this->comments, $node->getComments());
if (!$node instanceof Expr) {
return;
}
$this->linesWithStatements[] = $node->getStartLine();
}
public function result(): LinesOfCode
{
$commentLinesOfCode = 0;
foreach ($this->comments() as $comment) {
$commentLinesOfCode += ($comment->getEndLine() - $comment->getStartLine() + 1);
}
return new LinesOfCode(
$this->linesOfCode,
$commentLinesOfCode,
$this->linesOfCode - $commentLinesOfCode,
count(array_unique($this->linesWithStatements))
);
}
/**
* @return Comment[]
*/
private function comments(): array
{
$comments = [];
foreach ($this->comments as $comment) {
$comments[$comment->getStartLine() . '_' . $comment->getStartTokenPos() . '_' . $comment->getEndLine() . '_' . $comment->getEndTokenPos()] = $comment;
}
return $comments;
}
}
phpunit-lines-of-code-1.0.3/src/LinesOfCode.php 0000664 0000000 0000000 00000005073 13764430207 0021271 0 ustar 00root root 0000000 0000000
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\LinesOfCode;
/**
* @psalm-immutable
*/
final class LinesOfCode
{
/**
* @var int
*/
private $linesOfCode;
/**
* @var int
*/
private $commentLinesOfCode;
/**
* @var int
*/
private $nonCommentLinesOfCode;
/**
* @var int
*/
private $logicalLinesOfCode;
/**
* @throws IllogicalValuesException
* @throws NegativeValueException
*/
public function __construct(int $linesOfCode, int $commentLinesOfCode, int $nonCommentLinesOfCode, int $logicalLinesOfCode)
{
if ($linesOfCode < 0) {
throw new NegativeValueException('$linesOfCode must not be negative');
}
if ($commentLinesOfCode < 0) {
throw new NegativeValueException('$commentLinesOfCode must not be negative');
}
if ($nonCommentLinesOfCode < 0) {
throw new NegativeValueException('$nonCommentLinesOfCode must not be negative');
}
if ($logicalLinesOfCode < 0) {
throw new NegativeValueException('$logicalLinesOfCode must not be negative');
}
if ($linesOfCode - $commentLinesOfCode !== $nonCommentLinesOfCode) {
throw new IllogicalValuesException('$linesOfCode !== $commentLinesOfCode + $nonCommentLinesOfCode');
}
$this->linesOfCode = $linesOfCode;
$this->commentLinesOfCode = $commentLinesOfCode;
$this->nonCommentLinesOfCode = $nonCommentLinesOfCode;
$this->logicalLinesOfCode = $logicalLinesOfCode;
}
public function linesOfCode(): int
{
return $this->linesOfCode;
}
public function commentLinesOfCode(): int
{
return $this->commentLinesOfCode;
}
public function nonCommentLinesOfCode(): int
{
return $this->nonCommentLinesOfCode;
}
public function logicalLinesOfCode(): int
{
return $this->logicalLinesOfCode;
}
public function plus(self $other): self
{
return new self(
$this->linesOfCode() + $other->linesOfCode(),
$this->commentLinesOfCode() + $other->commentLinesOfCode(),
$this->nonCommentLinesOfCode() + $other->nonCommentLinesOfCode(),
$this->logicalLinesOfCode() + $other->logicalLinesOfCode(),
);
}
}
phpunit-lines-of-code-1.0.3/tests/ 0000775 0000000 0000000 00000000000 13764430207 0016774 5 ustar 00root root 0000000 0000000 phpunit-lines-of-code-1.0.3/tests/_fixture/ 0000775 0000000 0000000 00000000000 13764430207 0020621 5 ustar 00root root 0000000 0000000 phpunit-lines-of-code-1.0.3/tests/_fixture/ExampleClass.php 0000664 0000000 0000000 00000001606 13764430207 0023716 0 ustar 00root root 0000000 0000000
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\LinesOfCode;
use function file_get_contents;
use PhpParser\Lexer;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PHPUnit\Framework\TestCase;
/**
* @covers \SebastianBergmann\LinesOfCode\Counter
* @covers \SebastianBergmann\LinesOfCode\LineCountingVisitor
*
* @uses \SebastianBergmann\LinesOfCode\LinesOfCode
*
* @medium
*/
final class CounterTest extends TestCase
{
public function testCountsLinesOfCodeInSourceFile(): void
{
$count = (new Counter)->countInSourceFile(__DIR__ . '/../_fixture/ExampleClass.php');
$this->assertSame(43, $count->linesOfCode());
$this->assertSame(5, $count->commentLinesOfCode());
$this->assertSame(38, $count->nonCommentLinesOfCode());
$this->assertSame(13, $count->logicalLinesOfCode());
}
public function testCountsLinesOfCodeInSourceString(): void
{
$count = (new Counter)->countInSourceString(file_get_contents(__DIR__ . '/../_fixture/ExampleClass.php'));
$this->assertSame(43, $count->linesOfCode());
$this->assertSame(5, $count->commentLinesOfCode());
$this->assertSame(38, $count->nonCommentLinesOfCode());
$this->assertSame(13, $count->logicalLinesOfCode());
}
public function testCountsLinesOfCodeInAbstractSyntaxTree(): void
{
$nodes = $this->parser()->parse(
file_get_contents(__DIR__ . '/../_fixture/ExampleClass.php')
);
assert($nodes !== null);
$count = (new Counter)->countInAbstractSyntaxTree(51, $nodes);
$this->assertSame(51, $count->linesOfCode());
$this->assertSame(5, $count->commentLinesOfCode());
$this->assertSame(46, $count->nonCommentLinesOfCode());
$this->assertSame(13, $count->logicalLinesOfCode());
}
public function testHandlesFileWithoutNewline(): void
{
$count = (new Counter)->countInSourceFile(__DIR__ . '/../_fixture/source_without_newline.php');
$this->assertSame(1, $count->linesOfCode());
$this->assertSame(1, $count->commentLinesOfCode());
$this->assertSame(0, $count->nonCommentLinesOfCode());
$this->assertSame(0, $count->logicalLinesOfCode());
}
private function parser(): Parser
{
return (new ParserFactory)->create(ParserFactory::PREFER_PHP7, new Lexer);
}
}
phpunit-lines-of-code-1.0.3/tests/unit/ 0000775 0000000 0000000 00000000000 13764430207 0017753 5 ustar 00root root 0000000 0000000 phpunit-lines-of-code-1.0.3/tests/unit/LineCountingVisitorTest.php 0000664 0000000 0000000 00000004437 13764430207 0025312 0 ustar 00root root 0000000 0000000
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\LinesOfCode;
use function file_get_contents;
use PhpParser\Lexer;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PHPUnit\Framework\TestCase;
/**
* @covers \SebastianBergmann\LinesOfCode\LineCountingVisitor
*
* @uses \SebastianBergmann\LinesOfCode\LinesOfCode
*
* @small
*/
final class LineCountingVisitorTest extends TestCase
{
/**
* @dataProvider provideData
*/
public function testCountsLinesOfCodeInAbstractSyntaxTree(string $sourceFile, int $linesOfCode, int $commentLinesOfCode, int $nonCommentLinesOfCode, int $logicalLinesOfCode): void
{
$nodes = $this->parser()->parse(
file_get_contents($sourceFile)
);
$traverser = new NodeTraverser;
$visitor = new LineCountingVisitor($linesOfCode);
$traverser->addVisitor($visitor);
/* @noinspection UnusedFunctionResultInspection */
$traverser->traverse($nodes);
$this->assertSame($linesOfCode, $visitor->result()->linesOfCode());
$this->assertSame($commentLinesOfCode, $visitor->result()->commentLinesOfCode());
$this->assertSame($nonCommentLinesOfCode, $visitor->result()->nonCommentLinesOfCode());
$this->assertSame($logicalLinesOfCode, $visitor->result()->logicalLinesOfCode());
}
public function provideData(): array
{
return [
[
__DIR__ . '/../_fixture/ExampleClass.php',
51,
5,
46,
13,
],
[
__DIR__ . '/../_fixture/source_with_ignore.php',
44,
9,
35,
4,
],
[
__DIR__ . '/../_fixture/source_without_newline.php',
1,
1,
0,
0,
],
];
}
private function parser(): Parser
{
return (new ParserFactory)->create(ParserFactory::PREFER_PHP7, new Lexer);
}
}
phpunit-lines-of-code-1.0.3/tests/unit/LinesOfCodeTest.php 0000664 0000000 0000000 00000006224 13764430207 0023462 0 ustar 00root root 0000000 0000000
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\LinesOfCode;
use PHPUnit\Framework\TestCase;
/**
* @covers \SebastianBergmann\LinesOfCode\LinesOfCode
*
* @small
*/
final class LinesOfCodeTest extends TestCase
{
/**
* @testdox Has Lines of Code (LOC)
*/
public function testHasLinesOfCode(): void
{
$this->assertSame(1, $this->linesOfCode()->linesOfCode());
}
/**
* @testdox Has Comment Lines of Code (CLOC)
*/
public function testHasCommentLinesOfCode(): void
{
$this->assertSame(1, $this->linesOfCode()->commentLinesOfCode());
}
/**
* @testdox Has Non-Comment Lines of Code (NCLOC)
*/
public function testHasNonCommentLinesOfCode(): void
{
$this->assertSame(0, $this->linesOfCode()->nonCommentLinesOfCode());
}
/**
* @testdox Has Logical Lines of Code (LLOC)
*/
public function testHasLogicalLinesOfCode(): void
{
$this->assertSame(0, $this->linesOfCode()->logicalLinesOfCode());
}
public function testLinesOfCodeCannotBeNegative(): void
{
$this->expectException(NegativeValueException::class);
$this->expectExceptionMessage('$linesOfCode must not be negative');
new LinesOfCode(-1, 0, 0, 0);
}
public function testCommentLinesOfCodeCannotBeNegative(): void
{
$this->expectException(NegativeValueException::class);
$this->expectExceptionMessage('$commentLinesOfCode must not be negative');
new LinesOfCode(0, -1, 0, 0);
}
public function testNonCommentLinesOfCodeCannotBeNegative(): void
{
$this->expectException(NegativeValueException::class);
$this->expectExceptionMessage('$nonCommentLinesOfCode must not be negative');
new LinesOfCode(0, 0, -1, 0);
}
public function testLogicalLinesOfCodeCannotBeNegative(): void
{
$this->expectException(NegativeValueException::class);
$this->expectExceptionMessage('$logicalLinesOfCode must not be negative');
new LinesOfCode(0, 0, 0, -1);
}
/**
* @testdox Lines of Code = Comment Lines of Code + Non-Comment Lines of Code
*/
public function testNumbersHaveToMakeSense(): void
{
$this->expectException(IllogicalValuesException::class);
$this->expectExceptionMessage('$linesOfCode !== $commentLinesOfCode + $nonCommentLinesOfCode');
new LinesOfCode(1, 2, 2, 0);
}
public function testTwoInstancesCanBeAdded(): void
{
$a = new LinesOfCode(2, 1, 1, 1);
$b = new LinesOfCode(4, 2, 2, 2);
$sum = $a->plus($b);
$this->assertSame(6, $sum->linesOfCode());
$this->assertSame(3, $sum->commentLinesOfCode());
$this->assertSame(3, $sum->nonCommentLinesOfCode());
$this->assertSame(3, $sum->logicalLinesOfCode());
}
private function linesOfCode(): LinesOfCode
{
return new LinesOfCode(1, 1, 0, 0);
}
}