<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHPStan | Nerdpress.org</title>
	<atom:link href="https://nerdpress.org/tag/phpstan/feed/" rel="self" type="application/rss+xml" />
	<link>https://nerdpress.org</link>
	<description>...dev, tech problems and solutions.</description>
	<lastBuildDate>Sun, 23 Feb 2025 17:14:00 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Typed Arrays with PHPStan types</title>
		<link>https://nerdpress.org/2025/01/04/typed-arrays-with-phpstan-types/</link>
		
		<dc:creator><![CDATA[Ivo Bathke]]></dc:creator>
		<pubDate>Sat, 04 Jan 2025 16:54:36 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPStan]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=3367</guid>

					<description><![CDATA[<p>When dealing with legacy data, you often encounter arrays or associative arrays. These arrays are untyped, which PHPStan, of course, does not accept, resulting in numerous PHPStan errors. PHPStan, by the way, is a static analysis tool for PHP that enforces strict typing and checks for compliance with PHPDoc annotations, ensuring code is robust and &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2025/01/04/typed-arrays-with-phpstan-types/" class="more-link">Continue reading<span class="screen-reader-text"> "Typed Arrays with PHPStan types"</span></a></p>
The post <a href="https://nerdpress.org/2025/01/04/typed-arrays-with-phpstan-types/">Typed Arrays with PHPStan types</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>When dealing with legacy data, you often encounter arrays or associative arrays. These arrays are untyped, which PHPStan, of course, does not accept, resulting in numerous PHPStan errors.</p>



<p><a href="https://phpstan.org/" target="_blank" rel="noopener" title="">PHPStan</a>, by the way, is a static analysis tool for PHP that enforces strict typing and checks for compliance with PHPDoc annotations, ensuring code is robust and maintainable.<br />For any serious project you should use it.</p>



<p>For this code:</p>


<pre class="wp-block-code"><span><code class="hljs language-php">$array = <span class="hljs-keyword">$this</span>-&gt;getUntypedArray();
$res = <span class="hljs-keyword">$this</span>-&gt;funcWithInt($array&#91;<span class="hljs-string">'number'</span>]);
...
<span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">funcWithInt</span><span class="hljs-params">(int $number)</span>:<span class="hljs-title">int</span> </span>{
    <span class="hljs-keyword">return</span> $number++;
}</code></span></pre>


<p>A typical error could be:</p>


<pre class="wp-block-code"><span><code class="hljs language-php">Parameter <span class="hljs-comment">#1 $number of method TestClass::funcWithInt expects int, mixed given.</span></code></span></pre>


<span id="more-3367"></span>



<p>You can try to typecast the value:</p>


<pre class="wp-block-code"><span><code class="hljs language-php">$res = <span class="hljs-keyword">$this</span>-&gt;funcWithInt((int)$array&#91;<span class="hljs-string">'number'</span>]);</code></span></pre>


<p>But PHPStan will not accept this and will yield this error:<br /><br /><code>Cannot cast mixed to int.</code></p>



<p>To type these untyped arrays, you can use <a href="https://phpstan.org/writing-php-code/phpdoc-types" target="_blank" rel="noopener" title="">PHPStan types</a>. These types can be defined in a PHP DocBlock, and PHPStan will use them and the error will be gone.<br />You can also reuse the types throughout the code in this class.</p>



<p>Note that you need to define the PHPStan type in a DocBlock <strong>outside</strong> of the class. <br />If defined inside the class or in a function, it will not be recognized by PHPStan!<br /><br />Example (see also here: <a href="https://phpstan.org/r/4ff8a134-4494-46e1-a026-1d5a741289e7">https://phpstan.org/r/4ff8a134-4494-46e1-a026-1d5a741289e7</a>):</p>


<pre class="wp-block-code"><span><code class="hljs language-xml"><span class="php"><span class="hljs-meta">&lt;?php</span> <span class="hljs-keyword">declare</span>(strict_types = <span class="hljs-number">1</span>);

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@phpstan</span>-type Test array{
 *     string: string,
 *     number: int,
 *     array?: array{title: string}
 * }
 */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExampleClass</span>
</span>{
    <span class="hljs-comment">/**
     * <span class="hljs-doctag">@return</span> Test
     */</span>
    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getArray</span><span class="hljs-params">()</span>: <span class="hljs-title">array</span>
    </span>{
        <span class="hljs-keyword">return</span> &#91;<span class="hljs-string">'string'</span> =&gt; <span class="hljs-string">'test'</span>, <span class="hljs-string">'number'</span> =&gt; <span class="hljs-number">42</span>, <span class="hljs-string">'array'</span> =&gt; &#91;<span class="hljs-string">'title'</span> =&gt; <span class="hljs-string">'title'</span>]];
    }

    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">processArray</span><span class="hljs-params">()</span>: <span class="hljs-title">void</span>
    </span>{
        $array = <span class="hljs-keyword">$this</span>-&gt;getArray();
        $res = <span class="hljs-keyword">$this</span>-&gt;funcWithInt($array&#91;<span class="hljs-string">'number'</span>]);

	<span class="hljs-comment">/** Test $anotherArray */</span>
    	$anotherArray = &#91;<span class="hljs-string">'string'</span> =&gt; <span class="hljs-string">'test2'</span>, <span class="hljs-string">'number'</span> =&gt; <span class="hljs-number">66</span>];
	$res = <span class="hljs-keyword">$this</span>-&gt;funcWithInt($anotherArray&#91;<span class="hljs-string">'number'</span>]);
		
        <span class="hljs-keyword">echo</span> <span class="hljs-string">"Result: "</span> . $res;
    }

    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">funcWithInt</span><span class="hljs-params">(int $number)</span>: <span class="hljs-title">int</span>
    </span>{
        <span class="hljs-keyword">return</span> $number + <span class="hljs-number">1</span>;
    }
}</span></code></span></pre>


<p>Now the code will pass the PHPStan audit and some other benefits come with this:<br />PHPStan types are also supported beyond PHPStan. <br />IDEs like PhpStorm can recognize and use the type definitions. <br />Additionally, PHPStan types are compatible with <a href="https://psalm.dev/" target="_blank" rel="noopener" title="">Psalm</a> (another static analysis tool which is often used in combination with PHPStan).</p>



<p>In general, I would say it is better to map arrays to typed objects, such as DTOs, but sometimes this is not practical, and in those cases, PHPStan types provide a quick way to type untyped data.</p>



<p>You can also use PHPStan types across files by importing the phpstan-type in another file.</p>



<p>To use a phpstan-type across files, you can import it using the <code>@phpstan-import-type</code> annotation in another class&#8217;s PHPDocs. <br />Note that phpstan-import-type must be placed in the class&#8217;s docblock.<br />See: <a href="https://phpstan.org/writing-php-code/phpdoc-types#local-type-aliases">https://phpstan.org/writing-php-code/phpdoc-types#local-type-aliases</a></p>



<p></p>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">/**
 * <span class="hljs-doctag">@phpstan</span>-import-type Test from ExampleClass
 */</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">OtherClass</span>
</span>{
  <span class="hljs-comment">/**
  * <span class="hljs-doctag">@param</span> Test&#91;] $test
  */</span>
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">doSomething</span><span class="hljs-params">(array $test)</span> </span>{
    ...
  }
}</code></span></pre>The post <a href="https://nerdpress.org/2025/01/04/typed-arrays-with-phpstan-types/">Typed Arrays with PHPStan types</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
