Module Development

Sharpen the Battle Result with a Class for Drupal 8, 9

Sprout Video

The next thing we want to tackle is our battle() function, with its array, inside our BattleManager class. It's not obvious at all what's inside the $outcome variable, or whether the keys it has now might be missing or different in the future. To rectify this we're going to create a new BattleResult model class with some properties and methods that will clean things up and remove the need for weird associative arrays. Our BattleManager::battle() function will return a nice BattleResult object, and we'll be in full control of what public methods we put on that.