Giter Club home page Giter Club logo

Comments (3)

Ocramius avatar Ocramius commented on July 29, 2024

/** @extends Form<SomeModel> */

Very cool and elegant 💪

from laminas-form.

gsteel avatar gsteel commented on July 29, 2024

Hopefully the psalm bot is working here. This is what I have in mind:

https://psalm.dev/r/0d6f0ee752

from laminas-form.

psalm-github-bot avatar psalm-github-bot commented on July 29, 2024

I found these snippets:

https://psalm.dev/r/0d6f0ee752
<?php

/**
 * @template TValues
 */
interface Filter {
    /** @return TValues */
    public function getValues(): array;
}

/**
 * @psalm-type MyPayload = array{
 *    foo: non-empty-string,
 * }
 * @implements Filter<MyPayload>
 */
class MyFilter implements Filter {
    
    public function getValues(): array
    {
        return ['foo' => 'bar'];
    }
}

/** @template TFilterShape */
interface Form
{
    /** @return TFilterShape */
    public function getData(): array;
    
    /** @param Filter<TFilterShape> $filter **/
    public function setFilter(Filter $filter): void;
}

/**
 * @template TFilterShape
 * @implements Form<TFilterShape>
 */
abstract class BaseForm implements Form
{
    /** @var Filter<TFilterShape> */
    private Filter|null $filter = null;
    
    /** @return TFilterShape */
    public function getData(): array
    {
        if (! $this->filter) { throw new Exception('Bad News'); }
        
        return $this->filter->getValues();
    }
    
    /** @param Filter<TFilterShape> $filter **/
    public function setFilter(Filter $filter): void
    {
        $this->filter = $filter;
    }
}

/**
 * @psalm-import-type MyPayload from MyFilter
 * @extends BaseForm<MyPayload>
 */
class MyForm extends BaseForm
{
}

$form = new MyForm();
$form->setFilter(new MyFilter());

$foo = $form->getData()['foo'];

/** @psalm-trace $foo */
Psalm output (using commit baa6d5d):

INFO: Trace - 72:25 - $foo: non-empty-string

INFO: UnusedVariable - 70:1 - $foo is never referenced or the value is not used

from laminas-form.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.