Skip to content

Commit

Permalink
Merge pull request #12 from Oscabrera/Feature/PHPInsights
Browse files Browse the repository at this point in the history
Feature/php insights
  • Loading branch information
Oscabrera authored Jul 28, 2024
2 parents e35a19c + bbe4dff commit 56f9e50
Show file tree
Hide file tree
Showing 64 changed files with 1,320 additions and 888 deletions.
191 changes: 50 additions & 141 deletions ci-scripts/Analyzer/Analyzer.php
Original file line number Diff line number Diff line change
@@ -1,206 +1,115 @@
<?php

namespace Oscabrera\ModelRepository\CIScripts\Analyzer;
declare(strict_types=1);

require_once __DIR__ . '/AnalyzerSettings.php';
require_once __DIR__ . '/Constants/Color.php';
require_once __DIR__ . '/Constants/Icon.php';
namespace Oscabrera\ModelRepository\CIScripts\Analyzer;

use Exception;
use Oscabrera\ModelRepository\CIScripts\Analyzer\Constants\Color;
use Oscabrera\ModelRepository\CIScripts\Analyzer\Constants\Icon;
use RuntimeException;

/**
* Analyze with tool only modified files
*/
class Analyzer extends AnalyzerSettings
{
/**
* @var Color
*/
protected Color $color;

/**
* @var Icon
*/
protected Icon $icon;

/**
* Constructor for the class
*
*/
public function __construct()
{
$this->color = new Color();
$this->icon = new Icon();
}

/**
* Execute the analyzer
*
* @return bool Returns an exit status code. 0 indicates success (OK),
* and any other number indicates an error.
* Returns an exit status code. 0 indicates success (OK),
* and any other number indicates an error.
*/
public function analyze(): bool
{
try {
$modifiedFiles = $this->getFiles();
if (empty($modifiedFiles)) {
$this->echoColor(
'No files modified to analyze',
$this->color::get('BLUE'),
$this->icon::get('NO_FILES')
);
return true;
}
if (!$this->execute($modifiedFiles)) {
$this->echoColor(
"$this->tool analysis failed",
$this->color::get('RED'),
$this->icon::get('CRITICAL_ERROR')
);
return false;
}

$this->echoColor(
"$this->tool analysis succeeded",
$this->color::get('BLUE'),
$this->icon::get('SUCCESS')
);
return true;
} catch (RuntimeException|Exception $e) {
$this->echoColor($e->getMessage(), $this->color::get('RED'), $this->icon::get('EXCEPTION'));
return $this->executeAnalysis();
} catch (RuntimeException|Exception $exception) {
$this->reportExceptionThrown($exception);
return false;
}
}

/**
* Get files to analyze
* Execute the analysis and report the result.
*
* @return string Returns a string representation of the files to analyze.
* @throws Exception
*/
private function getFiles(): string
protected function executeAnalysis(): bool
{
$allFlag = in_array('--all', $this->args, true);
if ($allFlag) {
$this->echoColor('Executing analysis on .', $this->color::get('YELLOW'), $this->icon::get('STACK_TRACE'));
return '.';
$files = $this->getFiles();
if ($files === '') {
$this->reportNoFilesAnalyze();
return true;
}
if (!$this->execute($files)) {
$this->reportAnalysisFailed();
return false;
}
$dir = $this->getDirFlag();
return $dir ?? $this->getDiffBranch();
$this->reportAnalysisSucceeded();
return true;
}

/**
* Get the directory flag value from the command line arguments
* Get files to analyze
*
* Returns a string representation of the files to analyze.
*
* @return string|null Returns the directory flag value if found, null otherwise
* @throws Exception
*/
private function getDirFlag(): ?string
protected function getFiles(): string
{
foreach ($this->args as $arg) {
if (str_starts_with($arg, '--dir=')) {
$file = substr($arg, strlen('--dir='));
$this->echoColor(
'Executing analysis on ' . $file,
$this->color::get('YELLOW'),
$this->icon::get('STACK_TRACE')
);
return $file;
}
}
return null;
return $this->argumentHandler->shouldAnalyzeAll() ?
$this->analyzeAllFiles() :
$this->getDirectoryOrBranchFiles();
}

/**
* Returns an array of modified files in the git log.
* Analyze all files
*
* @return string An array of modified files
* @throws Exception
* This method runs the analysis process for all files.
*/
private function getDiffBranch(): string
protected function analyzeAllFiles(): string
{
$gitLog = shell_exec("git diff --name-only $this->branch 2>&1");
if ($gitLog === null) {
throw new RuntimeException('git log command failed');
}

return $this->getModifiedFiles(!$gitLog ? '' : $gitLog);
$this->echoAnalyzeAll();
return '.';
}

/**
* Returns an array of escaped shell arguments corresponding to modified files in the git log.
* Get the directory flag value from the command line arguments
*
* @param string $gitLog The git log containing a list of modified files
* @return string
* @throws Exception
*/
private function getModifiedFiles(string $gitLog): string
protected function getDirectoryOrBranchFiles(): string
{
$gitLogLines = array_filter(explode("\n", trim($gitLog)));
$escapedFiles = [];
foreach ($gitLogLines as $line) {
if (str_ends_with($line, ".$this->extension") && is_file($line)) {
$escapedFiles[] = escapeshellarg($line);
}
}
if (empty($escapedFiles)) {
return '';
$dir = $this->argumentHandler->getDirArg();
if ($dir === '') {
return $this->fileHandler->getModifiedFilesGit();
}

return implode(' ', $escapedFiles);
return $this->extractDirFromArg($dir);
}

/**
* Echoes a colored message to the console.
*
* @param string $message The message to be echoed
* @param string $color The color to be applied to the message
* @param string $icon
* @return void
* Extract the directory from the argument.
*/
private function echoColor(string $message, string $color, string $icon = ''): void
protected function extractDirFromArg(string $dir): string
{
echo "\n";
echo $icon . $color . ' ' . $message . $this->color::get('END');
echo "\n";
echo "\n";
$dirFiles = $this->fileHandler->getModifiedFilesComa($dir);
$this->reportAnalysisExecute($dirFiles);

return $dirFiles;
}

/**
* Executes analysis on the files.
*
* @param string $modifiedFiles The list of modified files passed as shell arguments
* @return bool Returns true if the analysis returns non-zero exit code, false otherwise
* Returns true if the analysis returns non-zero exit code, false otherwise
*/
private function execute(string $modifiedFiles): bool
protected function execute(string $modifiedFiles): bool
{
$additionalArgs = $this->getAdditionalArgs();
$this->echoColor(
str_replace('%FILES%', $modifiedFiles, $this->command) . implode(' ', $additionalArgs),
$this->color::get('GREEN'),
$this->icon::get('COMMAND')
);
$command = str_replace('%FILES%', $modifiedFiles, $this->command). ' ' . implode(' ', $additionalArgs);
passthru($command, $returnVar);
return $returnVar === 0;
}
echo $this->commandBuilder->buildCommand($modifiedFiles) . PHP_EOL;

/**
* Get the arguments for the analyzer
*
* @return array<int, string> Returns an array of arguments to be passed to the analyzer
*/
private function getAdditionalArgs(): array
{
$toRemove = ['--all'];
$prefixToRemove = '--dir=';
passthru($this->commandBuilder->buildCommand($modifiedFiles), $returnVar);

return array_filter(
$this->args,
static function ($arg) use ($toRemove, $prefixToRemove) {
return !in_array($arg, $toRemove) && !str_starts_with($arg, $prefixToRemove);
}
);
return $returnVar === 0;
}
}
Loading

0 comments on commit 56f9e50

Please sign in to comment.