Skip to content

4.x upgrade snippets

Mark Sch edited this page Nov 16, 2020 · 37 revisions

Manual Upgrade commands

to be used for CLI execution or to be wrapped in Upgrade commands.

Basically in < 1h using the following commands for re-usability:

This is specifically working for plugins and PSR2R coding standard. It will need adjustment for others.

If you didnt yet upgrade to 4.x, you can also replace some of this using the core upgrade commands and rector:

cp composer.json composer.backup && composer require --dev rector/rector && mv composer.backup composer.json

vendor/bin/rector process --set=cakephp40 src/
vendor/bin/rector process --set=cakephp40 tests/
vendor/bin/rector process --set=phpunit80 tests/

3.x => 4.x

A fresh upgrade

Please also check out as most of the following have been moved there as automated upgrade snippets. They also work for CakePHP coding standard and get applied to all files (even nested ones).

So definitely prefer the upgrade tool over the following manual regex snippets!


git checkout master
git pull
git checkout -b cake4

sed -i 's|"php": ">=5.6"|"php": ">=7.2"|g' composer.json
sed -i 's|"php": ">=5.6.[0-9]\+"|"php": ">=7.2"|g' composer.json

sed -i -e 's|"cakephp/cakephp": "^3.[0-9]\+"|"cakephp/cakephp": "^4.0.0"|g' composer.json
sed -i -e 's|"cakephp/cakephp": "^3.[0-9]\+.[0-9]\+"|"cakephp/cakephp": "^4.0.0"|g' composer.json

sed -i -e 's|phpunit-[56].[0-9]\+.[0-9]\+.phar|phpunit-8.5.8.phar|g' composer.json

Add - only if you use plugins that arent released yet:

	"prefer-stable": true,
	"minimum-stability": "dev",

Now you should already be able to composer update the new 4.x dependencies.

Tip: can help a lot here!


// many snippets moved to upgrade plugin now

sed -i 's|public function initialize(array $config) {|public function initialize(array $config): void {|g' src/Model/Table/*Table.php

# TODO: always $middlewareQueue var name etc
sed -i 's|public function middleware($middleware) {|public function middleware(\\Cake\\Http\\MiddlewareQueue $middleware): \\Cake\\Http\\MiddlewareQueue {|g' src/Application.php
sed -i 's|public function middleware($middlewareQueue) {|public function middleware(\\Cake\\Http\\MiddlewareQueue $middlewareQueue): \\Cake\\Http\\MiddlewareQueue {|g' src/Application.php

sed -i 's|protected function buildOptionParser(ConsoleOptionParser $parser) {|protected function buildOptionParser(ConsoleOptionParser $parser): \\Cake\\Console\\ConsoleOptionParser {|g' src/Command/*Command.php

sed -i 's|public function getOptionParser() {|public function getOptionParser(): \\Cake\\Console\\ConsoleOptionParser {|g' src/Shell/*.php
sed -i 's|public function startup() {|public function startup(): void {|g' src/Shell/*.php
sed -i 's|public function initialize() {|public function initialize(): void {|g' src/Shell/*.php

sed -i 's|public function initialize(array $config) {|public function initialize(array $config): void {|g' src/View/Helper/*Helper.php

mv src/Template/ templates/
mv templates/Layout/ templates/layout/
mv templates/Element/ templates/element/
mv templates/element/Flash/ templates/element/flash/
mv templates/Email/ templates/email/
mv templates/layout/Email/ templates/layout/email/

find templates/ -type f -name "*.ctp" -exec rename -f 's/\.ctp$/.php/' {} \;

mv src/Locale/ resources/locales


rm -f phpunit.phar && composer test-setup

// many snippets moved to upgrade plugin now

sed -i -e 's/  - 5.6/  - 7.2/g' .travis.yml
sed -i 's/php: 5.6/php: 7.2/' .travis.yml
sed -i -e 's/phpunit\/phpunit:"[^"]*"/phpunit\/phpunit:"^8.3"/g' .travis.yml

sed -i 's|public function init() {|public function init(): void {|g' tests/Fixture/*Fixture.php
sed -i 's|public $fixtures = \[|protected $fixtures = [|g' tests/TestCase/*/*.php

and if you have any tests/test_app/ test app set up, you also need to treat that as src/ code and run the same commands over it, e.g.:

sed -i 's|public function beforeFilter(Event $event)|public function beforeFilter(\\Cake\\Event\\EventInterface $event)|g' tests/test_app/Controller/*Controller.php

Specific files

sed -i 's/use Tools\TestSuite\IntegrationTestCase;/use Shim\TestSuite\IntegrationTestCase;/' tests/TestCase/*/*.php

sed -i 's/use Tools\TestSuite\TestCase;/use Shim\TestSuite\TestCase;/' tests/TestCase/*/*.php

meta files

sed -i 's/\*\*CakePHP 3\.[0-9]+\+\*\*/**CakePHP 4.0+**/'

sed -i 's/php-%3E%3D%205.6-/php-%3E%3D%207.2-/'

some custom ones

use Cake\Network\Exception\ => use Cake\Http\Exception\

'prefix' => 'admin' => 'prefix' => 'Admin'

hydrate() => enableHydration()

more to be added for sure... let me know