2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHP・Laravelのバージョンアップ手法(アプリ編)

Last updated at Posted at 2025-08-12

概要

レガシーな環境で動いていたPHPとLaravelの大幅なバージョンアップし、本番リリースまで行ったので手法と注意点を残します。
アプリのバージョンアップについて紹介。
インフラ編はこちら

バージョン

PHP: 7.1 => 8.3
Laravel: 5.5 => 12.x

流れ

Laravelはメジャーバージョンごとにアップデートを行う。
PHP、Laravelの後方互換性のない変更はrector(後述)を活用して検出する。

  1. アップデート後のLaravelバージョンに対応するDocker環境を作成
  2. Rectorのインストール
  3. メジャーバージョン毎にアップデート
  4. 以下の検証をする
    1. PHPのアップグレードガイドに記載されている変更の確認・修正(PHPのバージョン変更があった場合)
    2. Laravelのアップグレードガイドに記載されている変更の確認・修正
    3. rectorの実行
    4. 動作確認

rectorについて

rectorとは?

PHPで作成されたアプリを、バージョン毎の記法でリファクタリングしてくれるツール。
Laravelにも対応したrector-laravelも開発されている。
適用、無視するルールを設定可能。

動作環境

要求: PHP7.2+
動作保証コード: PHP5.x - 8.x

リンク

rector: https://getrector.com/documentation
rector-laravel: https://github.com/driftingly/rector-laravel
rectorルール: https://github.com/rectorphp/rector/blob/main/docs/rector_rules_overview.md
rector-laravelルール: https://github.com/driftingly/rector-laravel/blob/main/docs/rector_rules_overview.md

インストール

  • composer require rector/rector --dev
  • require driftingly/rector-laravel --dev

設定はrector.phpで指定
実行を試すと自動生成 or 公式からコピペ。
例:

rector.php
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Set\ValueObject\LevelSetList;
use RectorLaravel\Set\LaravelLevelSetList;

return RectorConfig::configure()
    ->withPaths([
        __DIR__ . '/app',
        __DIR__ . '/config',
        __DIR__ . '/public',
        __DIR__ . '/resources',
        __DIR__ . '/routes',
        __DIR__ . '/tests',
    ])
    // uncomment to reach your current PHP version
    // ->withPhpSets()
    ->withSets([
        LevelSetList::UP_TO_PHP_83,
        LaravelLevelSetList::UP_TO_LARAVEL_120,
    ])
    ->withTypeCoverageLevel(0)
    ->withDeadCodeLevel(0)
    ->withCodeQualityLevel(0)
    // 除外ルール
    ->withSkip([
       ClosureToArrowFunctionRector::class,
    ]);;

実行方法

vendor/bin/rector process
# 変更を事前に確認したい場合はdry-runできる
vendor/bin/rector process --dry-run

dry-runで以下のように適用されるルールが確認できる。
例:

Applied rules:
 * RenameClassRector
 * AddClosureVoidReturnTypeWhereNoReturnRector

Laravelのバージョンアップ

作業ディレクトリでコマンドを実行

composer require -W laravel/framework:"^12.0"

依存関係で引っかかる場合は、そのライブラリも併せて更新。

composer require -W laravel/framework:"^12.0" some/library:"^x.0"

※require-dev指定のライブラリは同時にアップデートするとrequireになってしまうので、後でdevに戻す。

注意点

引っかかった点を列挙

  • 設定ファイルの変更等、rector-laravelで対応不可な変更もあるので注意。アップグレードガイドを読むしかない。
  • Laravel以外の個別でインストールしたライブラリは、アップデート後のLaravel、PHPバージョンに対応していないケースがある。その場合は移行するライブラリを見つけるか自作する必要がある。
  • Laravelの依存でインストールしているライブラリを直接利用している場合(カスタム等の理由)、そちらのドキュメントも確認する必要がある。ありがちなのは以下
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?