PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label psalm-php. Show all posts
Showing posts with label psalm-php. Show all posts

Thursday, November 24, 2022

[FIXED] How to write generics for factories using psalm and phpstan

 November 24, 2022     factory-pattern, generics, php, phpstan, psalm-php     No comments   

Issue

I'm trying out phpstan and psalm for php and I would like to write a class that can take different type of objects and return the right one based on the factory to call.

What I'm trying to achieve is that if I pass an object of type A to the Transformer, the compiler knows that a SuperA will be returned.

While I can go without errors in psalm (though I still get SuperA|SuperB instead of the right object), I got an error on what I'm passing in phpstan.

https://phpstan.org/r/4fce6f46-7aea-4f73-8259-df895910f064

https://psalm.dev/r/352e64ea95

Is there a way to do it?


Solution

So you want to get SuperA based on A and SuperB based on B.

I'd connect A+SuperA and B+SuperB together like this: https://phpstan.org/r/28e4e6ec-887b-4735-9b34-c034b4fa04ec

/**
 * @template TSuper of Super
 */
interface Common
{
}

/**
 * @implements Common<SuperA>
 */ 
class A implements Common
{
}

/**
 * @implements Common<SuperB>
 */ 
class B implements Common
{
}

interface Super
{
}

class SuperA implements Super
{
    public function callA(): void{}
}

class SuperB implements Super
{
    public function callB(): void{}
}

The factory then needs to have this signature:

/**
 * @template T of Super
 * @param Common<T> $obj
 * @return T
 */
public function transform($obj)


Answered By - Ondřej Mirtes
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, April 18, 2022

[FIXED] How to annotate Laravel Collection elements during iteration

 April 18, 2022     laravel, php, phpstorm, psalm-php     No comments   

Issue

I was thinking about how to annotate types in PhpStorm. I believe PhpStorm is using Psalm to resolve types, but I can't find how to annotate type to get suggestions here:

enter image description here

$row in my app will always be Collection object and I want to have it marked somewhere here with annotations.

Does anyone have an idea how to accomplish that?

    /**
     * @param Collection $rows
     */
    public function collection(Collection $rows)
    {
        foreach ($rows as $row)
        {
            dump($row->); // $row is also Collection object
        }
    }

Solution

you can mark var type like this:

/**
 @var $row Collection
**/
dump($row->);

https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000701884-Using-var-type-hinting-with-properties-instantiated-by-Traits



Answered By - Eden Moshe
Answer Checked By - David Goodson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing