-
Notifications
You must be signed in to change notification settings - Fork 397
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
ConnectionInterface vs. SqlConnectionInterface #537
Conversation
Yes, this is because it's not sure that |
It is considered good practice to define the most generic type which is applicable for the actual use-case to increase flexibility and reduce coupling. |
Added ConnectionInterface type hints and inline type hints for SqlConnectionInterface |
Does your change fix the warnings in the IDE? |
yes |
So lets land it, thanks 👍 |
Actually, no - the code is wrong. It's correct when @staabm says "It is considered good practice to define the most generic type" BUT if you then use methods on that object from a specific subtype, you must check for that. So, either expect the specific subtype
Some IDEs support the instanceof check and automatically adjust their type-hinting, well if not, this is where the doc-hint would make sense. |
@gossi is right @gharlan when the method body of a method uses methods which are only available on In places where only methods are required which are defined in the phpdoc type should always match with the typehint. |
I'm not sure here because of:
How do you want to deal with databases which do not have these methods? Will the generated code differ depending on the database type? |
since we need to change the typehints for such kind of thing, this would break BC and we would need a new major version anyway... Nothing we need to think about right now IMO.. @marcj @willdurand opinions? |
I don't get the problem? |
Yes, as currently. |
So, is the current pr ok, or shall I change something? |
ping @willdurand |
Why not type-hinting with the |
People overriding this method in a subclass would get strict errors in case we change the typehint in the future from I am not sure though if we will get non-sql connection handling in the near future and we should bother with it right now... maybe we will nevertheless have a new major version for the non-sql things and therefore can break BC anyway. |
@willdurand Where would you change the type hints? All generated methods, or only for methods using methods of |
only for methods using methods of |
hmm, the code would look like this: public function save(ConnectionInterface $con = null)
{
// ...
$this->doSave($con);
// ...
}
public function doSave(ConnectionInterface $con = null)
{
// ...
$this->doInsert($con);
// ...
}
public function doInsert(SqlConnectionInterface $con = null)
{
// ...
$con->prepare(...);
// ...
} In my opinion the other methods also need |
yeah its a problem because you "cast" the more generic its only works the other way arround - pass a more specific in a method which expects a more generic interface - not the way it is depicted here. |
Changed a model class like above manually.. now I get an exception on save:
The |
Then, let's remove |
ok, it's ready.. |
ConnectionInterface vs. SqlConnectionInterface
Perfect, thanks! |
In many places ConnectionInterface is used as parameter type, but in method body methods of SqlConnectionInterface are used. So my IDE says that the method does not exist in ConnectionInterface.
Example: