lisachenko / z-engine

:zap: PHP Engine Direct API
MIT License
448 stars 22 forks source link

Question about typehints #45

Closed zmitic closed 3 years ago

zmitic commented 3 years ago

Sorry that I don't fully understand the abilities of FFI so I have to ask; is it possible to drop typehint check in generated opcode?

Reason 1: performance. I have few apps that deal with millions of callbacks like this (totally simplified example, real one has 8 methods running for each $result):


foreach($someGenerator as $result) {
    doSomething($result);
}

function doSomething(Product $product)  // <-  this is the issue

I use psalm and it is impossible to make a mistake and not see it. Even if I make one, during development runtime checks would still work; my question is about production.

The idea is not to use preprocess.io or similar or fallback to @param annotations; typehints must stay so reflection would still work (autowiring). It is only to remove opcode that verifies params/return types.


Reason 2: generics

If engine could transparently convert code like this


// from 
function doSomething(Collection<string, Product> $products)

// to
function doSomething(Collection $products)

with PHPStorm and psalm/phpstan support we could possibly have reified generics. Transparently means delete everything between < and > before giving it to compiler.

Problem

This would effectively remove TypeError exception but I don't expect many libraries rely on it for logic (if any). PHPUnit and similar are not business logic.


Another solution would be file-based config. So not all files would ignore typehint check but only if there is a declare at top.