如何使我的PHP代码可读而不重复?

时间:2019-05-02 06:20:13

标签: php

如您所见,我有一个modelScan.php文件

  

我有在功能/方法中重复的代码。如何使这段代码更具可读性

感谢您的好意。谢谢

<?php
/**
 * Class for Scanning files
 */
class modelScan
{
    /**
     * Get the total size of file in the directory
     * @param string $sPath and integer $iTotalCount
     */
    public function getBytesTotal($sPath, $iTotalCount)
    {
        $sPath = realpath($sPath);
        if($sPath!==false && $sPath!='' && file_exists($sPath)){
            foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sPath, FilesystemIterator::SKIP_DOTS)) as $oObject){
                $iTotalCount += $oObject->getSize();
            }
        }
        return $iTotalCount;
    }

    /**
     * Get the total files in the directory
     * @param string $sPath and integer $iTotalCount
     */
    public function getFilesTotal($sPath, $iTotalCount)
    {
        $ite = new RecursiveDirectoryIterator($sPath);
        foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) {
            if(is_file($cur)){
                $iTotalCount++;   
            }           
        }
        return $iTotalCount;
    }

    /**
     * Get the total Class in the directory
     * @param string $sPath and integer $iTotalCount
     */
    public function getClassTotal($sPath, $iTotalCount)
    {
        $ite = new RecursiveDirectoryIterator($sPath);
        foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) {          
            if(is_file($cur)){
                $fileToString = file_get_contents($cur);
                $token = token_get_all($fileToString);
                $tokenCount = count($token);

                //Class Count
                $sPathInfo = pathinfo($cur);
                if ($sPathInfo['extension'] === 'php') {
                    for ($i = 2; $i < $tokenCount; $i++) {
                        if ($token[$i-2][0] === T_CLASS && $token[$i-1][0] === T_WHITESPACE && $token[$i][0] === T_STRING ) {
                        $iTotalCount++;
                        }
                    }
                } else {
                    error_reporting(E_ALL & ~E_NOTICE);
                }               
            }
        }
        return $iTotalCount;
    }

    /**
     * Get the total Method in the directory
     * @param string $sPath and integer $iTotalCount
     */
    public function getMethodTotal($sPath, $iTotalCount)
    {
        $ite = new RecursiveDirectoryIterator($sPath);
        foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) {         
            if(is_file($cur)){ 
                $fileToString = file_get_contents($cur);
                $token = token_get_all($fileToString);
                $tokenCount = count($token);

                //Method Count 
                $sPathInfo = pathinfo($cur);
                if ($sPathInfo['extension'] === 'php') {
                    for ($i = 2; $i < $tokenCount; $i++) {
                        if ($token[$i-2][0] === T_FUNCTION) {
                                $iTotalCount++;
                        }
                    }
                } else {
                    error_reporting(E_ALL & ~E_NOTICE);
                }               
            }
        }       
        return $iTotalCount;
    }


}

0 个答案:

没有答案
相关问题