瀏覽代碼

Merge pull request #203 from spiasecki/feature/code_cleaning

Few code cleaning, episode 2
Justin Hileman 11 年之前
父節點
當前提交
77f98ad864
共有 6 個文件被更改,包括 45 次插入38 次删除
  1. 9 9
      bin/build_bootstrap.php
  2. 18 13
      bin/create_example.php
  3. 1 1
      src/Mustache/Engine.php
  4. 10 8
      src/Mustache/HelperCollection.php
  5. 4 4
      src/Mustache/Tokenizer.php
  6. 3 3
      test/bootstrap.php

+ 9 - 9
bin/build_bootstrap.php

@@ -20,13 +20,13 @@
  * containing all Mustache library classes. This file can then be included in
  * your project, rather than requiring the Mustache Autoloader.
  */
-$baseDir = realpath(dirname(__FILE__).'/..');
+$baseDir = realpath(dirname(__FILE__) . '/..');
 
-require $baseDir.'/src/Mustache/Autoloader.php';
+require $baseDir . '/src/Mustache/Autoloader.php';
 Mustache_Autoloader::register();
 
 // delete the old file
-$file = $baseDir.'/mustache.php';
+$file = $baseDir . '/mustache.php';
 if (file_exists($file)) {
     unlink($file);
 }
