msgraph-sdk-php icon indicating copy to clipboard operation
msgraph-sdk-php copied to clipboard

Model inheritance issues

Open ianef opened this issue 2 years ago • 0 comments

There is a problem when using using method chaining on models that is caused by using class inheritance. The methods of each classes 'set' methods return $this which is great for chaining if the class in question is a final class, however for inherited classes $this relates to to the class the method exists in and not the actual class instantiated.

If you take the Message model for example, this inherits from OutlookItem, which in turn inherits from Entity. Calling getId() on a Message object returns an Entity object because that's where the getId() method is defined. If you create a chained process like this:

$message = (new Message()) ->setId($id) ->setCategories('fred') ->setBody('1234'); While this will actually work when run, if you use PHPStan you will get errors. In this instance you can't call setCategories() on an Entity object which is what is returned by the setId() method.

I would like to propose the use of traits to define the methods of each model that is currently inheritable. The model classes would then be a none inherited object but use the traits, which define the methods for that model, instead of inheritance. This would create model objects that truly expose each method of every trait providing the methods return 'self'.

Currently you can overcome the PHPStan issues by careful arrangement of chained methods making sure that you call the final class methods first, then each child in turn. This is not ideal but it is a work around until something is changed to improve coding standards.

ianef avatar Mar 30 '22 17:03 ianef