The code should look like this IMO

```
        // adds legacy plugin paths if needed
        if (System::isLegacyMode()) {
            $this->addPluginDir('lib/legacy/plugins'); // Core legacy plugins
            $this->addPluginDir($mpluginPathOld); // Module plugins (legacy paths)
        }
            
        // theme plugins
        $themePluginsPath = isset($themeBundle) ? $themeBundle->getPath() . '/modules/$moduleName/plugins' : "themes/$theme/templates/modules/$moduleName/plugins";
        $this->addPluginDir($themePluginsPath); // Module override in themes
```