microsoftgraph / microsoftgraph/msgraph-sdk-php

Model inheritance issues

Open
#848 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
PHP
Stars
669
Forks
150
Avg merge
15h 21m
Merged PRs (30d)
3

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the Message, OutlookItem, and Entity model methods described in the issue, then reproduce the chained calls with PHPStan. Define the scope of the inheritance change and confirm that chaining preserves the concrete model type without the reported PHPStan errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend-api-design
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.