Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thoughts on the possible implementation #1

Open
klesun opened this issue Apr 15, 2020 · 5 comments
Open

Thoughts on the possible implementation #1

klesun opened this issue Apr 15, 2020 · 5 comments

Comments

@klesun
Copy link
Owner

klesun commented Apr 15, 2020

The task at hand is to make it possible for a developer to write php code with static typing of associative arrays similar to typescript interfaces.

  • Completion/GoTo
  • Wrong keys error checks
  • Ability to define global type aliases, like @psalm-type

I considered using hack, but that does not seem to be an option anymore in 2k20

First thing to do is to make basic completion from PSALM-ish phpdoc comment and type inference.

I just thought of one thing: what if we declare global types using .d.ts files and typescript syntax? Just a random idea, I did not think deep about problems like specifying php classes in this structure...

And here comes another idea: using files with hack syntax for global type defs! These would supposedly have syntax support from the hack plugins out of the box!

@klesun
Copy link
Owner Author

klesun commented Apr 20, 2020

After discussing with Louis and Mike today, we agreed to use PSALM-ish format for global type definition files, slightly adjusted to give names to types:

// same as `@template T` in phpdoc
type Sale<T> = array{
    key1: T,
};

type Passenger<T> = array{
    key1: T,
};

type DeepStructure = {
    records: array<array{
        name: string,
        passengers: array<Passenger>,
    }>
};

type IntOrStr = int | string;

@mholubowski
Copy link

@klesun

Wanted to bring these to your attention:

@klesun
Copy link
Owner Author

klesun commented May 10, 2020

A small summary on all the three analyzers and their extensions:

  • phpstan, among alive ones, has PHP Static Analysis extension, which seems to work ok with a phpstan.neon yaml-ish neon config file. You can declare/reuse typedefs in the config file like this:

    parameters:
      level: 7
      typeAliases:    
        Name: 'string'    
        NameResolver: 'callable(): string'    
        NameOrResolver: 'Name|NameResolver'

    For complex typedefs you can use neon's ''' to express multi-line type strings

    parameters:
      level: 7
      typeAliases:    
        SomePurchase: '''
          array{
            id: int,
            amount: float,
            currency: string,
            product: string,
            dt: \DateTime,
          }
        '''
  • In phan, as far as I could dig up, there does not exist an existing functionality to declare type aliases. There is this extension, though, for inline types only, that seems to work pretty ok: PHP Phan (Analyzer)

  • psalm has Psalm (PHP Static Analysis Linting Machine) extension that highlights errors when you hit ctrl+s. Yet again confirmed that they don't have global typedefs yet, though file-scoped typedefs seem to work ok with the extension:

    /**
     * @psalm-type Product = array{id:int, name:string, ololo: float}
     */
    

@klesun
Copy link
Owner Author

klesun commented May 10, 2020

@mholubowski are you ok with using the phpstan's phpstan.neon file in the root of a project for global type defininitions?

Since phpstan offers global type defs out of the box, I suggest us to stick with it instead of psalm. That way, all the static typing error validation would be available even without my extension. And then in deep-assoc I could fully focus on making high-quality completion and GoTo to work nicely with the phpstan extension. I'll also add PHP Static Analysis as an optional dependency so that you did not have to make notes of what extensions to install.

image

You can try this configured example out from here. You would only need phpstan installed: composer global require phpstan/phpstan and the phpstan extension

I'll gladly do something to improve the UI, like showing on which exactly key do types mismatch.

@klesun
Copy link
Owner Author

klesun commented May 15, 2020

Note, nothing stops us from using both psalm and phpstan formats. But since phpstan format is already complete and available for a long time, you may want to consider starting typing using it for now, while @psalm-import-type is still not in master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants