EC-CUBE 4のカスタマイズに際し、筆者が遭遇したエラーとその対処法について記録を残しています。(随時更新中)
同じようなエラーに遭遇したときの参考になれば幸いです。

名前空間とファイルの保存先が異なる / ファイル名とクラス名が異なるケース
Compile Error: Cannot declare class Customize\Controller\Admin\SampleController, because the name is already in use

名前空間(namespace)に書かれたディレクトリと、実際のファイルの保存先が異なっている場合などに発生するエラーです。上の例では、名前空間(namespace)が「Customize\Controller\Admin」であるにも関わらず、ファイルの保存先が「app\Customize\Controller\SampleController」となっています。
名前空間と保存先が一致するように修正すれば解消します。
Compile Error: Cannot declare class Customize\Controller\SamplePageController, because the name is already in use

ファイル名とクラス名が一致しない場合にも同様のエラーが発生します。オートローダーがクラス名から対応するファイルを見つける際に、ファイル名とクラス名が一致していないと、クラスを正しくロードできないために発生するようです。
ファイル名とクラス名を同じにすれば解決します。
上の例では「SampleController.php」というファイル名なので、そこに記載するクラス名は「SampleController」にします。
useステートメントがファイルに含まれていないケース
Attempted to load class “AbstractController” from namespace “Customize\Controller”.
Did you forget a “use” statement for e.g. “Symfony\Bundle\FrameworkBundle\Controller\AbstractController” or “Eccube\Controller\AbstractController”?

Could not load type “Customize\Form\Type\Admin\TextType”: class does not exist.

このファイル内で使用しているクラス(この場合「AbstractController」や「TextType」)が、「use」ステートメントに含まれていない場合に発生します。既存のControllerなどのファイルをCustomizeディレクトリに複製してカスタマイズした場合に発生しやすいです。
利用するファイルを「useステートメント」に追加すれば解消します。
上の例では、「use Eccube\Controller\AbstractController;」や「use Symfony\Component\Form\Extension\Core\Type\TextType;」を追加します。
Twigに渡されていない変数が使われているケース
Variable “sample” does not exist

ページにアクセスしたとき、そのページを構成するTwigテンプレート内で使用している変数(この場合「sample」)が定義されていない場合に発生します。ControllerからTwigに渡すはずの変数を定義し忘れていたり、単に変数のスペルを間違えていたりする場合に発生しやすいです。
エラーメッセージに表示されている変数(上の例では「sample」)を定義 または 削除すれば解消します。
ちなみに、変数を定義する方法は以下の通りです。
forタグやifタグが正しく閉じられていないケース
Unexpected “endblock” tag (expecting closing tag for the “for” tag defined near line 87).

Twigテンプレート内でfor
タグを開いたが、正しく閉じられていない場合に発生します。(for
タグが開かれたままの状態で、誤ってendblock
タグで閉じているためエラーになった。)
テンプレートを確認し、for
タグをendfor
で閉じることで解消します。
同様に、if
タグもendif
で閉じるのを忘れないように。

クラス内で同じメソッドを複数回宣言しているケース
Compile Error: Cannot redeclare Customize\Controller\SampleController::index()

クラスの中で同じ名前のメソッドを2回以上宣言しようとしたときに発生します。この例では、SampleController
クラス内で index
メソッドが重複して定義されていることが原因です。すでに宣言されているメソッドをコピーして新しいメソッドを作成したときに、メソッド名の変更を忘れて更新してしまうと発生しやすいです。
index
メソッドが1つになるよう削除するか、メソッド名を変更すれば解消します。
Symfony 4.3 以降でRegistryInterfaceを宣言しているケース
Cannot autowire service “Customize\Repository\FaqRepository”: argument “$registry” of method “__construct()” has type “Symfony\Bridge\Doctrine\RegistryInterface” but this class was not found.

古いバージョンで作成されたリポジトリをコピーするなどした際に発生しやすいエラーです。
Symfony 4.3 以降、RegistryInterface
は非推奨になり削除されたため、Doctrine\Persistence
名前空間のManagerRegistry
を使用する必要があります。
useステートメントで、use Symfony\Bridge\Doctrine\RegistryInterface;
をuse Doctrine\Persistence\ManagerRegistry as RegistryInterface;
に変更すると解消します。

Symfony 4.2 以降でFormTypeExtensionInterface::getExtendedTypes() メソッドを実装していないケース
Error: Class Customize\Form\Extension\Front\ContactTypeExtension contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Symfony\Component\Form\FormTypeExtensionInterface::getExtendedTypes)

古いバージョンで作成されたフォームタイプをコピーするなどした際に発生しやすいエラーです。
Symfony 4.2 以降、getExtendedType()
メソッドは 非推奨 となり、getExtendedTypes()
をstaticメソッド として実装する必要があります。
以下のようにgetExtendedType()
をgetExtendedTypes()
に変更、staticメソッドとして実装し、戻り値の型をiterableにすることで解消します。(参考:公式サイト)
public static function getExtendedTypes(): iterable
{
return [ContactType::class];
}


