Skip to content

Commit

Permalink
Merge pull request #26 from mborne/25-modernize
Browse files Browse the repository at this point in the history
Remove support for PHP 7.4 and modernize code
  • Loading branch information
mborne authored Mar 16, 2024
2 parents 35edea8 + e7107bd commit e85aacf
Show file tree
Hide file tree
Showing 42 changed files with 577 additions and 677 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
php-version: [7.4,8.1,8.2]
php-version: [8.1,8.2]

runs-on: ubuntu-latest

Expand Down
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PHP_CS_RULES=@Symfony,-ordered_imports,-phpdoc_summary,-global_namespace_import,-no_superfluous_phpdoc_tags
PHP_CS_RULES=@Symfony,-global_namespace_import
PHP_MD_RULES=cleancode,codesize,controversial,design,naming,unusedcode

.PHONY: test
Expand All @@ -10,10 +10,14 @@ test: check-style check-rules
--coverage-html output/coverage

.PHONY: check-rules
check-rules: vendor
check-rules: phpstan phpmd

phpmd:
@echo "-- Checking coding rules using phpmd (see @SuppressWarning to bypass control)"
vendor/bin/phpmd src text $(PHP_MD_RULES)

phpstan:
vendor/bin/phpstan analyse -c phpstan.neon --error-format=raw

.PHONY: fix-style
fix-style: vendor
Expand All @@ -25,8 +29,8 @@ fix-style: vendor
.PHONY: check-style
check-style: vendor
@echo "-- Checking coding style using php-cs-fixer (run 'make fix-style' if it fails)"
vendor/bin/php-cs-fixer fix src --rules $(PHP_CS_RULES) -v --dry-run --diff --using-cache=no
vendor/bin/php-cs-fixer fix tests --rules $(PHP_CS_RULES) -v --dry-run --diff --using-cache=no
vendor/bin/php-cs-fixer fix src --rules $(PHP_CS_RULES) -v --dry-run --diff
vendor/bin/php-cs-fixer fix tests --rules $(PHP_CS_RULES) -v --dry-run --diff

vendor:
composer install
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ This module is also used by [mborne/git-manager](https://github.com/mborne/git-m

## Requirements

* PHP >= 7.4 or >= 8.x
* [PHP >= 8.1](https://www.php.net/supported-versions.php)

## Supported GIT hosting services

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
}
},
"require": {
"php": ">=8.1",
"guzzlehttp/guzzle": "~7.0"
},
"require-dev": {
"phpunit/phpunit": "^9",
"friendsofphp/php-cs-fixer": "3.17.*",
"phpmd/phpmd": "^2.8",
"pdepend/pdepend": "2.15.*",
"php-coveralls/php-coveralls": "^2.5"
"php-coveralls/php-coveralls": "^2.5",
"phpstan/phpstan": "^1.10"
}
}
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 8
paths:
- src
- tests
44 changes: 19 additions & 25 deletions src/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace MBO\RemoteGit;

use Psr\Log\LoggerInterface;
use GuzzleHttp\Client as GuzzleHttpClient;
use MBO\RemoteGit\Helper\LoggerHelper;
use Psr\Log\LoggerInterface;

