first commit

This commit is contained in:
Sampanna Rimal
2024-08-27 17:48:06 +05:45
commit 53c0140f58
10839 changed files with 1125847 additions and 0 deletions

View File

@ -0,0 +1,90 @@
<?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 function assert;
use function file_get_contents;
use function substr_count;
use PhpParser\Error;
use PhpParser\Node;
use PhpParser\NodeTraverser;
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;
}
assert($linesOfCode >= 0);
try {
$nodes = (new ParserFactory)->createForHostVersion()->parse($source);
assert($nodes !== null);
return $this->countInAbstractSyntaxTree($linesOfCode, $nodes);
// @codeCoverageIgnoreStart
} catch (Error $error) {
throw new RuntimeException(
$error->getMessage(),
$error->getCode(),
$error,
);
}
// @codeCoverageIgnoreEnd
}
/**
* @psalm-param non-negative-int $linesOfCode
*
* @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(),
$error->getCode(),
$error,
);
}
// @codeCoverageIgnoreEnd
return $visitor->result();
}
}

View File

@ -0,0 +1,16 @@
<?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 Throwable;
interface Exception extends Throwable
{
}

View File

@ -0,0 +1,16 @@
<?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 LogicException;
final class IllogicalValuesException extends LogicException implements Exception
{
}

View File

@ -0,0 +1,16 @@
<?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
{
}

View File

@ -0,0 +1,14 @@
<?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;
final class RuntimeException extends \RuntimeException implements Exception
{
}

View File

@ -0,0 +1,93 @@
<?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 function array_merge;
use function array_unique;
use function assert;
use function count;
use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\NodeVisitorAbstract;
final class LineCountingVisitor extends NodeVisitorAbstract
{
/**
* @psalm-var non-negative-int
*/
private readonly int $linesOfCode;
/**
* @var Comment[]
*/
private array $comments = [];
/**
* @var int[]
*/
private array $linesWithStatements = [];
/**
* @psalm-param non-negative-int $linesOfCode
*/
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);
}
$nonCommentLinesOfCode = $this->linesOfCode - $commentLinesOfCode;
$logicalLinesOfCode = count(array_unique($this->linesWithStatements));
assert($commentLinesOfCode >= 0);
assert($nonCommentLinesOfCode >= 0);
assert($logicalLinesOfCode >= 0);
return new LinesOfCode(
$this->linesOfCode,
$commentLinesOfCode,
$nonCommentLinesOfCode,
$logicalLinesOfCode,
);
}
/**
* @return Comment[]
*/
private function comments(): array
{
$comments = [];
foreach ($this->comments as $comment) {
$comments[$comment->getStartLine() . '_' . $comment->getStartTokenPos() . '_' . $comment->getEndLine() . '_' . $comment->getEndTokenPos()] = $comment;
}
return $comments;
}
}

View File

@ -0,0 +1,119 @@
<?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;
/**
* @psalm-immutable
*/
final class LinesOfCode
{
/**
* @psalm-var non-negative-int
*/
private readonly int $linesOfCode;
/**
* @psalm-var non-negative-int
*/
private readonly int $commentLinesOfCode;
/**
* @psalm-var non-negative-int
*/
private readonly int $nonCommentLinesOfCode;
/**
* @psalm-var non-negative-int
*/
private readonly int $logicalLinesOfCode;
/**
* @psalm-param non-negative-int $linesOfCode
* @psalm-param non-negative-int $commentLinesOfCode
* @psalm-param non-negative-int $nonCommentLinesOfCode
* @psalm-param non-negative-int $logicalLinesOfCode
*
* @throws IllogicalValuesException
* @throws NegativeValueException
*/
public function __construct(int $linesOfCode, int $commentLinesOfCode, int $nonCommentLinesOfCode, int $logicalLinesOfCode)
{
/** @psalm-suppress DocblockTypeContradiction */
if ($linesOfCode < 0) {
throw new NegativeValueException('$linesOfCode must not be negative');
}
/** @psalm-suppress DocblockTypeContradiction */
if ($commentLinesOfCode < 0) {
throw new NegativeValueException('$commentLinesOfCode must not be negative');
}
/** @psalm-suppress DocblockTypeContradiction */
if ($nonCommentLinesOfCode < 0) {
throw new NegativeValueException('$nonCommentLinesOfCode must not be negative');
}
/** @psalm-suppress DocblockTypeContradiction */
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;
}
/**
* @psalm-return non-negative-int
*/
public function linesOfCode(): int
{
return $this->linesOfCode;
}
/**
* @psalm-return non-negative-int
*/
public function commentLinesOfCode(): int
{
return $this->commentLinesOfCode;
}
/**
* @psalm-return non-negative-int
*/
public function nonCommentLinesOfCode(): int
{
return $this->nonCommentLinesOfCode;
}
/**
* @psalm-return non-negative-int
*/
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(),
);
}
}