Syntax error for "new Collection" in generateConstraints
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3560073. --> Reported by: [jurgenhaas](https://www.drupal.org/user/168924) Related to !1026 >>> <p>[Tracker]<br> <strong>Update Summary: </strong>[One-line status update for stakeholders]<br> <strong>Short Description: </strong>[One-line issue summary for stakeholders]<br> <strong>Check-in Date: </strong>MM/DD/YYYY<br> <em>Metadata is used by the <a href="https://www.drupalstarforge.ai/" title="AI Tracker">AI Tracker.</a> Docs and additional fields <a href="https://www.drupalstarforge.ai/ai-dashboard/docs" title="AI Issue Tracker Documentation">here</a>.</em><br> [/Tracker]</p> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p>In <code>\Drupal\ai\Service\AiProviderValidator\AiProviderValidator::generateConstraints</code> I found the following code:</p> <div class="codeblock"> <pre><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #007700">return new </span><span style="color: #0000BB">Collection</span><span style="color: #007700">([<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'fields' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$fields </span><span style="color: #007700">+ </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">extraConstraints</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #DD0000">'allowExtraFields' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp; ]);<br></span><span style="color: #0000BB">?&gt;</span></span></pre></div> <p>This is the wrong syntax and fails in Drupal 11.3 due to a more specific constructor of collection than before:</p> <div class="codeblock"> <pre><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br></span><span style="color: #007700">public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">mixed $fields </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">, ?array </span><span style="color: #0000BB">$groups </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">, </span><span style="color: #0000BB">mixed $payload </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">, ?</span><span style="color: #0000BB">bool $allowExtraFields </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">, ?</span><span style="color: #0000BB">bool $allowMissingFields </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">, ?</span><span style="color: #0000BB">string $extraFieldsMessage </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">, ?</span><span style="color: #0000BB">string $missingFieldsMessage </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">)<br></span><span style="color: #0000BB">?&gt;</span></span></pre></div> <p>This is supporting named arguments, and that's what the problematic call was trying to achieve, but the syntax is wrong. It provides just 1 argument which is an array. But what it really wants to achieve is to provide an array for the <code>fields</code> argument, and FALSE for the <code>allowExtraFields</code> argument.</p> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>I presume, the correct version without using named arguments would be this:</p> <div class="codeblock"> <pre><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #007700">return new </span><span style="color: #0000BB">Collection</span><span style="color: #007700">(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$fields </span><span style="color: #007700">+ </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">extraConstraints</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">NULL</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">NULL</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">,<br>&nbsp;&nbsp;&nbsp; );<br></span><span style="color: #0000BB">?&gt;</span></span></pre></div>
issue