PhpStan finds an error where context goes over interface definition
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3540487. -->
Reported by: [jurgenhaas](https://www.drupal.org/user/168924)
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>The following code worked for a long time:</p>
<div class="codeblock">
<pre><span style="color: #000000"><span style="color: #0000BB"><?php<br> </span><span style="color: #007700">if (isset(</span><span style="color: #0000BB">$original</span><span style="color: #007700">) && !</span><span style="color: #0000BB">$entity</span><span style="color: #007700">-></span><span style="color: #0000BB">isNew</span><span style="color: #007700">()) {<br> </span><span style="color: #FF8000">// Behave the same as \Drupal\Core\Entity\EntityStorageBase::doPreSave.<br> </span><span style="color: #0000BB">$id </span><span style="color: #007700">= </span><span style="color: #0000BB">$entity</span><span style="color: #007700">-></span><span style="color: #0000BB">getOriginalId</span><span style="color: #007700">() ?? </span><span style="color: #0000BB">$entity</span><span style="color: #007700">-></span><span style="color: #0000BB">id</span><span style="color: #007700">();<br> if (</span><span style="color: #0000BB">$id </span><span style="color: #007700">!== </span><span style="color: #0000BB">NULL</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$etm </span><span style="color: #007700">= </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">entityTypeManager </span><span style="color: #007700">?? \</span><span style="color: #0000BB">Drupal</span><span style="color: #007700">::</span><span style="color: #0000BB">entityTypeManager</span><span style="color: #007700">();<br></span><span style="color: #0000BB">?></span></span></pre></div>
<p>But now, PhpStan insists that <code>$id !== NULL</code> is always TRUE. The interface definitions don't tell that, but because we checked for <code>!$entity->isNew()</code> before, which implies that ID can not be NULL anymore.</p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>We've discussed this at length and came to the conclusion that this is an improvement and the redundant if-statement should be removed.</p>
issue