faq.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. .. _faq:
  2. =================================
  3. Frequently Asked Questions (FAQs)
  4. =================================
  5. .. contents::
  6. :local:
  7. :depth: 1
  8. .. _faq.rhumsaa-abandoned:
  9. How do I fix "rhumsaa/uuid is abandoned" messages?
  10. ##################################################
  11. When installing your project's dependencies using Composer, you might see the
  12. following message:
  13. .. code-block:: text
  14. Package rhumsaa/uuid is abandoned; you should avoid using it. Use
  15. ramsey/uuid instead.
  16. Don't panic. Simply execute the following commands with Composer:
  17. .. code-block:: bash
  18. composer remove rhumsaa/uuid
  19. composer require ramsey/uuid=^2.9
  20. After doing so, you will have the latest ramsey/uuid package in the 2.x series,
  21. and there will be no need to modify any code; the namespace in the 2.x series is
  22. still ``Rhumsaa``.
  23. .. _faq.final:
  24. Why does ramsey/uuid use ``final``?
  25. ###################################
  26. You might notice that many of the concrete classes returned in ramsey/uuid are
  27. marked as ``final``. There are specific reasons for this choice, and I will
  28. offer a few solutions for those looking to extend or mock the classes for
  29. testing purposes.
  30. But Why?
  31. --------
  32. .. raw:: html
  33. <div style="width:100%;height:0;padding-bottom:56%;position:relative;">
  34. <iframe src="https://giphy.com/embed/eauCbbW6MvqKI" width="100%" height="100%" style="position:absolute" frameBorder="0" class="giphy-embed" allowFullScreen></iframe>
  35. </div>
  36. <p><a href="https://giphy.com/gifs/eauCbbW6MvqKI">via GIPHY</a></p>
  37. First, let's take a look at why ramsey/uuid uses ``final``.
  38. UUIDs are defined by a set of rules --- published as `RFC 4122`_ --- and those
  39. rules shouldn't change. If they do, then it's no longer a UUID --- at least not
  40. as defined by `RFC 4122`_.
  41. As an example, let's think about :php:class:`Rfc4122\\UuidV1
  42. <Ramsey\\Uuid\\Rfc4122\\UuidV1>`. If our application wants to do something
  43. special with this type, it might use the ``instanceof`` operator to check that a
  44. variable is a UuidV1, or it might use a type hint on a method argument. If a
  45. third-party library passes a UUID object to us that extends UuidV1 but
  46. overrides some very important internal logic, then we may no longer have a
  47. version 1 UUID. Perhaps we can all be adults and play nicely, but ramsey/uuid
  48. cannot make any guarantees for any subclasses of UuidV1.
  49. However, ramsey/uuid *can* make guarantees about classes that implement
  50. :php:interface:`UuidInterface <Ramsey\\Uuid\\UuidInterface>` or
  51. :php:interface:`Rfc4122\\UuidInterface <Ramsey\\Uuid\\Rfc4122\\UuidInterface>`.
  52. So, if we're working with an instance of a class that is marked ``final``, we
  53. can guarantee that the rules for the creation of that object will not change,
  54. even if a third-party library passes us an instance of the same class.
  55. This is the reason why ramsey/uuid specifies certain :ref:`argument and return
  56. types <reference.types>` that are marked ``final``. Since these are ``final``,
  57. ramsey/uuid is able to guarantee the type of data these value objects contain.
  58. :php:class:`Type\\Integer <Ramsey\\Uuid\\Type\\Integer>` should never contain
  59. any characters other than numeral digits, and :php:class:`Type\\Hexadecimal
  60. <Ramsey\\Uuid\\Type\\Hexadecimal>` should never contain any characters other
  61. than hexadecimal digits. If other libraries could extend these and return them
  62. from UUID instances, then ramsey/uuid cannot guarantee their values.
  63. This is very similar to using strict types with ``int``, ``float``, or ``bool``.
  64. These types cannot change, so think of final classes in ramsey/uuid as types
  65. that cannot change.
  66. Overriding Behavior
  67. -------------------
  68. You may override the behavior of ramsey/uuid as much as you want. Despite the
  69. use of ``final``, the library is very flexible. Take a look at the myriad
  70. opportunities to change how the library works:
  71. * :ref:`rfc4122.version1.random`
  72. * :ref:`customize.timestamp-first-comb-codec`
  73. * :ref:`customize.factory`
  74. * :ref:`And more... <customize>`
  75. ramsey/uuid is able to provide this flexibility through the use of `interfaces`_,
  76. `factories`_, and `dependency injection`_.
  77. At the same time, ramsey/uuid is able to guarantee that neither a
  78. :php:class:`UuidV1 <Ramsey\\Uuid\\Rfc4122\\UuidV1>` nor a
  79. :php:class:`UuidV4 <Ramsey\\Uuid\\Rfc4122\\UuidV4>` nor an
  80. :php:class:`Integer <Ramsey\\Uuid\\Type\\Integer>` nor a
  81. :php:class:`Time <Ramsey\\Uuid\\Type\\Time>`, etc. will ever change because of
  82. `downstream`_ code.
  83. UUIDs have specific rules that make them practically unique. ramsey/uuid ensures
  84. that other code cannot change this expectation while allowing your code and
  85. third-party libraries to change how UUIDs are generated and to return different
  86. types of UUIDs not specified by `RFC 4122`_.
  87. Testing With UUIDs
  88. ------------------
  89. Sometimes, the use of ``final`` can throw a wrench in our ability to write
  90. tests, but it doesn't have to be that way. To learn a few techniques for using
  91. ramsey/uuid instances in your tests, take a look at :ref:`testing`.
  92. .. _RFC 4122: https://tools.ietf.org/html/rfc4122
  93. .. _interfaces: https://www.php.net/interfaces
  94. .. _factories: https://en.wikipedia.org/wiki/Factory_%28object-oriented_programming%29
  95. .. _dependency injection: https://en.wikipedia.org/wiki/Dependency_injection
  96. .. _downstream: https://en.wikipedia.org/wiki/Downstream_(software_development)