| Total Lines | 53 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 0 | 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 |
||
| 81 | 4 | public function render(string $template, array $parameters): string |
|
| 82 | { |
||
| 83 | 4 | $source = self::loadHtml($template) ?? $template; |
|
| 84 | |||
| 85 | 4 | if ($source !== $template || !\file_exists($template)) { |
|
| 86 | $loader = new Twig\Loader\ArrayLoader([$template => $source]); |
||
| 87 | } else { |
||
| 88 | 4 | $loader = new class ($this->loader) extends Twig\Loader\FilesystemLoader { |
|
|
|
|||
| 89 | /** @var Template */ |
||
| 90 | private $loader; |
||
| 91 | |||
| 92 | public function __construct(Template $loader, $paths = [], string $rootPath = null) |
||
| 93 | { |
||
| 94 | 4 | $this->loader = $loader; |
|
| 95 | 4 | parent::__construct($paths, $rootPath); |
|
| 96 | } |
||
| 97 | |||
| 98 | protected function findTemplate(string $name, bool $throw = true): ?string |
||
| 99 | { |
||
| 100 | 4 | if (isset($this->cache[$name])) { |
|
| 101 | 3 | return $this->cache[$name]; |
|
| 102 | } |
||
| 103 | |||
| 104 | 4 | if (isset($this->errorCache[$name])) { |
|
| 105 | if (!$throw) { |
||
| 106 | return null; |
||
| 107 | } |
||
| 108 | |||
| 109 | throw new Twig\Error\LoaderError($this->errorCache[$name]); |
||
| 110 | } |
||
| 111 | |||
| 112 | 4 | if (!\is_file($name)) { |
|
| 113 | $newName = $this->loader->find($name); |
||
| 114 | |||
| 115 | if (null === $newName) { |
||
| 116 | $this->errorCache[$name] = \sprintf('The template "%s" is not a valid file path.', $name); |
||
| 117 | |||
| 118 | if (!$throw) { |
||
| 119 | return null; |
||
| 120 | } |
||
| 121 | |||
| 122 | throw new Twig\Error\LoaderError($this->errorCache[$name]); |
||
| 123 | } |
||
| 124 | $name = $newName; |
||
| 125 | } |
||
| 126 | |||
| 127 | 4 | return $this->cache[$name] = $name; |
|
| 128 | } |
||
| 129 | 4 | }; |
|
| 130 | } |
||
| 131 | 4 | $this->addLoader($loader); |
|
| 132 | |||
| 133 | 4 | return $this->environment->load($template)->render($parameters); |
|
| 134 | } |
||
| 198 |