{"id":"85379715c74d186b","repo":"laravel/framework","slug":"number-of-shifted-items-may-not-be-less-than-zero","errorCode":null,"errorMessage":"Number of shifted items may not be less than zero.","messagePattern":"Number of shifted items may not be less than zero\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Collections/Collection.php","lineNumber":1271,"sourceCode":"        if ($position === $keys->count() - 1) {\n            return null;\n        }\n\n        return $this->get($keys->get($position + 1));\n    }\n\n    /**\n     * Get and remove the first N items from the collection.\n     *\n     * @param  int<0, max>  $count\n     * @return ($count is 1 ? TValue|null : static<int, TValue>)\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function shift($count = 1)\n    {\n        if ($count < 0) {\n            throw new InvalidArgumentException('Number of shifted items may not be less than zero.');\n        }\n\n        if ($this->isEmpty()) {\n            return null;\n        }\n\n        if ($count === 0) {\n            return $this->newInstance();\n        }\n\n        if ($count === 1) {\n            return array_shift($this->items);\n        }\n\n        $results = [];\n\n        $collectionCount = $this->count();\n","sourceCodeStart":1253,"sourceCodeEnd":1289,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Collections/Collection.php#L1253-L1289","documentation":"Thrown by Collection::shift($count) when $count < 0. shift() removes and returns the first N items; a negative count has no meaningful semantics, so it is rejected. Zero returns an empty collection, one returns a single value, and a positive int returns a new collection.","triggerScenarios":"Calling $collection->shift(-1) or passing a computed count that goes negative.","commonSituations":"Count derived from subtraction that can underflow (e.g. count() - offset where offset > count); user input not validated; arithmetic on an empty collection's size.","solutions":["Clamp the count: $collection->shift(max(0, $count)).","Validate before calling: if ($count >= 0) { ... }.","Re-check the arithmetic producing the count.","Branch: if the value is negative, treat the input as empty rather than shifting."],"exampleFix":"// before\n$head = $collection->shift($count);\n\n// after\n$head = $collection->shift(max(0, (int) $count));","handlingStrategy":"validation","validationCode":"$count = max(0, (int) $count);\n$head = $collection->shift($count);","typeGuard":"function isValidShiftCount(int $count): bool {\n    return $count >= 0;\n}","tryCatchPattern":"try {\n    $head = $collection->shift($count);\n} catch (\\InvalidArgumentException $e) {\n    $head = $collection->shift(0);\n}","preventionTips":["Clamp count with max(0, $count).","Validate subtraction that can underflow before calling shift().","Branch on negative inputs rather than passing them through."],"tags":["collections","validation","laravel"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}