Пример #1
0
        public FileStatistics CollectInfo(string fileName)
        {
            var lexer      = new PhpAntlrLexer();
            var sourceFile = new TextFile(File.ReadAllText(fileName))
            {
                Name = fileName
            };
            var tokens = lexer.GetTokens(sourceFile, out _);

            var parser = new PhpAntlrParser();

            parser.Logger     = Logger;
            parser.SourceFile = sourceFile;
            PhpAntlrParseTree ust = (PhpAntlrParseTree)parser.Parse(tokens, out _);

            classUsings.Clear();
            methodInvocations.Clear();
            includes.Clear();

            Visit(ust.SyntaxTree);

            var result = new FileStatistics
            {
                FileName          = fileName,
                ClassUsings       = new Dictionary <string, int>(classUsings),
                MethodInvocations = new Dictionary <string, int>(methodInvocations),
                Includes          = new Dictionary <string, int>(includes)
            };

            return(result);
        }
Пример #2
0
 private void CollectPhpStatistics(PhpStatistics phpStatistics, string filePath)
 {
     try
     {
         var fileName = Path.GetFileName(filePath).ToLowerInvariant();
         if (fileName == "composer.json" || fileName == "composer.lock" || fileName == "php.ini")
         {
             var    lines = File.ReadAllLines(filePath);
             string content;
             if (fileName == "php.ini")
             {
                 content = string.Join(Environment.NewLine, lines.Where(line =>
                                                                        line.StartsWith("extension") || line.StartsWith("zend_extension")));
             }
             else
             {
                 content = string.Join(Environment.NewLine, lines.Where(line =>
                                                                        line.StartsWith("require") || line.StartsWith("require-dev") ||
                                                                        line.StartsWith("packages") || line.StartsWith("packages-dev")));
             }
             phpStatistics.FilesContent[fileName] = content;
         }
         else if (filePath.ToLowerInvariant().EndsWith(".htaccess"))
         {
             var lines = File.ReadAllLines(filePath);
             foreach (var line in lines)
             {
                 if (line.StartsWith("php_value") || line.StartsWith("php_flag") || line.StartsWith("RewriteCond") || line.StartsWith("RewriteRule"))
                 {
                     phpStatistics.HtaccessStrings.Add(line);
                 }
             }
         }
         else if (filePath.EndsWith(".php"))
         {
             var fileCollector = new PhpInfoCollectorVisitor();
             fileCollector.Logger = Logger;
             FileStatistics fileStat = fileCollector.CollectInfo(filePath);
             foreach (var classUsing in fileStat.ClassUsings)
             {
                 int count = 0;
                 phpStatistics.ClassUsings.TryGetValue(classUsing.Key, out count);
                 phpStatistics.ClassUsings[classUsing.Key] = count + classUsing.Value;
             }
             foreach (var invoke in fileStat.MethodInvocations)
             {
                 int count = 0;
                 phpStatistics.MethodInvocations.TryGetValue(invoke.Key, out count);
                 phpStatistics.MethodInvocations[invoke.Key] = count + invoke.Value;
             }
             foreach (var include in fileStat.Includes)
             {
                 int count = 0;
                 phpStatistics.Includes.TryGetValue(include.Key, out count);
                 phpStatistics.Includes[include.Key] = count + include.Value;
             }
         }
     }
     catch (Exception ex)
     {
         Logger?.LogInfo(new ErrorMessage(ex.ToString()));
     }
 }