microsoftgraph / microsoftgraph/msgraph-sdk-php

Model inheritance issues

Aperta
#848 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

enhancement
Lingua principale
PHP
Stelle
669
Fork
150
Merge medio
15h 21m
PR unite (30g)
3

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia esaminando i metodi dei modelli Message, OutlookItem ed Entity descritti nell’issue, quindi riproduci le chiamate concatenate con PHPStan. Definisci l’ambito della modifica dell’ereditarietà e conferma che il concatenamento mantenga il tipo concreto del modello senza gli errori PHPStan segnalati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
php
Ambito
backend-api-design
Tipo di issue
Refactoring
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.