| Total Lines | 111 |
| Code Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 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 |
||
| 53 | public function __construct(Builder $builder, $templatesPath) |
||
| 54 | { |
||
| 55 | $this->builder = $builder; |
||
| 56 | // load layouts |
||
| 57 | $loader = new \Twig\Loader\FilesystemLoader($templatesPath); |
||
| 58 | // default options |
||
| 59 | $loaderOptions = [ |
||
| 60 | 'debug' => $this->builder->isDebug(), |
||
| 61 | 'strict_variables' => true, |
||
| 62 | 'autoescape' => false, |
||
| 63 | 'auto_reload' => true, |
||
| 64 | 'cache' => false, |
||
| 65 | ]; |
||
| 66 | // use Twig cache? |
||
| 67 | if ((bool) $this->builder->getConfig()->get('cache.templates.enabled')) { |
||
| 68 | $loaderOptions = array_replace($loaderOptions, ['cache' => $this->builder->getConfig()->getCacheTemplatesPath()]); |
||
| 69 | } |
||
| 70 | // create the Twig instance |
||
| 71 | $this->twig = new \Twig\Environment($loader, $loaderOptions); |
||
| 72 | // set date format |
||
| 73 | $this->twig->getExtension(\Twig\Extension\CoreExtension::class) |
||
| 74 | ->setDateFormat((string) $this->builder->getConfig()->get('date.format')); |
||
| 75 | // set timezone |
||
| 76 | if ($this->builder->getConfig()->has('date.timezone')) { |
||
| 77 | $this->twig->getExtension(\Twig\Extension\CoreExtension::class) |
||
| 78 | ->setTimezone((string) $this->builder->getConfig()->get('date.timezone') ?? date_default_timezone_get()); |
||
| 79 | } |
||
| 80 | /* |
||
| 81 | * adds extensions |
||
| 82 | */ |
||
| 83 | // Cecil core extension |
||
| 84 | $this->twig->addExtension(new CoreExtension($this->builder)); |
||
| 85 | // required by `template_from_string()` |
||
| 86 | $this->twig->addExtension(new \Twig\Extension\StringLoaderExtension()); |
||
| 87 | // l10n |
||
| 88 | $this->translator = new Translator( |
||
| 89 | $this->builder->getConfig()->getLanguageProperty('locale'), |
||
| 90 | new MessageFormatter(new IdentityTranslator()), |
||
| 91 | (bool) $this->builder->getConfig()->get('cache.templates.enabled') ? $this->builder->getConfig()->getCacheTranslationsPath() : null, |
||
| 92 | $this->builder->isDebug() |
||
| 93 | ); |
||
| 94 | if (\count($this->builder->getConfig()->getLanguages()) > 0) { |
||
| 95 | foreach ((array) $this->builder->getConfig()->get('layouts.translations.formats') as $format) { |
||
| 96 | $loader = \sprintf('Symfony\Component\Translation\Loader\%sFileLoader', ucfirst($format)); |
||
| 97 | if (class_exists($loader)) { |
||
| 98 | $this->translator->addLoader($format, new $loader()); |
||
| 99 | $this->builder->getLogger()->debug(\sprintf('Translation loader for format "%s" found', $format)); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | foreach ($this->builder->getConfig()->getLanguages() as $lang) { |
||
| 103 | // internal |
||
| 104 | $this->addTransResource($this->builder->getConfig()->getTranslationsInternalPath(), $lang['locale']); |
||
| 105 | // themes |
||
| 106 | if ($themes = $this->builder->getConfig()->getTheme()) { |
||
| 107 | foreach ($themes as $theme) { |
||
| 108 | $this->addTransResource($this->builder->getConfig()->getThemeDirPath($theme, 'translations'), $lang['locale']); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | // site |
||
| 112 | $this->addTransResource($this->builder->getConfig()->getTranslationsPath(), $lang['locale']); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | $this->twig->addExtension(new TranslationExtension($this->translator)); |
||
| 116 | // intl |
||
| 117 | $this->twig->addExtension(new IntlExtension()); |
||
| 118 | if (\extension_loaded('intl')) { |
||
| 119 | $this->builder->getLogger()->debug('PHP Intl extension is loaded'); |
||
| 120 | } |
||
| 121 | // filters fallback |
||
| 122 | $this->twig->registerUndefinedFilterCallback(function ($name) { |
||
| 123 | switch ($name) { |
||
| 124 | case 'localizeddate': |
||
| 125 | return new \Twig\TwigFilter($name, function (?\DateTime $value = null) { |
||
| 126 | return date($this->builder->getConfig()->get('date.format'), $value->getTimestamp()); |
||
| 127 | }); |
||
| 128 | } |
||
| 129 | |||
| 130 | return false; |
||
| 131 | }); |
||
| 132 | // components |
||
| 133 | Configuration::make($this->twig) |
||
| 134 | ->setTemplatesPath($this->builder->getConfig()->get('layouts.components.dir') ?? 'components') |
||
| 135 | ->setTemplatesExtension($this->builder->getConfig()->get('layouts.components.ext') ?? 'twig') |
||
| 136 | ->useCustomTags() |
||
| 137 | ->setup(); |
||
| 138 | // cache |
||
| 139 | $this->twig->addExtension(new CacheExtension()); |
||
| 140 | $this->twig->addRuntimeLoader(new class () implements RuntimeLoaderInterface { |
||
| 141 | public function load($class) |
||
| 142 | { |
||
| 143 | if (CacheRuntime::class === $class) { |
||
| 144 | return new CacheRuntime(new TagAwareAdapter(new FilesystemAdapter())); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | }); |
||
| 148 | // debug |
||
| 149 | if ($this->builder->isDebug()) { |
||
| 150 | // dump() |
||
| 151 | $this->twig->addExtension(new \Twig\Extension\DebugExtension()); |
||
| 152 | // profiler |
||
| 153 | $this->profile = new \Twig\Profiler\Profile(); |
||
| 154 | $this->twig->addExtension(new \Twig\Extension\ProfilerExtension($this->profile)); |
||
| 155 | } |
||
| 156 | // loads custom extensions |
||
| 157 | if ($this->builder->getConfig()->has('layouts.extensions')) { |
||
| 158 | foreach ((array) $this->builder->getConfig()->get('layouts.extensions') as $name => $class) { |
||
| 159 | try { |
||
| 160 | $this->twig->addExtension(new $class($this->builder)); |
||
| 161 | $this->builder->getLogger()->debug(\sprintf('Twig extension "%s" added', $name)); |
||
| 162 | } catch (RuntimeException | \Error $e) { |
||
| 163 | $this->builder->getLogger()->error(\sprintf('Unable to add Twig extension "%s": %s', $name, $e->getMessage())); |
||
| 164 | } |
||
| 233 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.