How to open source a PHP package (phpDay 2017)

How to open source a PHP package (phpDay 2017)

6074dbe5dfa361c9888a705f40a49af4?s=128

Jonathan Reinink

May 13, 2017
Tweet

Transcript

  1. None
  2. Jonathan Reinink I live in Canada. I have been writing

    PHP for 18 years. I spent a decade at marketing agency. I am now a contract developer.
  3. None
  4. What is a package?

  5. Packages are important because they provide code you don’t have

    to write.
  6. Today there are 139,000 PHP package available on Packagist.

  7. packagist.org/statistics

  8. Packages are a BIG part of what makes a language

    successful.
  9. Have you ever contributed an open-source PHP package?

  10. Contributing to open-source helps both you as developer and the

    greater PHP ecosystem.
  11. If your business relies heavily on certain packages, those are

    great places to get involved.
  12. Creating packages can be a little intimidating.

  13. Introducing The PHP Package Checklist phppackagechecklist.com

  14. None
  15. None
  16. Recommendations for open-source packages, but applicable to any PHP library.

  17. Pick a name wisely

  18. Have a unique identifier in your package name.

  19. Twig Guzzle Monolog Flysystem Swift Mailer Eloquent Blade Aura

  20. Ensure the name isn't already used by another project.

  21. Maintain consistency between this name and your PHP namespace.

  22. Avoid using last names or personal handles in your PHP

    namespaces.
  23. // Good: namespace Glide; namespace League\Glide; // Avoid: namespace Reinink\Glide;

    namespace Images; namespace ImageManipulations; namespace PHPGlide; namespace YoloImages;
  24. Host source openly

  25. Hosting your code openly encourages: • Contributions • Downloads •

    Code Reviews • Bug Reports
  26. Recommendation: GitHub

  27. Even if you prefer Bitbucket, SourceForge, or Google Code, your

    end users most likely won’t.
  28. Autoloader friendly

  29. Use a PSR-4 compatible autoloader namespace.

  30. Distribute via Composer

  31. $ composer require league/glide

  32. { "name": "league/glide", "require": { "intervention/image": "^2.1", "league/flysystem": "^1.0", "php":

    "^5.4 | ^7.0", "psr/http-message": "^1.0" }, "autoload": { "psr-4": { "League\\Glide\\": "src/" } } }
  33. List your library on Packagist, the main Composer repository.

  34. None
  35. Committing your composer.lock file has no impact on your end

    users.
  36. Place code in the /src folder.

  37. Framework agnostic

  38. Don't limit your library to one framework.

  39. Offer framework specific support for your package using service providers.

  40. None
  41. Follow a coding style

  42. Highly recommended that you adhere to the PSR-2 coding style

    guide.
  43. A familiar coding style makes open-source code easier to contribute

    to.
  44. Do this quickly with an automated code fixer like PHP

    Coding Standards Fixer. cs.sensiolabs.org
  45. You can also use services like Nitpick or StyleCI to

    help you with this.
  46. Write unit tests

  47. Aim to cover the majority of your code.

  48. PHPUnit is the de facto testing framework in PHP.

  49. Alternatives: phpspec, Behat, atoum, Codeception.

  50. DocBlock your code

  51. /** * Set source file system. * @param Filesystem $source

    Source file system. */ public function setSource(Filesystem $source) { $this->source = $source; }
  52. DocBlock serve as inline documentation.

  53. They can also improve code completion in IDEs, like PhpStorm.

  54. Can be automatically converted into API documentation.
 (see phpDocumentor)

  55. Use semantic versioning

  56. Follow the pattern: MAJOR.MINOR.PATCH BREAKING.NEW-FEATURES.BUG-FIXES

  57. Semantic versioning allows developers to upgrade libraries safely.

  58. semver.org

  59. None
  60. Keep a changelog

  61. Show notable changes that have been made between releases.

  62. Consider following the Keep a CHANGELOG format. (see keepachangelog.com)

  63. None
  64. {% for release in site.github.releases %} ## [{{ release.name }}]({{

    release.html_url }}) - {{ release.published_at | date: "%Y-%m-%d" }} {{ release.body | markdownify }} {% endfor %}
  65. Use continuous integration

  66. Use a service to automatically check coding standards and run

    tests.
  67. None
  68. None
  69. Write extensive documentation

  70. “Tests are hard so we practice to get better. Docs

    are hard… so we don’t write any and complain.” — Steve Klabnik, Senior Technical Writer (maintainer of the official Rust documentation)
  71. Don’t release an open source package unless your committed to

    writing good documentation.
  72. Consider hosting documentation on GitHub Pages.

  73. The formula to awesome docs:
 Tomorrow at 11am

  74. Include a license

  75. See choosealicense.com. Most PHP projects use the MIT License.

  76. The MIT License Copyright (c) <year> <copyright holders> Permission is

    hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  77. At a minimum, include a LICENSE file with your library.

  78. Welcome contributions

  79. Have a CONTRIBUTING file, welcoming contributions to the project.

  80. None
  81. Thanks! Follow me on Twitter at @reinink. Please rate this

    talk at joind.in/talk/74e5f.