Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

ForbiddenParamTypeRemovalRule - should ignore __construct #4290

Open
MartinMystikJonas opened this issue Jul 29, 2022 · 9 comments
Open

ForbiddenParamTypeRemovalRule - should ignore __construct #4290

MartinMystikJonas opened this issue Jul 29, 2022 · 9 comments

Comments

@MartinMystikJonas
Copy link

Rule ForbiddenParamTypeRemovalRule reports change of parameters even for constructors, where forcing compatibility with parent class constructor is not required and often constructor signature changes (for example child sets some parent constructor params to fixed value)

Do you agree? I can prepare PR

@TomasVotruba
Copy link
Member

TomasVotruba commented Jul 29, 2022

Hi, do you have some exact example? Parent types should be respected in all methods.

@MartinMystikJonas
Copy link
Author

Constructor is usually not considered as overwritten parent method but always a class specific method. Because you always use constructor of specific class (unless you use some dynamic class name magic) and never call it on parent class type. LSP does not apply to constructors.

@MartinMystikJonas
Copy link
Author

I found a good article with longer explanation https://www.sitepoint.com/constructors-and-the-myth-of-breaking-the-lsp/

@MartinMystikJonas
Copy link
Author

TL;DR Subtypes often require dirrerent/more/fewer dependencies than parent. Subtypes may not require some dependency (because it uses specific hardwired version), and may require new dependencies (because it extends parent functionality). Therefore child and parent constructors needs to differ to reflect this.

LSP is about instances of subtype can be used evwrywhere where parent type can be used. But it does not say anything about creation of these instances. Therefore it does not apply to constructors.

@TomasVotruba
Copy link
Member

Could you share the PHP code? It would be easier to understand for me

@MartinMystikJonas
Copy link
Author

MartinMystikJonas commented Jul 31, 2022

Example based on our code

 abstract class DataSource {
  public function __construct(DataMapper $mapper, $data) {
    // ...
  }
}

class RawDataSource extends DataSource {
  public function __construct($data) { // <-- there it reports removed parameter type because parameter $mapper was removed
    parent::__construct(new NoMapper(), $data) {
    // ...
  }
}

@TomasVotruba
Copy link
Member

I see. This should be skipped, as parameter is removed completelly and passed via new 👍

@MartinMystikJonas
Copy link
Author

Yes. Theoretically constructor can have completely different parameters.

So either skip constrictor completely. Or on case of co stri tor maybe check only if parameter name also match?

@TomasVotruba
Copy link
Member

TomasVotruba commented Jul 31, 2022

Or on case of co stri tor maybe check only if parameter name also match?

The removed parameter should skip completely. Otherwise the similar names should be matched, yes

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

No branches or pull requests

2 participants