Conditions | 9 |
Paths | 16 |
Total Lines | 56 |
Code Lines | 30 |
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 |
||
80 | protected function getTemplate( $templateName ) { |
||
81 | // If a renderer has already been defined for this template, reuse it |
||
82 | if ( isset( $this->renderers[$templateName] ) && |
||
83 | is_callable( $this->renderers[$templateName] ) |
||
84 | ) { |
||
85 | return $this->renderers[$templateName]; |
||
86 | } |
||
87 | |||
88 | $filename = $this->getTemplateFilename( $templateName ); |
||
89 | |||
90 | if ( !file_exists( $filename ) ) { |
||
91 | throw new RuntimeException( "Could not locate template: {$filename}" ); |
||
92 | } |
||
93 | |||
94 | // Read the template file |
||
95 | $fileContents = file_get_contents( $filename ); |
||
96 | |||
97 | // Generate a quick hash for cache invalidation |
||
98 | $fastHash = md5( $fileContents ); |
||
99 | |||
100 | // Fetch a secret key for building a keyed hash of the PHP code |
||
101 | $config = ConfigFactory::getDefaultInstance()->makeConfig( 'main' ); |
||
102 | $secretKey = $config->get( 'SecretKey' ); |
||
103 | |||
104 | if ( $secretKey ) { |
||
105 | // See if the compiled PHP code is stored in cache. |
||
106 | $cache = ObjectCache::getLocalServerInstance( CACHE_ANYTHING ); |
||
107 | $key = $cache->makeKey( 'template', $templateName, $fastHash ); |
||
108 | $code = $this->forceRecompile ? null : $cache->get( $key ); |
||
109 | |||
110 | if ( !$code ) { |
||
111 | $code = $this->compileForEval( $fileContents, $filename ); |
||
112 | |||
113 | // Prefix the cached code with a keyed hash (64 hex chars) as an integrity check |
||
114 | $cache->set( $key, hash_hmac( 'sha256', $code, $secretKey ) . $code ); |
||
115 | } else { |
||
116 | // Verify the integrity of the cached PHP code |
||
117 | $keyedHash = substr( $code, 0, 64 ); |
||
118 | $code = substr( $code, 64 ); |
||
119 | if ( $keyedHash !== hash_hmac( 'sha256', $code, $secretKey ) ) { |
||
120 | // Generate a notice if integrity check fails |
||
121 | trigger_error( "Template failed integrity check: {$filename}" ); |
||
122 | } |
||
123 | } |
||
124 | // If there is no secret key available, don't use cache |
||
125 | } else { |
||
126 | $code = $this->compileForEval( $fileContents, $filename ); |
||
127 | } |
||
128 | |||
129 | $renderer = eval( $code ); |
||
|
|||
130 | if ( !is_callable( $renderer ) ) { |
||
131 | throw new RuntimeException( "Requested template, {$templateName}, is not callable" ); |
||
132 | } |
||
133 | $this->renderers[$templateName] = $renderer; |
||
134 | return $renderer; |
||
135 | } |
||
136 | |||
205 |
On one hand,
eval
might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM,eval
prevents some optimization that they perform.