NoUnnecessaryCollectionCallRule
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')
Unlike upstream, a call is reported only when the query method returns the same result, which needs the whole query in view: a DB::table() query, or a model query whose global scopes, collection and attribute reads are the framework's, built from filters, ordering, eager loads, joins and limits and fetched with get() or all(). Relations, variables, selects, macros and named scopes stay silent.
Constants
| protected REPLACEMENTS |
The method names that can be applied on a Collection, but should be applied on a Builder, with the
query method that returns the same result. Hypervel: upstream also reports last(), pop(), skip(), slice(), diff(), except(), find(), only(), contains(), containsStrict(), firstWhere(), where(), whereStrict() and the aggregates, whose query forms differ: last() and pop() have none, skip() and slice() keep keys, diff(), except(), find() and only() compare keys loosely, and SQL compares, casts and aggregates values differently from PHP. |
| private FILTERS |
The builder methods that only filter the rows a query returns. |
| private ORDERS |
The builder methods that order the rows a query returns. |
| private EAGER_LOADS |
The builder methods that choose the relations a model query eager loads. |
| private JOINS |
The builder methods that join tables, whose columns a model query's default select also fetches. |
| private LIMITS |
The builder methods that limit or offset the rows a query returns. |
| private DELEGATED_METHODS |
The collection methods a recommended method calls, which a custom collection may override. |
| private ATTRIBUTE_METHODS |
The model methods that hydrate a model and read its attributes. A collection reads each fetched model, while Builder::pluck() returns the fetched value of an attribute without a get accessor, cast or date, and otherwise reads it from a model holding that attribute alone. So an override, such as a translated attribute value, can change only the collection's result. |
| private PLUCK_METHODS |
The model methods a collection's pluck() reads attributes through, with data_get(). |
| private KEY_METHODS |
The model methods that name and read a model's key, which a collection's modelKeys() reads from each fetched
model and Builder::modelKeys() names from the query's model. |
Properties
| protected string[] | $shouldHandle |
Methods
Create a new no unnecessary collection call rule instance.
Get the node type the rule checks.
Report collection methods called on a query's results that the query itself could have returned.
Format the error.
Details
at line 213
__construct(ReflectionProvider $reflectionProvider, ModelRuleHelper $modelRuleHelper, ModelHelper $modelHelper, ModelPropertyHelper $modelPropertyHelper, ModelScopeMethodResolver $modelScopeMethodResolver, CollectionHelper $collectionHelper, RuntimeInputs $runtimeInputs, array $onlyMethods, array $excludeMethods)
Create a new no unnecessary collection call rule instance.
at line 248
string
getNodeType()
Get the node type the rule checks.
at line 259
array
processNode(Node $node, Scope $scope)
Report collection methods called on a query's results that the query itself could have returned.
at line 670
protected IdentifierRuleError
formatError(string $methodName, string $replacement)
Format the error.