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