{"record":{"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/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Collections/Collection.php#L1253-L1289","documentation":"Thrown by Collection::shift($count) when $count is negative. The method removes and returns the first N items; a negative count has no semantic meaning and is rejected. Zero and positive values are valid (0 returns an empty collection, 1 returns a single item).","triggerScenarios":"Calling $collection->shift(-1) or passing a computed $count that goes negative.","commonSituations":"Count derived from arithmetic (e.g. count - offset) that can go negative; user-supplied pagination input not bounded.","solutions":["Clamp to zero: $collection->shift(max(0, $count)).","Validate $count >= 0 before calling.","Use the default shift() (count = 1) when only one item is needed."],"exampleFix":"// before\n$head = $col->shift($n); // throws if $n < 0\n// after\n$head = $col->shift(max(0, $n));","handlingStrategy":"validation","validationCode":"$count = max(0, (int) $count);\n$result = $collection->shift($count);","typeGuard":"function isNonNegativeCount(int $count): bool { return $count >= 0; }","tryCatchPattern":"try {\n    $head = $col->shift($n);\n} catch (\\InvalidArgumentException $e) {\n    $head = $col->shift(0);\n}","preventionTips":["Clamp dynamic counts with max(0, ...).","Validate computed counts before passing.","Use shift() (default 1) when only one item is needed."],"tags":["collections","validation","laravel"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}