Fatal error in indexItem() when $result is null due to no-op branch
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3562268. --> Reported by: [tdoornbos](https://www.drupal.org/user/3823121) Related to !9 >>> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p><code>VragenAiBackend::indexItem()</code> performs an error check via <code>$result-&gt;getErrors()</code>. However, the control flow is such that if <code>$event-&gt;shouldIndex()</code> is <code>FALSE</code> and the nested condition <code>!$document-&gt;isNew() &amp;&amp; !empty($document-&gt;getId())</code> is also <code>FALSE</code>, then <code>$result</code> is never defined.</p> <p>The subsequent call:</p> <p><code>$result-&gt;getErrors()-&gt;isNotEmpty()</code></p> <p>then triggers a fatal error because <code>$result</code> is <code>NULL</code>.</p> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>Replace the existing conditional:</p> <p><code>!empty($result-&gt;getErrors()-&gt;isNotEmpty())</code></p> <p>with:</p> <p><code>$result?-&gt;getErrors()-&gt;isNotEmpty()</code></p> <p>The nullsafe operator ensures the error check only runs when a result object was actually returned by a document operation.</p> <h3 id="summary-additional-notes">Additional notes</h3> <ul> <li>This change does not alter the underlying indexing logic, but only prevents an unintended fatal error.</li> <li>As such, this change assumes that the undefined <code>$result</code> results from a valid no-op branch.</li> <li>The change also removes the redundant use of <code>empty()</code> around a boolean result.</li> </ul>
issue