| Conditions | 7 |
| Paths | 1 |
| Total Lines | 128 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 57 | public static function createDefaultConfiguration(): ConfigurationBuilderInterface |
||
| 58 | { |
||
| 59 | $config = new Configuration(); |
||
| 60 | |||
| 61 | // @todo Figure out a better way to provide context to the normalizing (before) callbacks. |
||
| 62 | $refConfig = new \ReflectionObject($config); |
||
| 63 | $refUserConfig = $refConfig->getProperty('userConfig'); |
||
| 64 | $refUserConfig->setAccessible(true); |
||
| 65 | |||
| 66 | /** @var Data $userConfig */ |
||
| 67 | $userConfig = $refUserConfig->getValue($config); |
||
| 68 | |||
| 69 | $config->addSchema( |
||
| 70 | 'convertEmoticons', |
||
| 71 | Expect::bool(true) |
||
| 72 | ); |
||
| 73 | |||
| 74 | $config->addSchema( |
||
| 75 | 'exclude', |
||
| 76 | Expect::structure([ |
||
| 77 | 'shortcodes' => Expect::arrayOf('string') |
||
| 78 | ->default([]) |
||
| 79 | ->before( |
||
| 80 | /** |
||
| 81 | * @param string|string[] $value |
||
| 82 | * |
||
| 83 | * @return string[] |
||
| 84 | */ |
||
| 85 | static function ($value): array { |
||
| 86 | return Normalize::shortcodes($value); |
||
| 87 | } |
||
| 88 | ), |
||
| 89 | ]) |
||
| 90 | ); |
||
| 91 | |||
| 92 | $config->addSchema( |
||
| 93 | 'locale', |
||
| 94 | Expect::anyOf(...EmojibaseDatasetInterface::SUPPORTED_LOCALES) |
||
| 95 | ->default('en') |
||
| 96 | ->before( |
||
| 97 | static function (string $value): string { |
||
| 98 | return Normalize::locale($value); |
||
| 99 | } |
||
| 100 | ) |
||
| 101 | ); |
||
| 102 | |||
| 103 | $config->addSchema( |
||
| 104 | 'native', |
||
| 105 | Expect::bool() |
||
| 106 | ->before( |
||
| 107 | static function (?bool $value = null) use ($userConfig): bool { |
||
| 108 | /** @var string $locale */ |
||
| 109 | $locale = $userConfig->has('locale') |
||
| 110 | ? $userConfig->get('locale') |
||
| 111 | : 'en'; |
||
| 112 | |||
| 113 | $default = \in_array($locale, EmojibaseDatasetInterface::NON_LATIN_LOCALES, true); |
||
| 114 | |||
| 115 | if ($value === null) { |
||
| 116 | return $default; |
||
| 117 | } |
||
| 118 | |||
| 119 | $native = $value && $default; |
||
| 120 | |||
| 121 | $userConfig->set('native', $native); |
||
| 122 | |||
| 123 | return $native; |
||
| 124 | } |
||
| 125 | ) |
||
| 126 | ); |
||
| 127 | |||
| 128 | $config->addSchema( |
||
| 129 | 'presentation', |
||
| 130 | Expect::anyOf(...EmojibaseDatasetInterface::SUPPORTED_PRESENTATIONS) |
||
| 131 | ->default(EmojibaseDatasetInterface::EMOJI) |
||
| 132 | ); |
||
| 133 | |||
| 134 | $config->addSchema( |
||
| 135 | 'preset', |
||
| 136 | Expect::arrayOf('string') |
||
| 137 | ->default(ShortcodeInterface::DEFAULT_PRESETS) |
||
| 138 | ->mergeDefaults(false) |
||
| 139 | ->before( |
||
| 140 | /** |
||
| 141 | * @param string|string[] $value |
||
| 142 | * |
||
| 143 | * @return string[] |
||
| 144 | */ |
||
| 145 | static function ($value): array { |
||
| 146 | // Presets. |
||
| 147 | $presets = []; |
||
| 148 | foreach ((array) $value as $preset) { |
||
| 149 | if (isset(ShortcodeInterface::PRESET_ALIASES[$preset])) { |
||
| 150 | $presets[] = ShortcodeInterface::PRESET_ALIASES[$preset]; |
||
| 151 | } elseif (isset(ShortcodeInterface::PRESETS[$preset])) { |
||
| 152 | $presets[] = ShortcodeInterface::PRESETS[$preset]; |
||
| 153 | } else { |
||
| 154 | throw InvalidConfigurationException::forConfigOption( |
||
| 155 | 'preset', |
||
| 156 | $preset, |
||
| 157 | \sprintf( |
||
| 158 | 'Accepted values are: %s.', |
||
| 159 | \implode( |
||
| 160 | ', ', |
||
| 161 | \array_map( |
||
| 162 | static function ($s) { |
||
| 163 | return \sprintf('"%s"', $s); |
||
| 164 | }, |
||
| 165 | ShortcodeInterface::SUPPORTED_PRESETS |
||
| 166 | ) |
||
| 167 | ) |
||
| 168 | ) |
||
| 169 | ); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | return \array_values(\array_unique($presets)); |
||
| 174 | } |
||
| 175 | ) |
||
| 176 | ); |
||
| 177 | |||
| 178 | $config->addSchema( |
||
| 179 | 'stringableType', |
||
| 180 | Expect::anyOf(Lexer::EMOTICON, Lexer::HTML_ENTITY, Lexer::SHORTCODE, Lexer::UNICODE) |
||
| 181 | ->default(Lexer::UNICODE) |
||
| 182 | ); |
||
| 183 | |||
| 184 | return $config; |
||
| 185 | } |
||
| 257 |