Refactor ProjectType handling

This commit is contained in:
netkas 2024-09-17 22:08:23 -04:00
parent 1bcfe90bea
commit d0e484631f
3 changed files with 11 additions and 18 deletions

View file

@ -798,10 +798,9 @@
throw new OperationException(sprintf('Failed to detect project type from source %s: %s', $archive, $e->getMessage()), $e); throw new OperationException(sprintf('Failed to detect project type from source %s: %s', $archive, $e->getMessage()), $e);
} }
// TODO: getProjectType() could return the enum case
switch($project_detection->getProjectType()) switch($project_detection->getProjectType())
{ {
case ProjectType::NCC->value: case ProjectType::NCC:
try try
{ {
$package_path = (new ProjectManager($project_detection->getProjectFilePath()))->build( $package_path = (new ProjectManager($project_detection->getProjectFilePath()))->build(
@ -824,7 +823,7 @@
throw new OperationException(sprintf('Failed to build from source %s: %s', $archive, $e->getMessage()), $e); throw new OperationException(sprintf('Failed to build from source %s: %s', $archive, $e->getMessage()), $e);
} }
case ProjectType::COMPOSER->value: case ProjectType::COMPOSER:
try try
{ {
$project_manager = ProjectManager::initializeFromComposer(dirname($project_detection->getProjectFilePath()), $options); $project_manager = ProjectManager::initializeFromComposer(dirname($project_detection->getProjectFilePath()), $options);

View file

@ -32,27 +32,21 @@
/** /**
* @var string * @var string
*/ */
private $project_file_path; private string $project_file_path;
/** /**
* @see ProjectType * @var ProjectType
* @var string
*/ */
private $project_type; private ProjectType $project_type;
/** /**
* ProjectDetectionResults Constructor * ProjectDetectionResults Constructor
* *
* @param string $project_file_path * @param string $project_file_path
* @param string $project_type * @param ProjectType $project_type
*/ */
public function __construct(string $project_file_path, string $project_type) public function __construct(string $project_file_path, ProjectType $project_type)
{ {
if(!in_array($project_type, ProjectType::ALL))
{
throw new InvalidArgumentException(sprintf('Invalid project type "%s"', $project_type));
}
$this->project_file_path = $project_file_path; $this->project_file_path = $project_file_path;
$this->project_type = $project_type; $this->project_type = $project_type;
} }
@ -70,9 +64,9 @@
/** /**
* Returns the detected project type * Returns the detected project type
* *
* @return string * @return ProjectType
*/ */
public function getProjectType(): string public function getProjectType(): ProjectType
{ {
return $this->project_type; return $this->project_type;
} }

View file

@ -173,12 +173,12 @@
{ {
if(str_ends_with($file, 'project.json')) if(str_ends_with($file, 'project.json'))
{ {
return new ProjectDetectionResults($file, ProjectType::NCC->value); return new ProjectDetectionResults($file, ProjectType::NCC);
} }
if(str_ends_with($file, 'composer.json')) if(str_ends_with($file, 'composer.json'))
{ {
return new ProjectDetectionResults($file, ProjectType::COMPOSER->value); return new ProjectDetectionResults($file, ProjectType::COMPOSER);
} }
} }