art4, German

I'm planning to add return types to interface methods in a #PHP library. Because of #BC I plan to do this in a next major release.

What's the best practice to document this return type change now, so the users of the library can add the return types now instead of waiting for the major release? Is there a recommended #phpdoc annotation? Or should I use the ReturnTypeWillChange attribute?

#software #dev #Symfony #WebDev

omerida,

@art4 add them as /** @return <type> $var */ docblock annotations. Users can then rely on static analysis to prepare for the change

art4,

@omerida Thanks, I already have a return annotation in v1.x.

I would like to have something like this:

/** @return bool */
#[\ReturnTypeWillChange('bool')]
public function has(string $key);

In v2.0 I can add the return type:

public function has(string $key): bool;

This way I would have a forward compatibility layer.

But I'm afraid the IDEs do not inform the users of v1.x that they should add the return type in there implementations now.

omerida,

@art4 why would users be adding return types to your library? If you already have the docblock return type, I think you don't need an annotation. You're users can use phpstan or psalm to make sure they are using your returned values in a forward-compatible way

art4,

@omerida because the library offers interfaces or abstract classes that the users have implemented/extended.
I'm looking for a programmatically way to inform them about changes they will have to do in their code.

omerida,

@art4 gotcha. You should look at rector and providing rules that will add the return type hints automatically to implementations. Then again, worst case - won't PHP throw an error when the return type hint is missing or different than the interface/base class?

art4,

@omerida
I'm not talking about doing the actual changes, I'm only talking about informing the users about the future work. Not every user is reading the issues or project blog. And yes, at least #PHP will throw exceptions after users upgrades to v2 without making the changes.

But I would like to offer a FC layer so users are informed upfront by their IDE, #PHPStan or other tools as early as possible. The goal is to improve the upgrade path.

Girgias,
@Girgias@phpc.social avatar

@art4 @omerida You could just use the #[\ReturnTypeWillChange] Attribute introduced in PHP 8.1.

https://php.watch/versions/8.1/internal-method-return-types

art4,

@Girgias @omerida Does this also work with userland code? And is there a way to communicate the new type?

Girgias,
@Girgias@phpc.social avatar

@art4 @omerida Yes, and just use a standard type declaration

art4,

@Girgias @omerida thank you very much! I will test that.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • php
  • ethstaker
  • DreamBathrooms
  • osvaldo12
  • InstantRegret
  • ngwrru68w68
  • magazineikmin
  • mdbf
  • thenastyranch
  • Youngstown
  • slotface
  • everett
  • kavyap
  • Durango
  • tester
  • JUstTest
  • GTA5RPClips
  • tacticalgear
  • modclub
  • khanakhh
  • rosin
  • cisconetworking
  • normalnudes
  • provamag3
  • Leos
  • cubers
  • anitta
  • megavids
  • lostlight
  • All magazines