Scan files in a directory to get the number of methods and PHP classes in a directory
Clash Royale CLAN TAG#URR8PPP
Scan files in a directory to get the number of methods and PHP classes in a directory
Im trying to get the number of class and methods in a specific directory which contain sub folder and scan through them. So far I can only count the number of files.
$ite=new RecursiveDirectoryIterator("scanME");
//keyword search
$classWords = array('class');
$functionWords = array('function');
//Global Counts
$bytestotal=0;
$nbfiles=0;
$classCount = 0;
$methodCount = 0;
foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur)
$filesize=$cur->getSize();
$bytestotal+=$filesize;
if(is_file($cur))
$nbfiles++;
foreach ($classWords as $classWord)
$fileContents = file_get_contents($cur);
$place = strpos($fileContents, $classWord);
if (!empty($place))
$classCount++;
foreach($functionWords as $functionWord)
$fileContents = file_get_contents($cur);
$place = strpos($fileContents, $functionWord);
if (!empty($place))
$methodCount++;
EDIT: I manage to count the keyword class and function but the problem is it only concatenate for each file. Eg: I have 2 class in one file it will just count 1. How do I count for each keyword in a file?
<?php
That would count as well. I dont know for sure if strpos works Im having trouble with file_get_contents since what im tring to search for is a folder not just a specific file.
– ProgrammerDummy
Aug 6 at 1:44
2 Answers
2
The only time you define $classContents is at the top where you're attempting to get the contents of the directory:
$classContents = file_get_contents('scanMeDir');
You should be getting the contents of each file while looping through the RecursiveDirectoryIterator results. (You also don't need to create a new iterator instance):
foreach ($ite as $filename => $cur)
$classContents = file_get_contents($filename);
...
using token instead of keyword is the better solution for this
$bytestotal=0;
$nbfiles=0;
$fileToString;
$token;
$pathInfo;
$classCount = 0;
$methodCount = 0;
foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur)
$filesize=$cur->getSize();
$bytestotal+=$filesize;
if(is_file($cur))
$nbfiles++;
$fileToString = file_get_contents($cur);
$token = token_get_all($fileToString);
$tokenCount = count($token);
//Class Count
$pathInfo = pathinfo($cur);
if ($pathInfo['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 )
$classCount++;
else
error_reporting(E_ALL & ~E_NOTICE);
//Method Count
for ($i = 2; $i < $tokenCount; $i++)
if ($token[$i-2][0] === T_FUNCTION && $token[$i-1][0] === T_WHITESPACE && $token[$i][0] === T_STRING)
$methodCount++;
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What about files that have more than one class? How accurate do you need this? While your use of strpos's result this time will work because of the
<?php
, be careful with functions that return indexes, strpos could return a successful result of 0.– Devon
Aug 6 at 1:32