Code

Patches and pull requests are a great way to contribute code back to CakePHP.Pull requests can be created in GitHub, and are preferred over patch files inticket comments.

Initial Setup

Before working on patches for CakePHP, it’s a good idea to get your environmentsetup. You’ll need the following software:

  • Git
  • PHP 7.2.0 or greater
  • PHPUnit 5.7.0 or greater

Set up your user information with your name/handle and working email address:

  1. git config --global user.name 'Bob Barker'
  2. git config --global user.email 'bob.barker@example.com'

Note

If you are new to Git, we highly recommend you to read the excellent andfree ProGit book.

Get a clone of the CakePHP source code from GitHub:

After your fork is made, clone your fork to your local machine:

  1. git clone git@github.com:YOURNAME/cakephp.git

Add the original CakePHP repository as a remote repository. You’ll use thislater to fetch changes from the CakePHP repository. This will let you stay upto date with CakePHP:

  1. cd cakephp
  2. git remote add upstream git://github.com/cakephp/cakephp.git

Now that you have CakePHP setup you should be able to define a $testdatabase connection, andrun all the tests.

Working on a Patch

Each time you want to work on a bug, feature or enhancement create a topicbranch.

The branch you create should be based on the version that your fix/enhancementis for. For example if you are fixing a bug in 3.x you would want to use themaster branch as the base for your branch. If your change is a bug fix forthe 2.x release series, you should use the 2.x branch:

  1. # fixing a bug on 3.x
  2. git fetch upstream
  3. git checkout -b ticket-1234 upstream/master
  4.  
  5. # fixing a bug on 2.x
  6. git fetch upstream
  7. git checkout -b ticket-1234 upstream/2.x

Tip

Use a descriptive name for your branch, referencing the ticket or featurename is a good convention. e.g. ticket-1234, feature-awesome

The above will create a local branch based on the upstream (CakePHP) 2.x branch.Work on your fix, and make as many commits as you need; but keep in mind thefollowing:

  • Follow the Coding Standards.
  • Add a test case to show the bug is fixed, or that the new feature works.
  • Keep your commits logical, and write clear commit messages that providecontext on what you changed and why.

Submitting a Pull Request

Once your changes are done and you’re ready for them to be merged into CakePHP,you’ll want to update your branch:

  1. # Rebase fix on top of master
  2. git checkout master
  3. git fetch upstream
  4. git merge upstream/master
  5. git checkout <branch_name>
  6. git rebase master

This will fetch + merge in any changes that have happened in CakePHP since youstarted. It will then rebase - or replay your changes on top of the currentcode. You might encounter a conflict during the rebase. If the rebase quitsearly you can see which files are conflicted/un-merged with git status.Resolve each conflict, and then continue the rebase:

  1. git add <filename> # do this for each conflicted file.
  2. git rebase --continue

Check that all your tests continue to pass. Then push your branch to your fork:

  1. git push origin <branch-name>

If you’ve rebased after pushing your branch, you’ll need to use force push:

  1. git push --force origin <branch-name>

Once your branch is on GitHub, you can submit a pull request on GitHub.

Choosing Where Your Changes will be Merged Into

When making pull requests you should make sure you select the correct basebranch, as you cannot edit it once the pull request is created.

  • If your change is a bugfix and doesn’t introduce new functionality andonly corrects existing behavior that is present in the current release. Thenchoose master as your merge target.
  • If your change is a new feature or an addition to the framework, then youshould choose the branch with the next version number. For example if thecurrent stable release is 4.0.0, the branch accepting new features willbe 4.next.
  • If your change is a breaks existing functionality, or API’s then you’ll haveto choose then next major release. For example, if the current release is4.0.0 then the next time existing behavior can be broken will be in5.x so you should target that branch.

Note

Remember that all code you contribute to CakePHP will be licensed under theMIT License, and the Cake Software Foundation will become the owner of anycontributed code. Contributors should follow the CakePHP CommunityGuidelines.

All bug fixes merged into a maintenance branch will also be merged into upcomingreleases periodically by the core team.