@@ -77,7 +77,7 @@ SymfonyClassCollectionLoader::load(array(
  */
 class SymfonyClassCollectionLoader
 {
-    static private $loaded;
+    private static $loaded;
 
     const HEADER = <<<EOS
 <?php
@@ -102,7 +102,7 @@ EOS;
      *
      * @throws InvalidArgumentException When class can't be loaded
      */
-    static public function load(array $classes, $cacheDir, $name, $extension = '.php')
+    public static function load(array $classes, $cacheDir, $name, $extension = '.php')
     {
         // each $name can only be loaded once per PHP process
         if (isset(self::$loaded[$name])) {
@@ -121,9 +121,9 @@ EOS;
             $content .= preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($r->getFileName()));
         }
 
-        $cache  = $cacheDir.'/'.$name.$extension;
+        $cache  = $cacheDir . '/' . $name . $extension;
         $header = sprintf(self::HEADER, strftime('%Y'));
-        self::writeCacheFile($cache, $header . substr(self::stripComments('<?php '.$content), 5));
+        self::writeCacheFile($cache, $header . substr(self::stripComments('<?php ' . $content), 5));
     }
 
     /**
@@ -134,7 +134,7 @@ EOS;
      *
      * @throws RuntimeException when a cache file cannot be written
      */
-    static private function writeCacheFile($file, $content)
+    private static function writeCacheFile($file, $content)
     {
         $tmpFile = tempnam(dirname($file), basename($file));
         if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
@@ -156,7 +156,7 @@ EOS;
      *
      * @return string The PHP string with the comments removed
      */
-    static private function stripComments($source)
+    private static function stripComments($source)
     {
         if (!function_exists('token_get_all')) {
             return $source;

+ 18 - 13
bin/create_example.php

@@ -24,7 +24,6 @@ USAGE
 
 define('EXAMPLE_PATH', realpath(dirname(__FILE__) . '/../test/fixtures/examples'));
 
-
 /**
  * transform a string to lowercase using underlines.
  * Examples:
@@ -37,7 +36,8 @@ define('EXAMPLE_PATH', realpath(dirname(__FILE__) . '/../test/fixtures/examples'
  * @access public
  * @return string
  */
-function getLowerCaseName($name) {
+function getLowerCaseName($name)
+{
     return preg_replace_callback("/([A-Z])/", create_function (
         '$match',
         'return "_" . strtolower($match[1]);'
@@ -56,14 +56,14 @@ function getLowerCaseName($name) {
  * @access public
  * @return string
  */
-function getUpperCaseName($name) {
+function getUpperCaseName($name)
+{
     return preg_replace_callback("/_([a-z])/", create_function (
         '$match',
         'return strtoupper($match{1});'
     ), ucfirst($name));
 }
 
-
 /**
  * return the given value and echo it out appending "\n"
  *
@@ -71,8 +71,10 @@ function getUpperCaseName($name) {
  * @access public
  * @return mixed
  */
-function out($value) {
+function out($value)
+{
     echo $value . "\n";
+
     return $value;
 }
 
@@ -88,7 +90,8 @@ function out($value) {
  * @access public
  * @return string
  */
-function buildPath($directory, $filename = null,  $extension = null) {
+function buildPath($directory, $filename = null,  $extension = null)
+{
     return out(EXAMPLE_PATH . '/' . $directory.
                     ($extension !== null && $filename !== null ? '/' . $filename. "." . $extension : ""));
 }
@@ -101,8 +104,9 @@ function buildPath($directory, $filename = null,  $extension = null) {
  * @access public
  * @return void
  */
-function createDirectory($directory) {
-    if(!@mkdir(buildPath($directory))) {
+function createDirectory($directory)
+{
+    if (!@mkdir(buildPath($directory))) {
         die("FAILED to create directory\n");
     }
 }
@@ -118,9 +122,10 @@ function createDirectory($directory) {
  * @access public
  * @return void
  */
-function createFile($directory, $filename, $extension, $content = "") {
+function createFile($directory, $filename, $extension, $content = "")
+{
     $handle = @fopen(buildPath($directory, $filename, $extension), "w");
-    if($handle) {
+    if ($handle) {
         fwrite($handle, $content);
         fclose($handle);
     } else {
@@ -128,7 +133,6 @@ function createFile($directory, $filename, $extension, $content = "") {
     }
 }
 
-
 /**
  * routine to create the example directory and 3 files
  *
@@ -142,7 +146,8 @@ function createFile($directory, $filename, $extension, $content = "") {
  * @access public
  * @return void
  */
-function main($example_name) {
+function main($example_name)
+{
     $lowercase = getLowerCaseName($example_name);
     $uppercase = getUpperCaseName($example_name);
     createDirectory($lowercase);
@@ -160,7 +165,7 @@ CONTENT
 }
 
 // check if enougth arguments are given
-if(count($argv) > 1) {
+if (count($argv) > 1) {
     // get the name of the example
     $example_name = $argv[1];
 

+ 1 - 1
src/Mustache/Engine.php

@@ -213,7 +213,7 @@ class Mustache_Engine
      */
     public function getEntityFlags()
     {
-      return $this->entityFlags;
+        return $this->entityFlags;
     }
 
     /**

+ 10 - 8
src/Mustache/HelperCollection.php

@@ -27,14 +27,16 @@ class Mustache_HelperCollection
      */
     public function __construct($helpers = null)
     {
-        if ($helpers !== null) {
-            if (!is_array($helpers) && !$helpers instanceof Traversable) {
-                throw new Mustache_Exception_InvalidArgumentException('HelperCollection constructor expects an array of helpers');
-            }
-
-            foreach ($helpers as $name => $helper) {
-                $this->add($name, $helper);
-            }
+        if ($helpers === null) {
+            return;
+        }
+
+        if (!is_array($helpers) && !$helpers instanceof Traversable) {
+            throw new Mustache_Exception_InvalidArgumentException('HelperCollection constructor expects an array of helpers');
+        }
+
+        foreach ($helpers as $name => $helper) {
+            $this->add($name, $helper);
         }
     }
 

+ 4 - 4
src/Mustache/Tokenizer.php

@@ -116,7 +116,7 @@ class Mustache_Tokenizer
                     } else {
                         $char = $text[$i];
                         $this->buffer .= $char;
-                        if ($char == "\n") {
+                        if ($char === "\n") {
                             $this->flushBuffer();
                             $this->line++;
                         }
@@ -157,14 +157,14 @@ class Mustache_Tokenizer
                             self::OTAG  => $this->otag,
                             self::CTAG  => $this->ctag,
                             self::LINE  => $this->line,
-                            self::INDEX => ($this->tagType == self::T_END_SECTION) ? $this->seenTag - $this->otagLen : $i + $this->ctagLen
+                            self::INDEX => ($this->tagType === self::T_END_SECTION) ? $this->seenTag - $this->otagLen : $i + $this->ctagLen
                         );
 
                         $this->buffer = '';
                         $i += $this->ctagLen - 1;
                         $this->state = self::IN_TEXT;
-                        if ($this->tagType == self::T_UNESCAPED) {
-                            if ($this->ctag == '}}') {
+                        if ($this->tagType === self::T_UNESCAPED) {
+                            if ($this->ctag === '}}') {
                                 $i++;
                             } else {
                                 // Clean up `{{{ tripleStache }}}` style tokens.

+ 3 - 3
test/bootstrap.php

@@ -9,8 +9,8 @@
  * file that was distributed with this source code.
  */
 
-require dirname(__FILE__).'/../src/Mustache/Autoloader.php';
+require dirname(__FILE__) . '/../src/Mustache/Autoloader.php';
 Mustache_Autoloader::register();
-Mustache_Autoloader::register(dirname(__FILE__).'/../test');
+Mustache_Autoloader::register(dirname(__FILE__) . '/../test');
 
-require dirname(__FILE__).'/../vendor/yaml/lib/sfYamlParser.php';
+require dirname(__FILE__) . '/../vendor/yaml/lib/sfYamlParser.php';