Simon Kollross

Simon Kollross

Software developer, blogger, elegant code enthusiast.

Casing of acronyms in PHP and case-sensitive Git on macOS

Some weeks ago I started a Twitter survey to find out how PHP developers handle capitalization of acronyms in class names. 60 % of the participants voted for using strict camelCase in class names to avoid messy names like RESTAPIService.

As a result of this, I wanted to apply this convention to my own projects. So I had to rename classes like UserDTO to UserDto and apply this to namespaces as well.

I started renaming files and folders and I was surprised: Git didn't recognize the new filenames. Since I'm working on a MacBook with the latest macOS Mojave, I'm using the APFS file system, which is case-insensitive by default. Therefore, Git has no chance to detect if the filename has changed. In addition, this might become a real problem in usually case-sensitive file systems of Linux-based production environments, like a Laravel Forge provisioned server. The autoloader could fail to load classes, e.g. if we use UserDTO as class name that is stored in UserDto.php by accident. It won't fail in our local environment, but it will break our app in production.

I found out that you could use commands such as git mv to tell Git explicitly about the renamed files, which is a bit tedious if you have a lot of files to rename. To avoid that and to mimic production environments as close as possible, I created a new volume on my MacBook using a case-sensitive file system. As a result of this, Git picks up casing changes in filenames automatically and errors bubble up before breaking production environments.

  1. Open macOS "Disk Utility"
  2. Right click on "Macintosh HD" and choose "Add APFS volume..."
  3. Choose a name (e.g. Code) and format as "APFS (case-sensitive)"
  4. Click "Add"
  5. Copy your project to the newly created volume located at /Volumes/Code
  6. Open the project folder in Terminal and run git config --unset core.ignorecase to tell Git we're on a case-sensitive file system now
  7. Rename files and commit

If you git clone or git init a repository on your new case-sensitive volume, you don't have to run the git config command. Git will detect the file system automatically.