/**
* Abstract class providing a framework to implement clients
* based on API
* based on API.
*/
abstract class AbstractClient implements ClientInterface
{
Expand All @@ -23,7 +23,7 @@ abstract class AbstractClient implements ClientInterface
protected $logger;

/**
* Constructor with an httpClient ready to performs API requests
* Constructor with an httpClient ready to performs API requests.
*
* @param LoggerInterface $logger
*
Expand All @@ -37,38 +37,34 @@ protected function __construct(
$this->logger = LoggerHelper::handleNull($logger);
}

/**
* @return GuzzleHttpClient
*/
protected function getHttpClient()
protected function getHttpClient(): GuzzleHttpClient
{
return $this->httpClient;
}

/**
* @return LoggerInterface
*/
protected function getLogger()
protected function getLogger(): LoggerInterface
{
return $this->logger;
}

/**
* Create a project according to JSON metadata provided by an API
* Create a project according to JSON metadata provided by an API.
*
* @return ProjectInterface
* @param array<string,mixed> $rawProject
*/
abstract protected function createProject(array $rawProject);
abstract protected function createProject(array $rawProject): ProjectInterface;

/**
* Get projets for a given path with parameters
* Get projets for a given path with parameters.
*
* @param array<string,string|int> $params
*
* @return ProjectInterface[]
*/
protected function getProjects(
$path,
string $path,
array $params = []
) {
): array {
$uri = $path.'?'.$this->implodeParams($params);
$this->getLogger()->debug('GET '.$uri);
$response = $this->getHttpClient()->request('GET', $uri);
Expand All @@ -82,30 +78,28 @@ protected function getProjects(
}

/**
* Implode params to performs request
*
* @param array $params key=>value
* Implode params to performs HTTP request.
*
* @return string
* @param array<string,string|int> $params key=>value
*/
protected function implodeParams($params)
protected function implodeParams(array $params): string
{
$parts = [];
foreach ($params as $key => $value) {
$parts[] = $key.'='.urlencode($value);
$parts[] = $key.'='.urlencode((string) $value);
}

return implode('&', $parts);
}

/**
* Helper to apply filter to a project list
* Helper to apply filter to a project list.
*
* @param ProjectInterface[] $projects
*
* @return ProjectInterface[]
*/
protected function filter(array $projects, ProjectFilterInterface $filter)
protected function filter(array $projects, ProjectFilterInterface $filter): array
{
$result = [];
foreach ($projects as $project) {
Expand Down
54 changes: 23 additions & 31 deletions src/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace MBO\RemoteGit;

use Psr\Log\LoggerInterface;
use GuzzleHttp\Client as GuzzleHttpClient;
use MBO\RemoteGit\Exception\ClientNotFoundException;
use MBO\RemoteGit\Helper\LoggerHelper;
use MBO\RemoteGit\Http\TokenType;
use MBO\RemoteGit\Github\GithubClient;
use MBO\RemoteGit\Gitlab\GitlabClient;
use MBO\RemoteGit\Gogs\GogsClient;
use MBO\RemoteGit\Helper\ClientHelper;
use MBO\RemoteGit\Helper\LoggerHelper;
use MBO\RemoteGit\Http\TokenType;
use MBO\RemoteGit\Local\LocalClient;
use Psr\Log\LoggerInterface;

/**
* Helper to create clients according to URL.
Expand All @@ -28,9 +28,9 @@ class ClientFactory
private static $instance;

/**
* Associates client type to metadata ('className','tokenType')
* Associates client type to metadata ('className','tokenType').
*
* @var array
* @var array<string,array<string,string>>
*/
private $types = [];

Expand All @@ -43,7 +43,7 @@ private function __construct()
}

/**
* True if type is registred
* True if type is registred.
*
* @param string $type
*
Expand All @@ -55,7 +55,7 @@ public function hasType($type)
}

/**
* Get supported types
* Get supported types.
*
* @return string[]
*/
Expand All @@ -65,32 +65,24 @@ public function getTypes()
}

/**
* Create a client with options
*
* @param LoggerInterface $logger
*
* @return ClientInterface
* Create a client with options.
*/
public static function createClient(
ClientOptions $options,
LoggerInterface $logger = null
) {
): ClientInterface {
return self::getInstance()->createGitClient($options, $logger);
}

/**
* Create a client with options
*
* @param LoggerInterface $logger
*
* @return ClientInterface
* Create a client with options.
*
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function createGitClient(
ClientOptions $options,
LoggerInterface $logger = null
) {
): ClientInterface {
$logger = LoggerHelper::handleNull($logger);

/* Detect client type from URL if not specified */
Expand Down Expand Up @@ -141,27 +133,27 @@ public function createGitClient(
/* create http client */
$httpClient = new GuzzleHttpClient($guzzleOptions);
/* create git client */
return new $clientClass($httpClient, $logger);
$result = new $clientClass($httpClient, $logger);
assert($result instanceof ClientInterface);

return $result;
}

/**
* Get client class according to URL content
*
* @param string $url
*
* @return string
* Get client class according to URL content.
*/
public static function detectClientClass($url)
public static function detectClientClass(string $url): string
{
$scheme = parse_url($url, PHP_URL_SCHEME);
if (!in_array($scheme, ['http', 'https'])) {
return LocalClient::class;
}

$hostname = parse_url($url, PHP_URL_HOST);
assert('string' === gettype($hostname));
if ('api.github.com' === $hostname || 'github.com' === $hostname) {
return GithubClient::class;
} elseif (false !== strpos($hostname, 'gogs')) {
} elseif (str_contains($hostname, 'gogs')) {
return GogsClient::class;
}
/*
Expand All @@ -176,21 +168,21 @@ public static function detectClientClass($url)
*/
public static function getInstance()
{
if (is_null(self::$instance)) {
if (null == self::$instance) {
self::$instance = new ClientFactory();
}

return self::$instance;
}

/**
* Register client type
* Register client type.
*
* @param string $className
* @param class-string $className
*
* @SuppressWarnings(PHPMD.StaticAccess)
*/
private function register($className)
private function register(string $className): void
{
$clientProperties = ClientHelper::getStaticProperties($className);
$this->types[$clientProperties['typeName']] = $clientProperties;
Expand Down
22 changes: 10 additions & 12 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,29 @@

/**
* Lightweight client interface to list hosted git project
* and access files such as composer.json
* and access files such as composer.json.
*
* @author mborne
*/
interface ClientInterface
{
/**
* Find projects throw API
* Find projects throw API.
*
* @return ProjectInterface[]
*/
public function find(FindOptions $options);
public function find(FindOptions $options): array;

/**
* Get raw file
* Get raw file.
*
* @param string $projectId ex : 123456
* @param string $filePath ex : composer.json
* @param string $ref ex : master
*
* @return string
* @param ProjectInterface $project ex : 123456
* @param string $filePath ex : composer.json
* @param string $ref ex : master
*/
public function getRawFile(
ProjectInterface $project,
$filePath,
$ref
);
string $filePath,
string $ref
): string;
}
Loading

0 comments on commit e85aacf

Please sign in to comment.