On Github msonnabaum / naming-things-presentation
Communication with people is the purpose of class names. As far as the computer is concerned, classes could simply be numbered.
Kent Beck. Implementation Patterns
<?php namespace Drupal\Core; class SystemListing {
<?php namespace Drupal\Core; /** * Returns information about system object files (modules, * themes, etc.). * * This class requires the list of profiles to be scanned (see * \Drupal\Core\SystemListing::scan) to be passed into the * constructor. Also, info files are not parsed. */ class SystemListing {
function scan($mask, $directory, $key = 'name') {
/** * @param string $mask * The preg_match() regular expression for the files to * find. The expression must be anchored and use * DRUPAL_PHP_FUNCTION_PATTERN for the file name part before * the extension, since the results could contain matches * that do not present valid Drupal extensions otherwise.
/** * @param string $directory * The subdirectory name in which the files are found. For * example, 'modules' will search all 'modules' directories * and their sub-directories as explained above.
/** * @param string $key * (optional) The key to be used for the associative array * returned. * Possible values are: * - 'uri' for the file's URI. * - 'filename' for the basename of the file. * - 'name' for the name of the file without the extension. * For 'name' and 'filename' only the highest-precedence file * is returned. * Defaults to 'name'.
/** * @return array * An associative array of file objects, keyed on the chosen * key. Each element in the array is an object containing * file information, with properties: * - 'uri': Full URI of the file. * - 'filename': File name. * - 'name': Name of file without the extension. */ function scan($mask, $directory, $key = 'name') {
protected function moduleData($module) { // First, find profiles. $profiles_scanner = new SystemListing(); $all_profiles = $profiles_scanner->scan( '/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.profile$/', 'profiles' ); $profiles = array_keys( array_intersect_key($this->moduleList, $all_profiles) ); // Now find modules. $modules_scanner = new SystemListing($profiles); $this->moduleData = $modules_scanner->scan( '/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.module$/', 'modules' );
protected function moduleData($module) { // First, find profiles. $extension_finder = new ExtensionFinder(); $all_profiles = $extension_finder->findProfiles(); $profiles = array_keys( array_intersect_key($this->moduleList, $all_profiles) ); // Now find modules. $this->moduleData = $extension_finder->findModules($profiles);
namespace Drupal\Core; class SystemListingInfo extends SystemListing {
namespace Drupal\Core; /** * Returns information about system object files (modules, * themes, etc.). * * This class finds the profile directories itself and also * parses info files. */ class SystemListingInfo extends SystemListing {
function drupal_system_listing($mask, $directory, $key = 'name', $min_depth = 1) {
namespace Drupal\field; class Field { public static function fieldInfo() { return \Drupal::service('field.info'); } }
/** * @file * Contains \Drupal\field\Field. */ namespace Drupal\field; /** * Static service container wrapper for Field. */ class Field { /** * Returns the field info service. * * @return \Drupal\field\FieldInfo * Returns a field info object. */ public static function fieldInfo() { return \Drupal::service('field.info'); } }
interface PluginManagerInterface extends DiscoveryInterface, FactoryInterface, MapperInterface { }
/** * Provides a breadcrumb manager. * * Holds an array of path processor objects and uses them to * sequentially process a path, in order of processor priority. */ class BreadcrumbManager implements BreadcrumbBuilderInterface { public function addBuilder(BreadcrumbBuilderInterface $builder, $priority) {} public function build(array $attributes) {} }
/** * Class responsible for initializing each language type. */ class LanguageManager {
namespace Drupal\Core\Locale; use Drupal\Core\Extension\ModuleHandlerInterface; /** * Provides list of countries. */ class CountryManager implements CountryManagerInterface {
namespace Drupal\system; /** * System Manager Service. */ class SystemManager { // Checks for requirement severity. public function checkRequirements() {} // Displays the site status report. Can also be used as a // pure check. public function listRequirements() {} // Fixes anonymous user on MySQL. public function fixAnonymousUid() {} // Extracts the highest severity from the requirements array. public function getMaxSeverity(&$requirements) {}
The names of subclasses have two jobs. They need to communicate what class they are like and how they are different.
Ken Beck. Implementation Patterns
namespace KeyValueStore; class DatabaseStorage {} class DatabaseStorageExpirable {}
When you feel the need to write a comment, first try to refactor the code so that any comment becomes superfluous.
Martin Fowler, Kent Beck. Refactoring: Improving the Design of Existing Code
protected function moduleData($module) { // First, find profiles. $profiles_scanner = new SystemListing(); $all_profiles = $profiles_scanner->scan( '/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.profile$/', 'profiles' );
protected function moduleData($module) { $extension_finder = new ExtensionFinder(); $all_profiles = $extension_finder->findProfiles();
Beck, Kent (2007-10-23). Implementation Patterns
Fowler, Martin; Beck, Kent; Brant, John; Opdyke, William; Roberts, Don (2012-03-09). Refactoring: Improving the Design of Existing Code