class NoUnnecessaryCollectionCallRule implements Rule

This rule checks for unnecessary heavy operations on the Collection class that could have instead been performed on the Builder class.

For example: User::all()->count() could be simplified to: User::count()

In addition, this code: User::whereStatus('active')->get()->pluck('id') could be simplified to: User::whereStatus('active')->pluck('id')

Constants

protected RISKY_METHODS

The method names that can be applied on a Collection, but should be applied on a Builder.

protected RISKY_PARAM_METHODS

The method names that can be applied on a Collection, but should in some cases be applied on a builder depending on what parameter is passed to the method.

Properties

protected string[] $shouldHandle
protected ReflectionProvider $reflectionProvider
protected ModelPropertyExtension $propertyExtension

Methods

__construct(ReflectionProvider $reflectionProvider, ModelPropertyExtension $propertyExtension, array $onlyMethods, array $excludeMethods)

NoRedundantCollectionCallRule constructor.

string
getNodeType()

No description

array
processNode(Node $node, Scope $scope)

No description

bool
firstArgIsDatabaseColumn(StaticCall|MethodCall $node, Scope $scope)

Determines whether the first argument is a string and references a database column.

bool
callIsQuery(Expr $call, Scope $scope)

Returns whether the method call is a call on a builder instance.

bool
isRiskyMethod(Identifier $name)

Returns whether the method is one of the risky methods.

bool
isRiskyParamMethod(Identifier $name)

Returns whether the method might be a risky method depending on the parameters passed.

bool
isBuilder(Type $type)

Returns whether its argument is some builder instance.

bool
isCalledOnCollection(Expr $expr, Scope $scope)

Returns whether the Expr was not called on a Collection instance.

string
formatError(string $method_name)

Formats the error.

Details

at line 103
__construct(ReflectionProvider $reflectionProvider, ModelPropertyExtension $propertyExtension, array $onlyMethods, array $excludeMethods)

NoRedundantCollectionCallRule constructor.

Parameters

ReflectionProvider $reflectionProvider
ModelPropertyExtension $propertyExtension
array $onlyMethods
array $excludeMethods

at line 139
string getNodeType()

No description

Return Value

string

at line 149
array processNode(Node $node, Scope $scope)

No description

Parameters

Node $node
Scope $scope

Return Value

array

at line 226
protected bool firstArgIsDatabaseColumn(StaticCall|MethodCall $node, Scope $scope)

Determines whether the first argument is a string and references a database column.

Parameters

StaticCall|MethodCall $node
Scope $scope

Return Value

bool

at line 281
protected bool callIsQuery(Expr $call, Scope $scope)

Returns whether the method call is a call on a builder instance.

Parameters

Expr $call
Scope $scope

Return Value

bool

at line 310
protected bool isRiskyMethod(Identifier $name)

Returns whether the method is one of the risky methods.

Parameters

Identifier $name

Return Value

bool

at line 321
protected bool isRiskyParamMethod(Identifier $name)

Returns whether the method might be a risky method depending on the parameters passed.

Parameters

Identifier $name

Return Value

bool

at line 332
protected bool isBuilder(Type $type)

Returns whether its argument is some builder instance.

Parameters

Type $type

Return Value

bool

at line 346
protected bool isCalledOnCollection(Expr $expr, Scope $scope)

Returns whether the Expr was not called on a Collection instance.

Parameters

Expr $expr
Scope $scope

Return Value

bool

at line 359
protected string formatError(string $method_name)

Formats the error.

Parameters

string $method_name

Return Value

string