Skip to content

Commit d187536

Browse files
committed
Add a section about how to add an argument to a method without breaking BC
1 parent 5790b77 commit d187536

File tree

1 file changed

+136
-71
lines changed

1 file changed

+136
-71
lines changed

contributing/code/bc.rst

Lines changed: 136 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -334,84 +334,149 @@ Changing Traits
334334
This table tells you which changes you are allowed to do when working on
335335
Symfony's traits:
336336

337-
================================================== ============== ===============
338-
Type of Change Change Allowed Notes
339-
================================================== ============== ===============
340-
Remove entirely No
341-
Change name or namespace No
342-
Use another trait Yes
337+
=============================================================================== ============== ===============
338+
Type of Change Change Allowed Notes
339+
=============================================================================== ============== ===============
340+
Remove entirely No
341+
Change name or namespace No
342+
Use another trait Yes
343343
**Public Properties**
344-
Add public property Yes
345-
Remove public property No
346-
Reduce visibility No
347-
Move to a used trait Yes
344+
Add public property Yes
345+
Remove public property No
346+
Reduce visibility No
347+
Move to a used trait Yes
348348
**Protected Properties**
349-
Add protected property Yes
350-
Remove protected property No
351-
Reduce visibility No
352-
Make public No
353-
Move to a used trait Yes
349+
Add protected property Yes
350+
Remove protected property No
351+
Reduce visibility No
352+
Make public No
353+
Move to a used trait Yes
354354
**Private Properties**
355-
Add private property Yes
356-
Remove private property No
357-
Make public or protected Yes
358-
Move to a used trait Yes
355+
Add private property Yes
356+
Remove private property No
357+
Make public or protected Yes
358+
Move to a used trait Yes
359359
**Constructors and destructors**
360-
Have constructor or destructor No
360+
Have constructor or destructor No
361361
**Public Methods**
362-
Add public method Yes
363-
Remove public method No
364-
Change name No
365-
Reduce visibility No
366-
Make final No :ref:`[6] <note-6>`
367-
Move to used trait Yes
368-
Add argument without a default value No
369-
Add argument with a default value No
370-
Remove argument No
371-
Add default value to an argument No
372-
Remove default value of an argument No
373-
Add type hint to an argument No
374-
Remove type hint of an argument No
375-
Change argument type No
376-
Change return type No
362+
Add public method Yes
363+
Remove public method No
364+
Change name No
365+
Reduce visibility No
366+
Make final No :ref:`[6] <note-6>`
367+
Move to used trait Yes
368+
:ref:`Add argument with a default value <add-argument-public-method>` No
369+
:ref:`Add argument with a default value <add-argument-public-method>` No
370+
Remove argument No
371+
Add default value to an argument No
372+
Remove default value of an argument No
373+
Add type hint to an argument No
374+
Remove type hint of an argument No
375+
Change argument type No
376+
Change return type No
377377
**Protected Methods**
378-
Add protected method Yes
379-
Remove protected method No
380-
Change name No
381-
Reduce visibility No
382-
Make final No :ref:`[6] <note-6>`
383-
Make public No :ref:`[8] <note-8>`
384-
Move to used trait Yes
385-
Add argument without a default value No
386-
Add argument with a default value No
387-
Remove argument No
388-
Add default value to an argument No
389-
Remove default value of an argument No
390-
Add type hint to an argument No
391-
Remove type hint of an argument No
392-
Change argument type No
393-
Change return type No
378+
Add protected method Yes
379+
Remove protected method No
380+
Change name No
381+
Reduce visibility No
382+
Make final No :ref:`[6] <note-6>`
383+
Make public No :ref:`[8] <note-8>`
384+
Move to used trait Yes
385+
Add argument without a default value No
386+
Add argument with a default value No
387+
Remove argument No
388+
Add default value to an argument No
389+
Remove default value of an argument No
390+
Add type hint to an argument No
391+
Remove type hint of an argument No
392+
Change argument type No
393+
Change return type No
394394
**Private Methods**
395-
Add private method Yes
396-
Remove private method No
397-
Change name No
398-
Make public or protected Yes
399-
Move to used trait Yes
400-
Add argument without a default value No
401-
Add argument with a default value No
402-
Remove argument No
403-
Add default value to an argument No
404-
Remove default value of an argument No
405-
Add type hint to an argument No
406-
Remove type hint of an argument No
407-
Change argument type No
408-
Add return type No
409-
Remove return type No
410-
Change return type No
411-
**Static Methods and Properties**
412-
Turn non static into static No
413-
Turn static into non static No
414-
================================================== ============== ===============
395+
Add private method Yes
396+
Remove private method No
397+
Change name No
398+
Make public or protected Yes
399+
Move to used trait Yes
400+
Add argument without a default value No
401+
Add argument with a default value No
402+
Remove argument No
403+
Add default value to an argument No
404+
Remove default value of an argument No
405+
Add type hint to an argument No
406+
Remove type hint of an argument No
407+
Change argument type No
408+
Add return type No
409+
Remove return type No
410+
Change return type No
411+
**Static Methods and Properties**
412+
Turn non static into static No
413+
Turn static into non static No
414+
=============================================================================== ============== ===============
415+
416+
Making Code Changes in a Backward Compatible Way
417+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
418+
419+
As you read above, many changes are not allowed because they would represent a
420+
backward compability break. However, we want to be able to improve the code and
421+
its features over time and that can nbe done thanks to some strategies that
422+
allow to still do some unallowed changes in several steps that ensure backward
423+
compability and a smooth upgrade path. Some of them are described in the next
424+
sections.
425+
426+
.. _add-argument-public-method:
427+
428+
Adding an Argument to a Public Method
429+
.....................................
430+
431+
Adding a new argument to a public method is possible only if this is the last
432+
argument of the method.
433+
434+
If that's the case, here is how to do it properly in a minor version:
435+
436+
#. Add the argument as a comment in the signature::
437+
438+
// the new argument can be optional
439+
public function say(string $text, /* bool $stripWithespace = true */): void
440+
{
441+
}
442+
443+
// or required
444+
public function say(string $text, /* bool $stripWithespace */): void
445+
{
446+
}
447+
448+
#. Document the new argument in a PHPDoc::
449+
450+
/**
451+
* @param bool $stripWithespace
452+
*/
453+
454+
#. Use ``func_num_args`` and ``func_get_arg`` to retrieve the argument in the
455+
method::
456+
457+
$stripWithespace = 2 <= \func_num_args() ? func_get_arg(1) : false;
458+
459+
Note that the default value is ``false`` to keep the current behavior.
460+
461+
#. If the argument has a default value that will change the current behavior,
462+
warn the user::
463+
464+
trigger_deprecation('symfony/COMPONENT', 'X.Y', 'Not passing the "bool $stripWithespace" argument explicitly is deprecated, its default value will change to X in Z.0.');
465+
466+
#. If the argument has no default value, warn the user that is going to be
467+
required in the next major version::
468+
469+
if (\func_num_args() < 2) {
470+
trigger_deprecation('symfony/COMPONENT', 'X.Y', 'The "%s()" method will have a new "bool $stripWithespace" argument in version Z.0, not defining it is deprecated.', __METHOD__);
471+
472+
$stripWithespace = false;
473+
} else {
474+
$stripWithespace = func_get_arg(1);
475+
}
476+
477+
5/ In the next major version (``X.0``), uncomment the argument, remove the
478+
PHPDoc if there is no need for a description, and remove the ``func_get_arg``
479+
code and the warning if any.
415480

416481
Notes
417482
~~~~~

0 commit comments

Comments
 (0)