Работает обработка ссылок на таблицы с численностью

This commit is contained in:
2024-09-06 14:11:38 +03:00
parent 04374fef40
commit 2be45826c1
1698 changed files with 138656 additions and 174 deletions

View File

@ -0,0 +1,84 @@
# ChangeLog
All notable changes are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.
## [4.0.1] - 2024-07-03
### Changed
* This project now uses PHPStan instead of Psalm for static analysis
## [4.0.0] - 2024-02-02
### Removed
* This component now requires PHP-Parser 5
* This component is no longer supported on PHP 8.1
## [3.2.0] - 2023-12-21
### Added
* `ComplexityCollection::sortByDescendingCyclomaticComplexity()`
* Support for `match` arms
### Changed
* This component is now compatible with `nikic/php-parser` 5.0
## [3.1.0] - 2023-09-28
### Added
* `Complexity::isFunction()` and `Complexity::isMethod()`
* `ComplexityCollection::isFunction()` and `ComplexityCollection::isMethod()`
* `ComplexityCollection::mergeWith()`
### Fixed
* Anonymous classes are not processed correctly
## [3.0.1] - 2023-08-31
### Fixed
* [#7](https://github.com/sebastianbergmann/complexity/pull/7): `ComplexityCalculatingVisitor` tries to process interface methods
## [3.0.0] - 2023-02-03
### Removed
* This component is no longer supported on PHP 7.3, PHP 7.4 and PHP 8.0
## [2.0.2] - 2020-10-26
### Fixed
* `SebastianBergmann\Complexity\Exception` now correctly extends `\Throwable`
## [2.0.1] - 2020-09-28
### Changed
* Changed PHP version constraint in `composer.json` from `^7.3 || ^8.0` to `>=7.3`
## [2.0.0] - 2020-07-25
### Removed
* The `ParentConnectingVisitor` has been removed (it should have been marked as `@internal`)
## [1.0.0] - 2020-07-22
* Initial release
[4.0.1]: https://github.com/sebastianbergmann/complexity/compare/4.0.0...4.0.1
[4.0.0]: https://github.com/sebastianbergmann/complexity/compare/3.2...4.0.0
[3.2.0]: https://github.com/sebastianbergmann/complexity/compare/3.1.0...3.2.0
[3.1.0]: https://github.com/sebastianbergmann/complexity/compare/3.0.1...3.1.0
[3.0.1]: https://github.com/sebastianbergmann/complexity/compare/3.0.0...3.0.1
[3.0.0]: https://github.com/sebastianbergmann/complexity/compare/2.0.2...3.0.0
[2.0.2]: https://github.com/sebastianbergmann/complexity/compare/2.0.1...2.0.2
[2.0.1]: https://github.com/sebastianbergmann/complexity/compare/2.0.0...2.0.1
[2.0.0]: https://github.com/sebastianbergmann/complexity/compare/1.0.0...2.0.0
[1.0.0]: https://github.com/sebastianbergmann/complexity/compare/70ee0ad32d9e2be3f85beffa3e2eb474193f2487...1.0.0

29
vendor/sebastian/complexity/LICENSE vendored Normal file
View File

@ -0,0 +1,29 @@
BSD 3-Clause License
Copyright (c) 2020-2024, 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:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. 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.
3. Neither the name of the copyright holder nor the names of its
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 HOLDER 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.

21
vendor/sebastian/complexity/README.md vendored Normal file
View File

@ -0,0 +1,21 @@
[![Latest Stable Version](https://poser.pugx.org/sebastian/complexity/v/stable.png)](https://packagist.org/packages/sebastian/complexity)
[![CI Status](https://github.com/sebastianbergmann/complexity/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/complexity/actions)
[![codecov](https://codecov.io/gh/sebastianbergmann/complexity/branch/main/graph/badge.svg)](https://codecov.io/gh/sebastianbergmann/complexity)
# sebastian/complexity
Library for calculating the complexity of PHP code units.
## Installation
You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):
```
composer require sebastian/complexity
```
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/complexity
```

30
vendor/sebastian/complexity/SECURITY.md vendored Normal file
View File

@ -0,0 +1,30 @@
# Security Policy
If you believe you have found a security vulnerability in the library that is developed in this repository, please report it to us through coordinated disclosure.
**Please do not report security vulnerabilities through public GitHub issues, discussions, or pull requests.**
Instead, please email `sebastian@phpunit.de`.
Please include as much of the information listed below as you can to help us better understand and resolve the issue:
* The type of issue
* Full paths of source file(s) related to the manifestation of the issue
* The location of the affected source code (tag/branch/commit or direct URL)
* Any special configuration required to reproduce the issue
* Step-by-step instructions to reproduce the issue
* Proof-of-concept or exploit code (if possible)
* Impact of the issue, including how an attacker might exploit the issue
This information will help us triage your report more quickly.
## Web Context
The library that is developed in this repository was either extracted from [PHPUnit](https://github.com/sebastianbergmann/phpunit) or developed specifically as a dependency for PHPUnit.
The library is developed with a focus on development environments and the command-line. No specific testing or hardening with regard to using the library in an HTTP or web context or with untrusted input data is performed. The library might also contain functionality that intentionally exposes internal application data for debugging purposes.
If the library is used in a web application, the application developer is responsible for filtering inputs or escaping outputs as necessary and for verifying that the used functionality is safe for use within the intended context.
Vulnerabilities specific to the use outside a development context will be fixed as applicable, provided that the fix does not have an averse effect on the primary use case for development purposes.

View File

@ -0,0 +1,43 @@
{
"name": "sebastian/complexity",
"description": "Library for calculating the complexity of PHP code units",
"type": "library",
"homepage": "https://github.com/sebastianbergmann/complexity",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Sebastian Bergmann",
"email": "sebastian@phpunit.de",
"role": "lead"
}
],
"support": {
"issues": "https://github.com/sebastianbergmann/complexity/issues",
"security": "https://github.com/sebastianbergmann/complexity/security/policy"
},
"prefer-stable": true,
"require": {
"php": ">=8.2",
"nikic/php-parser": "^5.0"
},
"require-dev": {
"phpunit/phpunit": "^11.0"
},
"config": {
"platform": {
"php": "8.2.0"
},
"optimize-autoloader": true,
"sort-packages": true
},
"autoload": {
"classmap": [
"src/"
]
},
"extra": {
"branch-alias": {
"dev-main": "4.0-dev"
}
}
}

View File

@ -0,0 +1,95 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/complexity.
*
* (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\Complexity;
use function assert;
use function file_exists;
use function file_get_contents;
use function is_readable;
use function is_string;
use PhpParser\Error;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\NodeVisitor\ParentConnectingVisitor;
use PhpParser\ParserFactory;
final class Calculator
{
/**
* @param non-empty-string $sourceFile
*
* @throws RuntimeException
*/
public function calculateForSourceFile(string $sourceFile): ComplexityCollection
{
assert(file_exists($sourceFile));
assert(is_readable($sourceFile));
$source = file_get_contents($sourceFile);
assert(is_string($source));
return $this->calculateForSourceString($source);
}
/**
* @throws RuntimeException
*/
public function calculateForSourceString(string $source): ComplexityCollection
{
try {
$nodes = (new ParserFactory)->createForHostVersion()->parse($source);
assert($nodes !== null);
return $this->calculateForAbstractSyntaxTree($nodes);
// @codeCoverageIgnoreStart
} catch (Error $error) {
throw new RuntimeException(
$error->getMessage(),
$error->getCode(),
$error,
);
}
// @codeCoverageIgnoreEnd
}
/**
* @param Node[] $nodes
*
* @throws RuntimeException
*/
public function calculateForAbstractSyntaxTree(array $nodes): ComplexityCollection
{
$traverser = new NodeTraverser;
$complexityCalculatingVisitor = new ComplexityCalculatingVisitor(true);
$traverser->addVisitor(new NameResolver);
$traverser->addVisitor(new ParentConnectingVisitor);
$traverser->addVisitor($complexityCalculatingVisitor);
try {
/* @noinspection UnusedFunctionResultInspection */
$traverser->traverse($nodes);
// @codeCoverageIgnoreStart
} catch (Error $error) {
throw new RuntimeException(
$error->getMessage(),
$error->getCode(),
$error,
);
}
// @codeCoverageIgnoreEnd
return $complexityCalculatingVisitor->result();
}
}

View File

@ -0,0 +1,64 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/complexity.
*
* (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\Complexity;
use function str_contains;
/**
* @immutable
*/
final readonly class Complexity
{
/**
* @var non-empty-string
*/
private string $name;
/**
* @var positive-int
*/
private int $cyclomaticComplexity;
/**
* @param non-empty-string $name
* @param positive-int $cyclomaticComplexity
*/
public function __construct(string $name, int $cyclomaticComplexity)
{
$this->name = $name;
$this->cyclomaticComplexity = $cyclomaticComplexity;
}
/**
* @return non-empty-string
*/
public function name(): string
{
return $this->name;
}
/**
* @return positive-int
*/
public function cyclomaticComplexity(): int
{
return $this->cyclomaticComplexity;
}
public function isFunction(): bool
{
return !$this->isMethod();
}
public function isMethod(): bool
{
return str_contains($this->name, '::');
}
}

View File

@ -0,0 +1,134 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/complexity.
*
* (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\Complexity;
use function array_filter;
use function array_merge;
use function array_reverse;
use function array_values;
use function count;
use function usort;
use Countable;
use IteratorAggregate;
/**
* @template-implements IteratorAggregate<int, Complexity>
*
* @psalm-immutable
*/
final readonly class ComplexityCollection implements Countable, IteratorAggregate
{
/**
* @var list<Complexity>
*/
private array $items;
public static function fromList(Complexity ...$items): self
{
return new self($items);
}
/**
* @param list<Complexity> $items
*/
private function __construct(array $items)
{
$this->items = $items;
}
/**
* @return list<Complexity>
*/
public function asArray(): array
{
return $this->items;
}
public function getIterator(): ComplexityCollectionIterator
{
return new ComplexityCollectionIterator($this);
}
/**
* @return non-negative-int
*/
public function count(): int
{
return count($this->items);
}
public function isEmpty(): bool
{
return empty($this->items);
}
/**
* @return non-negative-int
*/
public function cyclomaticComplexity(): int
{
$cyclomaticComplexity = 0;
foreach ($this as $item) {
$cyclomaticComplexity += $item->cyclomaticComplexity();
}
return $cyclomaticComplexity;
}
public function isFunction(): self
{
return new self(
array_values(
array_filter(
$this->items,
static fn (Complexity $complexity): bool => $complexity->isFunction(),
),
),
);
}
public function isMethod(): self
{
return new self(
array_values(
array_filter(
$this->items,
static fn (Complexity $complexity): bool => $complexity->isMethod(),
),
),
);
}
public function mergeWith(self $other): self
{
return new self(
array_merge(
$this->asArray(),
$other->asArray(),
),
);
}
public function sortByDescendingCyclomaticComplexity(): self
{
$items = $this->items;
usort(
$items,
static function (Complexity $a, Complexity $b): int
{
return $a->cyclomaticComplexity() <=> $b->cyclomaticComplexity();
},
);
return new self(array_reverse($items));
}
}

View File

@ -0,0 +1,54 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/complexity.
*
* (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\Complexity;
use Iterator;
/**
* @template-implements Iterator<int, Complexity>
*/
final class ComplexityCollectionIterator implements Iterator
{
/**
* @var list<Complexity>
*/
private readonly array $items;
private int $position = 0;
public function __construct(ComplexityCollection $items)
{
$this->items = $items->asArray();
}
public function rewind(): void
{
$this->position = 0;
}
public function valid(): bool
{
return isset($this->items[$this->position]);
}
public function key(): int
{
return $this->position;
}
public function current(): Complexity
{
return $this->items[$this->position];
}
public function next(): void
{
$this->position++;
}
}

View File

@ -0,0 +1,16 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/complexity.
*
* (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\Complexity;
use Throwable;
interface Exception extends Throwable
{
}

View File

@ -0,0 +1,14 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/complexity.
*
* (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\Complexity;
final class RuntimeException extends \RuntimeException implements Exception
{
}

View File

@ -0,0 +1,133 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/complexity.
*
* (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\Complexity;
use function assert;
use function is_array;
use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use PhpParser\NodeVisitorAbstract;
final class ComplexityCalculatingVisitor extends NodeVisitorAbstract
{
/**
* @var list<Complexity>
*/
private array $result = [];
private bool $shortCircuitTraversal;
public function __construct(bool $shortCircuitTraversal)
{
$this->shortCircuitTraversal = $shortCircuitTraversal;
}
public function enterNode(Node $node): ?int
{
if (!$node instanceof ClassMethod && !$node instanceof Function_) {
return null;
}
if ($node instanceof ClassMethod) {
if ($node->getAttribute('parent') instanceof Interface_) {
return null;
}
if ($node->isAbstract()) {
return null;
}
$name = $this->classMethodName($node);
} else {
$name = $this->functionName($node);
}
$statements = $node->getStmts();
assert(is_array($statements));
$this->result[] = new Complexity(
$name,
$this->cyclomaticComplexity($statements),
);
if ($this->shortCircuitTraversal) {
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
}
return null;
}
public function result(): ComplexityCollection
{
return ComplexityCollection::fromList(...$this->result);
}
/**
* @param Stmt[] $statements
*
* @return positive-int
*/
private function cyclomaticComplexity(array $statements): int
{
$traverser = new NodeTraverser;
$cyclomaticComplexityCalculatingVisitor = new CyclomaticComplexityCalculatingVisitor;
$traverser->addVisitor($cyclomaticComplexityCalculatingVisitor);
/* @noinspection UnusedFunctionResultInspection */
$traverser->traverse($statements);
return $cyclomaticComplexityCalculatingVisitor->cyclomaticComplexity();
}
/**
* @return non-empty-string
*/
private function classMethodName(ClassMethod $node): string
{
$parent = $node->getAttribute('parent');
assert($parent instanceof Class_ || $parent instanceof Trait_);
if ($parent->getAttribute('parent') instanceof New_) {
return 'anonymous class';
}
assert(isset($parent->namespacedName));
assert($parent->namespacedName instanceof Name);
return $parent->namespacedName->toString() . '::' . $node->name->toString();
}
/**
* @return non-empty-string
*/
private function functionName(Function_ $node): string
{
assert(isset($node->namespacedName));
assert($node->namespacedName instanceof Name);
$functionName = $node->namespacedName->toString();
assert($functionName !== '');
return $functionName;
}
}

View File

@ -0,0 +1,61 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/complexity.
*
* (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\Complexity;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\LogicalAnd;
use PhpParser\Node\Expr\BinaryOp\LogicalOr;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Stmt\Case_;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\For_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\While_;
use PhpParser\NodeVisitorAbstract;
final class CyclomaticComplexityCalculatingVisitor extends NodeVisitorAbstract
{
/**
* @var positive-int
*/
private int $cyclomaticComplexity = 1;
public function enterNode(Node $node): void
{
switch ($node::class) {
case BooleanAnd::class:
case BooleanOr::class:
case Case_::class:
case Catch_::class:
case ElseIf_::class:
case For_::class:
case Foreach_::class:
case If_::class:
case LogicalAnd::class:
case LogicalOr::class:
case Node\MatchArm::class:
case Ternary::class:
case While_::class:
$this->cyclomaticComplexity++;
}
}
/**
* @return positive-int
*/
public function cyclomaticComplexity(): int
{
return $this->cyclomaticComplexity;
}
}