File UnionTypeVisitor.php
has 2798 lines of code (exceeds 250 allowed). Consider refactoring. Open
<?php
declare(strict_types=1);
namespace Phan\AST;
UnionTypeVisitor
has 90 functions (exceeds 20 allowed). Consider refactoring. Open
class UnionTypeVisitor extends AnalysisVisitor
{
/**
* If an dynamic unpacked array has more elements than this, then give up on building up the union type
*/
Function visitDim
has a Cognitive Complexity of 76 (exceeds 5 allowed). Consider refactoring. Open
public function visitDim(Node $node, bool $treat_undef_as_nullable = false): UnionType
{
$union_type = self::unionTypeFromNode(
$this->code_base,
$this->context,
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function visitVar
has a Cognitive Complexity of 54 (exceeds 5 allowed). Consider refactoring. Open
public function visitVar(Node $node): UnionType
{
// $$var or ${...} (whose idea was that anyway?)
$name_node = $node->children['name'];
if (($name_node instanceof Node)) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function resolveArrayShapeElementTypesForOffset
has a Cognitive Complexity of 51 (exceeds 5 allowed). Consider refactoring. Open
public static function resolveArrayShapeElementTypesForOffset(UnionType $union_type, $dim_value, bool $is_computing_real_type_set = false)
{
/**
* @var bool $has_non_array_shape_type this will be true if there are types that support array access
* but have unknown array shapes in $union_type
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method visitDim
has 173 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function visitDim(Node $node, bool $treat_undef_as_nullable = false): UnionType
{
$union_type = self::unionTypeFromNode(
$this->code_base,
$this->context,
Function visitArray
has a Cognitive Complexity of 43 (exceeds 5 allowed). Consider refactoring. Open
public function visitArray(Node $node): UnionType
{
$children = $node->children;
if (\count($children) > 0) {
$key_set = $this->getEquivalentArraySet($node);
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function resolveArrayShapeElementTypes
has a Cognitive Complexity of 35 (exceeds 5 allowed). Consider refactoring. Open
private function resolveArrayShapeElementTypes(Node $node, UnionType $union_type): ?UnionType
{
$dim_node = $node->children['dim'];
$dim_value = $dim_node instanceof Node ? (new ContextNode($this->code_base, $this->context, $dim_node))->getEquivalentPHPScalarValue() : $dim_node;
// TODO: detect and warn about null
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function classTypesForNonName
has a Cognitive Complexity of 32 (exceeds 5 allowed). Consider refactoring. Open
private function classTypesForNonName(Node $node): UnionType
{
$node_type = UnionTypeVisitor::unionTypeFromNode(
$this->code_base,
$this->context,
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function visitMethodCall
has a Cognitive Complexity of 31 (exceeds 5 allowed). Consider refactoring. Open
public function visitMethodCall(Node $node): UnionType
{
$method_name = $node->children['method'] ?? '';
// Give up on any complicated nonsense where the
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function methodFQSENListFromParts
has a Cognitive Complexity of 30 (exceeds 5 allowed). Consider refactoring. Open
private function methodFQSENListFromParts($class_or_expr, $method_name): array
{
$code_base = $this->code_base;
$context = $this->context;
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function visitConditional
has a Cognitive Complexity of 29 (exceeds 5 allowed). Consider refactoring. Open
public function visitConditional(Node $node): UnionType
{
$cond_node = $node->children['cond'];
$cond_truthiness = self::checkCondUnconditionalTruthiness($cond_node);
// For the shorthand $a ?: $b, the cond node will be the truthy value.
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method visitVar
has 106 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function visitVar(Node $node): UnionType
{
// $$var or ${...} (whose idea was that anyway?)
$name_node = $node->children['name'];
if (($name_node instanceof Node)) {
Method analyzeProp
has 98 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function analyzeProp(Node $node, bool $is_static): UnionType
{
// Either expr(instance) or class(static) is set
$expr_node = $node->children['expr'] ?? null;
try {
Method visitConditional
has 95 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function visitConditional(Node $node): UnionType
{
$cond_node = $node->children['cond'];
$cond_truthiness = self::checkCondUnconditionalTruthiness($cond_node);
// For the shorthand $a ?: $b, the cond node will be the truthy value.
Method unionTypeFromClassNode
has 95 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function unionTypeFromClassNode(
CodeBase $code_base,
Context $context,
$node
): UnionType {
Function analyzeProp
has a Cognitive Complexity of 26 (exceeds 5 allowed). Consider refactoring. Open
private function analyzeProp(Node $node, bool $is_static): UnionType
{
// Either expr(instance) or class(static) is set
$expr_node = $node->children['expr'] ?? null;
try {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method visitMethodCall
has 88 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function visitMethodCall(Node $node): UnionType
{
$method_name = $node->children['method'] ?? '';
// Give up on any complicated nonsense where the
Method methodFQSENListFromParts
has 85 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function methodFQSENListFromParts($class_or_expr, $method_name): array
{
$code_base = $this->code_base;
$context = $this->context;
The class UnionTypeVisitor has 87 non-getter- and setter-methods. Consider refactoring UnionTypeVisitor to keep number of methods under 25. Open
class UnionTypeVisitor extends AnalysisVisitor
{
/**
* If an dynamic unpacked array has more elements than this, then give up on building up the union type
*/
- Read upRead up
- Exclude checks
TooManyMethods
Since: 0.1
A class with too many methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.
By default it ignores methods starting with 'get' or 'set'.
The default was changed from 10 to 25 in PHPMD 2.3.
Example
Source https://phpmd.org/rules/codesize.html#toomanymethods
The class UnionTypeVisitor has 62 public methods. Consider refactoring UnionTypeVisitor to keep number of public methods under 10. Open
class UnionTypeVisitor extends AnalysisVisitor
{
/**
* If an dynamic unpacked array has more elements than this, then give up on building up the union type
*/
- Read upRead up
- Exclude checks
TooManyPublicMethods
Since: 0.1
A class with too many public methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.
By default it ignores methods starting with 'get' or 'set'.
Example
Source https://phpmd.org/rules/codesize.html#toomanypublicmethods
The class UnionTypeVisitor has an overall complexity of 625 which is very high. The configured complexity threshold is 50. Open
class UnionTypeVisitor extends AnalysisVisitor
{
/**
* If an dynamic unpacked array has more elements than this, then give up on building up the union type
*/
- Exclude checks
Function unionTypeFromClassNode
has a Cognitive Complexity of 23 (exceeds 5 allowed). Consider refactoring. Open
public static function unionTypeFromClassNode(
CodeBase $code_base,
Context $context,
$node
): UnionType {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function methodFQSENListFromObjectAndMethodName
has a Cognitive Complexity of 23 (exceeds 5 allowed). Consider refactoring. Open
private function methodFQSENListFromObjectAndMethodName($class_or_expr, string $method_name): array
{
$code_base = $this->code_base;
$context = $this->context;
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method visitArray
has 81 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function visitArray(Node $node): UnionType
{
$children = $node->children;
if (\count($children) > 0) {
$key_set = $this->getEquivalentArraySet($node);
Function visitMagicConst
has a Cognitive Complexity of 22 (exceeds 5 allowed). Consider refactoring. Open
public function visitMagicConst(Node $node): UnionType
{
$flags = $node->flags;
switch ($flags) {
case ast\flags\MAGIC_CLASS:
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function createArrayShapeType
has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring. Open
private function createArrayShapeType(array $key_set): ArrayShapeType
{
$field_types = [];
foreach ($key_set as $key => $child) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function visitTypeUnion
has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring. Open
public function visitTypeUnion(Node $node): UnionType
{
// TODO: Validate that there aren't any duplicates
if (\count($node->children) === 1) {
// Might be possible due to the polyfill in the future.
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getEquivalentArraySet
has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring. Open
private function getEquivalentArraySet(Node $node): ?array
{
$elements = [];
$context_node = null;
foreach ($node->children as $child_node) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method visitMagicConst
has 70 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function visitMagicConst(Node $node): UnionType
{
$flags = $node->flags;
switch ($flags) {
case ast\flags\MAGIC_CLASS:
Function functionLikeFQSENListFromNode
has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring. Open
private function functionLikeFQSENListFromNode($node): array
{
$orig_node = $node;
if ($node instanceof Node) {
$node = (new ContextNode($this->code_base, $this->context, $node))->getEquivalentPHPValue();
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method resolveArrayShapeElementTypes
has 66 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function resolveArrayShapeElementTypes(Node $node, UnionType $union_type): ?UnionType
{
$dim_node = $node->children['dim'];
$dim_value = $dim_node instanceof Node ? (new ContextNode($this->code_base, $this->context, $dim_node))->getEquivalentPHPScalarValue() : $dim_node;
// TODO: detect and warn about null
Method resolveArrayShapeElementTypesForOffset
has 65 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function resolveArrayShapeElementTypesForOffset(UnionType $union_type, $dim_value, bool $is_computing_real_type_set = false)
{
/**
* @var bool $has_non_array_shape_type this will be true if there are types that support array access
* but have unknown array shapes in $union_type
Method methodFQSENListFromObjectAndMethodName
has 65 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function methodFQSENListFromObjectAndMethodName($class_or_expr, string $method_name): array
{
$code_base = $this->code_base;
$context = $this->context;
Function arrayTypeFromRealTypeBuilder
has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring. Open
private function arrayTypeFromRealTypeBuilder(?UnionTypeBuilder $builder, Node $node, bool $has_key): array
{
// Here, we only check for the real type being an integer.
// Unknown strings such as '0' will cast to integers when used as array keys,
// and if we knew all of the array keys were literals we would have generated an array shape instead.
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function checkIsValidStringOffset
has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring. Open
private function checkIsValidStringOffset(UnionType $union_type, Node $node, UnionType $dim_type): void
{
$max_len = -1;
foreach ($union_type->getRealTypeSet() as $type) {
if ($type instanceof StringType) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function functionLikeListFromNodeAndContext
has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring. Open
public static function functionLikeListFromNodeAndContext(CodeBase $code_base, Context $context, $node, bool $log_error): array
{
try {
$function_fqsens = (new UnionTypeVisitor($code_base, $context, true))->functionLikeFQSENListFromNode($node);
} catch (FQSENException $e) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method visitClassNameNode
has 54 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function visitClassNameNode(Node $node): UnionType
{
$kind = $node->kind;
// Anonymous class of form `new class { ... }`
if ($kind === \ast\AST_CLASS
Method classTypesForNonName
has 54 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function classTypesForNonName(Node $node): UnionType
{
$node_type = UnionTypeVisitor::unionTypeFromNode(
$this->code_base,
$this->context,
Method functionLikeFQSENListFromNode
has 53 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function functionLikeFQSENListFromNode($node): array
{
$orig_node = $node;
if ($node instanceof Node) {
$node = (new ContextNode($this->code_base, $this->context, $node))->getEquivalentPHPValue();
Method visitNew
has 49 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function visitNew(Node $node): UnionType
{
static $object_type;
if ($object_type === null) {
$object_type = ObjectType::instance(false);
Function checkInvalidUnpackKeyType
has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring. Open
private function checkInvalidUnpackKeyType(Node $node, UnionType $union_type, bool $is_array_spread): void
{
$is_invalid_because_associative = false;
if (!$is_array_spread) {
foreach ($union_type->getTypeSet() as $type) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method visitClassName
has 47 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function visitClassName(Node $node): UnionType
{
$class_node = $node->children['class'];
try {
$class_list = (new ContextNode(
Method checkIsValidStringOffset
has 45 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function checkIsValidStringOffset(UnionType $union_type, Node $node, UnionType $dim_type): void
{
$max_len = -1;
foreach ($union_type->getRealTypeSet() as $type) {
if ($type instanceof StringType) {
Method visitUnaryOp
has 45 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function visitUnaryOp(Node $node): UnionType
{
$result = self::unionTypeFromNode(
$this->code_base,
$this->context,
Method lookupClassOfCallableByName
has 44 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function lookupClassOfCallableByName(string $class_name): ?FullyQualifiedClassName
{
switch (\strtolower($class_name)) {
case 'self':
case 'static':
Function fromTypeInSignature
has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring. Open
public function fromTypeInSignature(Node $node): UnionType
{
$is_nullable = $node->kind === ast\AST_NULLABLE_TYPE;
if ($is_nullable) {
$node = $node->children['type'];
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getPackedArrayFieldTypes
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
private function getPackedArrayFieldTypes($expr): ?array
{
if (!$expr instanceof Node) {
// TODO: Warn if non-array
return null;
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method functionLikeListFromNodeAndContext
has 39 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function functionLikeListFromNodeAndContext(CodeBase $code_base, Context $context, $node, bool $log_error): array
{
try {
$function_fqsens = (new UnionTypeVisitor($code_base, $context, true))->functionLikeFQSENListFromNode($node);
} catch (FQSENException $e) {
Method classListFromClassNameNode
has 37 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function classListFromClassNameNode(CodeBase $code_base, Context $context, $node): array
{
$results = [];
$strings = UnionTypeVisitor::unionTypeFromNode($code_base, $context, $node)->asStringScalarValues();
foreach ($strings as $string) {
Method unionTypeFromNode
has 36 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function unionTypeFromNode(
CodeBase $code_base,
Context $context,
$node,
bool $should_catch_issue_exception = true
Function couldRealTypesHaveKey
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
private static function couldRealTypesHaveKey(array $real_type_set, $dim_value): bool
{
foreach ($real_type_set as $type) {
if ($type instanceof ArrayShapeType) {
if (\array_key_exists($dim_value, $type->getFieldTypes())) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method createArrayShapeType
has 35 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function createArrayShapeType(array $key_set): ArrayShapeType
{
$field_types = [];
foreach ($key_set as $key => $child) {
Method visitTypeUnion
has 34 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function visitTypeUnion(Node $node): UnionType
{
// TODO: Validate that there aren't any duplicates
if (\count($node->children) === 1) {
// Might be possible due to the polyfill in the future.
Method arrayTypeFromRealTypeBuilder
has 33 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function arrayTypeFromRealTypeBuilder(?UnionTypeBuilder $builder, Node $node, bool $has_key): array
{
// Here, we only check for the real type being an integer.
// Unknown strings such as '0' will cast to integers when used as array keys,
// and if we knew all of the array keys were literals we would have generated an array shape instead.
Method getEquivalentArraySet
has 32 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function getEquivalentArraySet(Node $node): ?array
{
$elements = [];
$context_node = null;
foreach ($node->children as $child_node) {
Method visitType
has 32 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function visitType(Node $node): UnionType
{
switch ($node->flags) {
case \ast\flags\TYPE_ARRAY:
return ArrayType::instance(false)->asRealUnionType();
Function visitClassName
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
public function visitClassName(Node $node): UnionType
{
$class_node = $node->children['class'];
try {
$class_list = (new ContextNode(
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method visitCast
has 30 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function visitCast(Node $node): UnionType
{
// This calls unionTypeFromNode to trigger any warnings
// TODO: Check if the cast would throw an error at runtime, based on the type (e.g. casting object to string/int)
Method visitName
has 30 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function visitName(Node $node): UnionType
{
$name = $node->children['name'];
try {
if ($node->flags & \ast\flags\NAME_NOT_FQ) {
Method analyzeUnpack
has 30 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function analyzeUnpack(Node $node, bool $is_array_spread): UnionType
{
$union_type = self::unionTypeFromNode(
$this->code_base,
$this->context,
The class UnionTypeVisitor has 62 public methods and attributes. Consider reducing the number of public items to less than 45. Open
class UnionTypeVisitor extends AnalysisVisitor
{
/**
* If an dynamic unpacked array has more elements than this, then give up on building up the union type
*/
- Read upRead up
- Exclude checks
ExcessivePublicCount
Since: 0.1
A large number of public methods and attributes declared in a class can indicate the class may need to be broken up as increased effort will be required to thoroughly test it.
Example
public class Foo {
public $value;
public $something;
public $var;
// [... more more public attributes ...]
public function doWork() {}
public function doMoreWork() {}
public function doWorkAgain() {}
// [... more more public methods ...]
}
Source https://phpmd.org/rules/codesize.html#excessivepubliccount
Method fromTypeInSignature
has 29 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function fromTypeInSignature(Node $node): UnionType
{
$is_nullable = $node->kind === ast\AST_NULLABLE_TYPE;
if ($is_nullable) {
$node = $node->children['type'];
Method classListFromNode
has 28 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function classListFromNode(Node $node): \Generator
{
// Get the types associated with the node
$union_type = self::unionTypeFromNode(
$this->code_base,
Function typeAfterCastToObject
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
private static function typeAfterCastToObject(UnionType $expr_type): UnionType
{
static $stdclass;
if ($stdclass === null) {
$stdclass = Type::fromFullyQualifiedString('\stdClass');
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function classListFromClassNameNode
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
public static function classListFromClassNameNode(CodeBase $code_base, Context $context, $node): array
{
$results = [];
$strings = UnionTypeVisitor::unionTypeFromNode($code_base, $context, $node)->asStringScalarValues();
foreach ($strings as $string) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method typeAfterCastToObject
has 27 lines of code (exceeds 25 allowed). Consider refactoring. Open
private static function typeAfterCastToObject(UnionType $expr_type): UnionType
{
static $stdclass;
if ($stdclass === null) {
$stdclass = Type::fromFullyQualifiedString('\stdClass');
Method checkInvalidUnpackKeyType
has 26 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function checkInvalidUnpackKeyType(Node $node, UnionType $union_type, bool $is_array_spread): void
{
$is_invalid_because_associative = false;
if (!$is_array_spread) {
foreach ($union_type->getTypeSet() as $type) {
Function visitClassNameNode
has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring. Open
private function visitClassNameNode(Node $node): UnionType
{
$kind = $node->kind;
// Anonymous class of form `new class { ... }`
if ($kind === \ast\AST_CLASS
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function unionTypeFromLiteralOrConstant
has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring. Open
public static function unionTypeFromLiteralOrConstant(CodeBase $code_base, Context $context, $node): ?UnionType
{
if ($node instanceof Node) {
// TODO: There are a lot more types of expressions that have known union types that this doesn't handle.
// Maybe callers should call something else if this fails (e.g. it's useful for them to know if an expression becomes a string)
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Avoid deeply nested control flow statements. Open
if ($dim_type->containsNullable() && $dim_type->nonNullableClone()->canCastToUnionType($expected_key_type)) {
$issue_type = Issue::TypeMismatchDimFetchNullable;
}
Avoid deeply nested control flow statements. Open
if ($this->should_catch_issue_exception) {
$this->emitIssue(
$issue_type,
$node->lineno,
(string)$union_type,
Method warnAboutInvalidUnaryOp
has 5 arguments (exceeds 4 allowed). Consider refactoring. Open
Node $node,
Closure $is_valid_type,
UnionType $type,
string $operator,
string $issue_type
Function visitCall
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public function visitCall(Node $node): UnionType
{
$expression = $node->children['expr'];
$function_list_generator = (new ContextNode(
$this->code_base,
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function visitClassConst
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public function visitClassConst(Node $node): UnionType
{
try {
$constant = (new ContextNode(
$this->code_base,
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function unionTypeFromNode
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public static function unionTypeFromNode(
CodeBase $code_base,
Context $context,
$node,
bool $should_catch_issue_exception = true
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Avoid too many return
statements within this method. Open
return UnionTypeVisitor::unionTypeFromNode(
$this->code_base,
$this->context,
$arg_node
);
Avoid too many return
statements within this method. Open
return $object_type->asRealUnionType();
Avoid too many return
statements within this method. Open
return $resulting_element_type;
Avoid too many return
statements within this method. Open
return ObjectType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return NullType::instance(false)->asRealUnionType()->withIsDefinitelyUndefined();
Avoid too many return
statements within this method. Open
return Type::fromObject($node)->asRealUnionType();
Avoid too many return
statements within this method. Open
return NullType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return Variable::getUnionTypeOfHardcodedGlobalVariableWithName($variable_name);
Avoid too many return
statements within this method. Open
return IntType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return null;
Avoid too many return
statements within this method. Open
return (bool)$cond;
Avoid too many return
statements within this method. Open
return $union_type;
Avoid too many return
statements within this method. Open
return NullType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return $element_types;
Avoid too many return
statements within this method. Open
return NullType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return $parent_type_option->get()->asRealUnionType();
Avoid too many return
statements within this method. Open
return $field_types;
Avoid too many return
statements within this method. Open
return $this->typeAfterCastToObject($expr_type);
Avoid too many return
statements within this method. Open
return $resulting_element_type->withRealTypeSet(
\is_object($resulting_real_element_type) ? $resulting_real_element_type->getRealTypeSet() : []
);
Avoid too many return
statements within this method. Open
return UnionType::empty();
Avoid too many return
statements within this method. Open
return UnionType::of($types, $types);
Avoid too many return
statements within this method. Open
return self::literalIntUnionType($node->lineno);
Avoid too many return
statements within this method. Open
return IterableType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return StringType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return Type::fromType($type, $template_type_list);
Avoid too many return
statements within this method. Open
return $generic_types;
Avoid too many return
statements within this method. Open
return NullType::instance(false)->asPHPDocUnionType();
Avoid too many return
statements within this method. Open
return $result;
Avoid too many return
statements within this method. Open
return null;
Avoid too many return
statements within this method. Open
return $result;
Avoid too many return
statements within this method. Open
return;
Avoid too many return
statements within this method. Open
return ObjectType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return $override_union_type;
Avoid too many return
statements within this method. Open
return [];
Avoid too many return
statements within this method. Open
return $union_type;
Avoid too many return
statements within this method. Open
return self::literalStringUnionType(Config::projectPath($this->context->getFile()));
Avoid too many return
statements within this method. Open
return NullType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return ArrayShapeType::empty(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return NullType::instance(false)->asPHPDocUnionType();
Avoid too many return
statements within this method. Open
return $type->withIsNullable(true);
Avoid too many return
statements within this method. Open
return $result->applyUnaryBitwiseNotOperator();
Avoid too many return
statements within this method. Open
return UnionType::empty();
Avoid too many return
statements within this method. Open
return $class->getParentClassFQSEN(); // may or may not exist.
Avoid too many return
statements within this method. Open
return UnionType::empty();
Avoid too many return
statements within this method. Open
return ($type->isValidNumericOperand() && $type->isValidBitwiseOperand()) || $type instanceof StringType;
Avoid too many return
statements within this method. Open
return $this->context->getClassFQSEN()->asType()->asRealUnionType();
Avoid too many return
statements within this method. Open
return [];
Avoid too many return
statements within this method. Open
return self::literalStringUnionType(\ltrim($this->context->getNamespace(), '\\'));
Avoid too many return
statements within this method. Open
return UnionType::empty();
Avoid too many return
statements within this method. Open
return FalseType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return StringType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return $generic_types;
Avoid too many return
statements within this method. Open
return $result->applyUnaryPlusOperator();
Avoid too many return
statements within this method. Open
return $result;
Avoid too many return
statements within this method. Open
return UnionType::empty();
Avoid too many return
statements within this method. Open
return StringType::instance(false)->asPHPDocUnionType();
Avoid too many return
statements within this method. Open
return StaticType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return $true_type;
Avoid too many return
statements within this method. Open
return null;
Avoid too many return
statements within this method. Open
return $closure_types;
Avoid too many return
statements within this method. Open
return ObjectType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return $false_type;
Avoid too many return
statements within this method. Open
return $element_types;
Avoid too many return
statements within this method. Open
return [$method->getFQSEN()];
Avoid too many return
statements within this method. Open
return $int_or_string_type;
Avoid too many return
statements within this method. Open
return UnionType::empty();
Avoid too many return
statements within this method. Open
return $expr_type;
Avoid too many return
statements within this method. Open
return;
Avoid too many return
statements within this method. Open
return null;
Avoid too many return
statements within this method. Open
return UnionType::empty();
Avoid too many return
statements within this method. Open
return ArrayType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return FullyQualifiedClassName::fromFullyQualifiedString($class_name);
Avoid too many return
statements within this method. Open
return [];
Avoid too many return
statements within this method. Open
return [];
Avoid too many return
statements within this method. Open
return MixedType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return $expr_type->withRealType(ObjectType::instance(false));
Avoid too many return
statements within this method. Open
return ObjectType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return [];
Avoid too many return
statements within this method. Open
return self::literalStringUnionType('');
Avoid too many return
statements within this method. Open
return VoidType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return UnionType::of($type_set, $real_type_set);
Avoid too many return
statements within this method. Open
return $element_types;
Avoid too many return
statements within this method. Open
return $class->getMethodByName($this->code_base, '__get')->getUnionType();
Avoid too many return
statements within this method. Open
return self::literalStringUnionType(\ltrim($this->context->getClassFQSEN()->__toString(), '\\'));
Avoid too many return
statements within this method. Open
return $resulting_element_type;
Avoid too many return
statements within this method. Open
return NullType::instance(false)->asRealUnionType();
Avoid too many return
statements within this method. Open
return $this->methodFQSENListFromParts($node[0], $node[1]);
Function visitInstanceOf
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public function visitInstanceOf(Node $node): UnionType
{
$code_base = $this->code_base;
$context = $this->context;
// Check to make sure the left side is valid
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function visitNew
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public function visitNew(Node $node): UnionType
{
static $object_type;
if ($object_type === null) {
$object_type = ObjectType::instance(false);
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function arrayKeyUnionTypeOfUnionType
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public static function arrayKeyUnionTypeOfUnionType(UnionType $union_type): ?UnionType
{
if ($union_type->isEmpty()) {
return null;
}
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function analyzeUnpack
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
private function analyzeUnpack(Node $node, bool $is_array_spread): UnionType
{
$union_type = self::unionTypeFromNode(
$this->code_base,
$this->context,
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
The method visitArray() has an NPath complexity of 1345. The configured NPath complexity threshold is 200. Open
public function visitArray(Node $node): UnionType
{
$children = $node->children;
if (\count($children) > 0) {
$key_set = $this->getEquivalentArraySet($node);
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method functionLikeFQSENListFromNode() has an NPath complexity of 420. The configured NPath complexity threshold is 200. Open
private function functionLikeFQSENListFromNode($node): array
{
$orig_node = $node;
if ($node instanceof Node) {
$node = (new ContextNode($this->code_base, $this->context, $node))->getEquivalentPHPValue();
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method methodFQSENListFromParts() has an NPath complexity of 672. The configured NPath complexity threshold is 200. Open
private function methodFQSENListFromParts($class_or_expr, $method_name): array
{
$code_base = $this->code_base;
$context = $this->context;
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method visitVar() has 121 lines of code. Current threshold is set to 100. Avoid really long methods. Open
public function visitVar(Node $node): UnionType
{
// $$var or ${...} (whose idea was that anyway?)
$name_node = $node->children['name'];
if (($name_node instanceof Node)) {
- Exclude checks
The method analyzeProp() has an NPath complexity of 1926. The configured NPath complexity threshold is 200. Open
private function analyzeProp(Node $node, bool $is_static): UnionType
{
// Either expr(instance) or class(static) is set
$expr_node = $node->children['expr'] ?? null;
try {
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method unionTypeFromClassNode() has an NPath complexity of 1020. The configured NPath complexity threshold is 200. Open
public static function unionTypeFromClassNode(
CodeBase $code_base,
Context $context,
$node
): UnionType {
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method visitConditional() has an NPath complexity of 18306. The configured NPath complexity threshold is 200. Open
public function visitConditional(Node $node): UnionType
{
$cond_node = $node->children['cond'];
$cond_truthiness = self::checkCondUnconditionalTruthiness($cond_node);
// For the shorthand $a ?: $b, the cond node will be the truthy value.
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method visitConditional() has 142 lines of code. Current threshold is set to 100. Avoid really long methods. Open
public function visitConditional(Node $node): UnionType
{
$cond_node = $node->children['cond'];
$cond_truthiness = self::checkCondUnconditionalTruthiness($cond_node);
// For the shorthand $a ?: $b, the cond node will be the truthy value.
- Exclude checks
The method resolveArrayShapeElementTypes() has an NPath complexity of 3752. The configured NPath complexity threshold is 200. Open
private function resolveArrayShapeElementTypes(Node $node, UnionType $union_type): ?UnionType
{
$dim_node = $node->children['dim'];
$dim_value = $dim_node instanceof Node ? (new ContextNode($this->code_base, $this->context, $dim_node))->getEquivalentPHPScalarValue() : $dim_node;
// TODO: detect and warn about null
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method visitVar() has an NPath complexity of 139536. The configured NPath complexity threshold is 200. Open
public function visitVar(Node $node): UnionType
{
// $$var or ${...} (whose idea was that anyway?)
$name_node = $node->children['name'];
if (($name_node instanceof Node)) {
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method analyzeProp() has 119 lines of code. Current threshold is set to 100. Avoid really long methods. Open
private function analyzeProp(Node $node, bool $is_static): UnionType
{
// Either expr(instance) or class(static) is set
$expr_node = $node->children['expr'] ?? null;
try {
- Exclude checks
The method methodFQSENListFromObjectAndMethodName() has an NPath complexity of 666. The configured NPath complexity threshold is 200. Open
private function methodFQSENListFromObjectAndMethodName($class_or_expr, string $method_name): array
{
$code_base = $this->code_base;
$context = $this->context;
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method unionTypeFromClassNode() has 123 lines of code. Current threshold is set to 100. Avoid really long methods. Open
public static function unionTypeFromClassNode(
CodeBase $code_base,
Context $context,
$node
): UnionType {
- Exclude checks
The class UnionTypeVisitor has 4028 lines of code. Current threshold is 1000. Avoid really long classes. Open
class UnionTypeVisitor extends AnalysisVisitor
{
/**
* If an dynamic unpacked array has more elements than this, then give up on building up the union type
*/
- Exclude checks
The method visitNew() has an NPath complexity of 256. The configured NPath complexity threshold is 200. Open
public function visitNew(Node $node): UnionType
{
static $object_type;
if ($object_type === null) {
$object_type = ObjectType::instance(false);
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method visitDim() has 214 lines of code. Current threshold is set to 100. Avoid really long methods. Open
public function visitDim(Node $node, bool $treat_undef_as_nullable = false): UnionType
{
$union_type = self::unionTypeFromNode(
$this->code_base,
$this->context,
- Exclude checks
The method resolveArrayShapeElementTypesForOffset() has an NPath complexity of 14112. The configured NPath complexity threshold is 200. Open
public static function resolveArrayShapeElementTypesForOffset(UnionType $union_type, $dim_value, bool $is_computing_real_type_set = false)
{
/**
* @var bool $has_non_array_shape_type this will be true if there are types that support array access
* but have unknown array shapes in $union_type
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method visitDim() has an NPath complexity of 19976880. The configured NPath complexity threshold is 200. Open
public function visitDim(Node $node, bool $treat_undef_as_nullable = false): UnionType
{
$union_type = self::unionTypeFromNode(
$this->code_base,
$this->context,
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method visitMethodCall() has an NPath complexity of 4374. The configured NPath complexity threshold is 200. Open
public function visitMethodCall(Node $node): UnionType
{
$method_name = $node->children['method'] ?? '';
// Give up on any complicated nonsense where the
- Read upRead up
- Exclude checks
NPathComplexity
Since: 0.1
The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.
Example
class Foo {
function bar() {
// lots of complicated code
}
}
Source https://phpmd.org/rules/codesize.html#npathcomplexity
The method visitMethodCall() has 116 lines of code. Current threshold is set to 100. Avoid really long methods. Open
public function visitMethodCall(Node $node): UnionType
{
$method_name = $node->children['method'] ?? '';
// Give up on any complicated nonsense where the
- Exclude checks
The method visitMagicConst() has a Cyclomatic Complexity of 19. The configured cyclomatic complexity threshold is 10. Open
public function visitMagicConst(Node $node): UnionType
{
$flags = $node->flags;
switch ($flags) {
case ast\flags\MAGIC_CLASS:
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method methodFQSENListFromParts() has a Cyclomatic Complexity of 17. The configured cyclomatic complexity threshold is 10. Open
private function methodFQSENListFromParts($class_or_expr, $method_name): array
{
$code_base = $this->code_base;
$context = $this->context;
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method methodFQSENListFromObjectAndMethodName() has a Cyclomatic Complexity of 13. The configured cyclomatic complexity threshold is 10. Open
private function methodFQSENListFromObjectAndMethodName($class_or_expr, string $method_name): array
{
$code_base = $this->code_base;
$context = $this->context;
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method visitType() has a Cyclomatic Complexity of 14. The configured cyclomatic complexity threshold is 10. Open
public function visitType(Node $node): UnionType
{
switch ($node->flags) {
case \ast\flags\TYPE_ARRAY:
return ArrayType::instance(false)->asRealUnionType();
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method analyzeProp() has a Cyclomatic Complexity of 26. The configured cyclomatic complexity threshold is 10. Open
private function analyzeProp(Node $node, bool $is_static): UnionType
{
// Either expr(instance) or class(static) is set
$expr_node = $node->children['expr'] ?? null;
try {
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method resolveArrayShapeElementTypesForOffset() has a Cyclomatic Complexity of 28. The configured cyclomatic complexity threshold is 10. Open
public static function resolveArrayShapeElementTypesForOffset(UnionType $union_type, $dim_value, bool $is_computing_real_type_set = false)
{
/**
* @var bool $has_non_array_shape_type this will be true if there are types that support array access
* but have unknown array shapes in $union_type
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method visitClassName() has a Cyclomatic Complexity of 11. The configured cyclomatic complexity threshold is 10. Open
public function visitClassName(Node $node): UnionType
{
$class_node = $node->children['class'];
try {
$class_list = (new ContextNode(
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method visitDim() has a Cyclomatic Complexity of 43. The configured cyclomatic complexity threshold is 10. Open
public function visitDim(Node $node, bool $treat_undef_as_nullable = false): UnionType
{
$union_type = self::unionTypeFromNode(
$this->code_base,
$this->context,
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method resolveArrayShapeElementTypes() has a Cyclomatic Complexity of 19. The configured cyclomatic complexity threshold is 10. Open
private function resolveArrayShapeElementTypes(Node $node, UnionType $union_type): ?UnionType
{
$dim_node = $node->children['dim'];
$dim_value = $dim_node instanceof Node ? (new ContextNode($this->code_base, $this->context, $dim_node))->getEquivalentPHPScalarValue() : $dim_node;
// TODO: detect and warn about null
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method checkInvalidUnpackKeyType() has a Cyclomatic Complexity of 10. The configured cyclomatic complexity threshold is 10. Open
private function checkInvalidUnpackKeyType(Node $node, UnionType $union_type, bool $is_array_spread): void
{
$is_invalid_because_associative = false;
if (!$is_array_spread) {
foreach ($union_type->getTypeSet() as $type) {
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method functionLikeFQSENListFromNode() has a Cyclomatic Complexity of 13. The configured cyclomatic complexity threshold is 10. Open
private function functionLikeFQSENListFromNode($node): array
{
$orig_node = $node;
if ($node instanceof Node) {
$node = (new ContextNode($this->code_base, $this->context, $node))->getEquivalentPHPValue();
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method visitArray() has a Cyclomatic Complexity of 16. The configured cyclomatic complexity threshold is 10. Open
public function visitArray(Node $node): UnionType
{
$children = $node->children;
if (\count($children) > 0) {
$key_set = $this->getEquivalentArraySet($node);
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method fromTypeInSignature() has a Cyclomatic Complexity of 10. The configured cyclomatic complexity threshold is 10. Open
public function fromTypeInSignature(Node $node): UnionType
{
$is_nullable = $node->kind === ast\AST_NULLABLE_TYPE;
if ($is_nullable) {
$node = $node->children['type'];
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method arrayTypeFromRealTypeBuilder() has a Cyclomatic Complexity of 12. The configured cyclomatic complexity threshold is 10. Open
private function arrayTypeFromRealTypeBuilder(?UnionTypeBuilder $builder, Node $node, bool $has_key): array
{
// Here, we only check for the real type being an integer.
// Unknown strings such as '0' will cast to integers when used as array keys,
// and if we knew all of the array keys were literals we would have generated an array shape instead.
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method visitTypeUnion() has a Cyclomatic Complexity of 11. The configured cyclomatic complexity threshold is 10. Open
public function visitTypeUnion(Node $node): UnionType
{
// TODO: Validate that there aren't any duplicates
if (\count($node->children) === 1) {
// Might be possible due to the polyfill in the future.
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method visitConditional() has a Cyclomatic Complexity of 18. The configured cyclomatic complexity threshold is 10. Open
public function visitConditional(Node $node): UnionType
{
$cond_node = $node->children['cond'];
$cond_truthiness = self::checkCondUnconditionalTruthiness($cond_node);
// For the shorthand $a ?: $b, the cond node will be the truthy value.
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method getPackedArrayFieldTypes() has a Cyclomatic Complexity of 11. The configured cyclomatic complexity threshold is 10. Open
private function getPackedArrayFieldTypes($expr): ?array
{
if (!$expr instanceof Node) {
// TODO: Warn if non-array
return null;
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method classTypesForNonName() has a Cyclomatic Complexity of 16. The configured cyclomatic complexity threshold is 10. Open
private function classTypesForNonName(Node $node): UnionType
{
$node_type = UnionTypeVisitor::unionTypeFromNode(
$this->code_base,
$this->context,
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method unionTypeFromClassNode() has a Cyclomatic Complexity of 15. The configured cyclomatic complexity threshold is 10. Open
public static function unionTypeFromClassNode(
CodeBase $code_base,
Context $context,
$node
): UnionType {
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method checkIsValidStringOffset() has a Cyclomatic Complexity of 11. The configured cyclomatic complexity threshold is 10. Open
private function checkIsValidStringOffset(UnionType $union_type, Node $node, UnionType $dim_type): void
{
$max_len = -1;
foreach ($union_type->getRealTypeSet() as $type) {
if ($type instanceof StringType) {
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method visitMethodCall() has a Cyclomatic Complexity of 21. The configured cyclomatic complexity threshold is 10. Open
public function visitMethodCall(Node $node): UnionType
{
$method_name = $node->children['method'] ?? '';
// Give up on any complicated nonsense where the
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The method visitVar() has a Cyclomatic Complexity of 27. The configured cyclomatic complexity threshold is 10. Open
public function visitVar(Node $node): UnionType
{
// $$var or ${...} (whose idea was that anyway?)
$name_node = $node->children['name'];
if (($name_node instanceof Node)) {
- Read upRead up
- Exclude checks
CyclomaticComplexity
Since: 0.1
Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.
Example
// Cyclomatic Complexity = 11
class Foo {
1 public function example() {
2 if ($a == $b) {
3 if ($a1 == $b1) {
fiddle();
4 } elseif ($a2 == $b2) {
fiddle();
} else {
fiddle();
}
5 } elseif ($c == $d) {
6 while ($c == $d) {
fiddle();
}
7 } elseif ($e == $f) {
8 for ($n = 0; $n < $h; $n++) {
fiddle();
}
} else {
switch ($z) {
9 case 1:
fiddle();
break;
10 case 2:
fiddle();
break;
11 case 3:
fiddle();
break;
default:
fiddle();
break;
}
}
}
}
Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity
The class UnionTypeVisitor has a coupling between objects value of 68. Consider to reduce the number of dependencies under 13. Open
class UnionTypeVisitor extends AnalysisVisitor
{
/**
* If an dynamic unpacked array has more elements than this, then give up on building up the union type
*/
- Read upRead up
- Exclude checks
CouplingBetweenObjects
Since: 1.1.0
A class with too many dependencies has negative impacts on several quality aspects of a class. This includes quality criteria like stability, maintainability and understandability
Example
class Foo {
/**
* @var \foo\bar\X
*/
private $x = null;
/**
* @var \foo\bar\Y
*/
private $y = null;
/**
* @var \foo\bar\Z
*/
private $z = null;
public function setFoo(\Foo $foo) {}
public function setBar(\Bar $bar) {}
public function setBaz(\Baz $baz) {}
/**
* @return \SplObjectStorage
* @throws \OutOfRangeException
* @throws \InvalidArgumentException
* @throws \ErrorException
*/
public function process(\Iterator $it) {}
// ...
}
Source https://phpmd.org/rules/design.html#couplingbetweenobjects
Avoid using empty try-catch blocks in visitMethodCall. Open
} catch (RecursionDepthException $_) {
}
- Read upRead up
- Exclude checks
EmptyCatchBlock
Since: 2.7.0
Usually empty try-catch is a bad idea because you are silently swallowing an error condition and then continuing execution. Occasionally this may be the right thing to do, but often it's a sign that a developer saw an exception, didn't know what to do about it, and so used an empty catch to silence the problem.
Example
class Foo {
public function bar()
{
try {
// ...
} catch (Exception $e) {} // empty catch block
}
}
Source https://phpmd.org/rules/design.html#emptycatchblock
Identical blocks of code found in 2 locations. Consider refactoring. Open
public function visitClosure(Node $node): UnionType
{
// The type of a closure is the fqsen pointing
// at its definition
$closure_fqsen =
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 90.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Avoid variables with short names like $i. Configured minimum length is 3. Open
$i = 0;
- Read upRead up
- Exclude checks
ShortVariable
Since: 0.2
Detects when a field, local, or parameter has a very short name.
Example
class Something {
private $q = 15; // VIOLATION - Field
public static function main( array $as ) { // VIOLATION - Formal
$r = 20 + $this->q; // VIOLATION - Local
for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
$r += $this->q;
}
}
}