Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Implement linter #6

Merged
merged 2 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "Linter"

on: [pull_request]
jobs:
lint:
name: Linter
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 2

- run: git checkout HEAD^2

- name: Run Linter
run: |
docker run --rm -v $PWD:/app composer sh -c \
"composer install --profile --ignore-platform-reqs && composer lint"
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"autoload-dev": {
"psr-4": {"Utopia\\Tests\\": "tests/Transfer"}
},
"scripts": {
"lint": "./vendor/bin/pint --test",
"format": "./vendor/bin/pint"
},
"require": {
"php": ">=8.0",
"utopia-php/cli": "^0.13.0",
Expand Down
4 changes: 2 additions & 2 deletions playground.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* A place to test and debug the Transfer Library stuff
*/
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__.'/vendor/autoload.php';

use Appwrite\Query;
use Dotenv\Dotenv;
Expand Down Expand Up @@ -65,7 +65,7 @@
// $_ENV['DESTINATION_APPWRITE_TEST_KEY']
// );

$destinationLocal = new Local(__DIR__ . '/localBackup/');
$destinationLocal = new Local(__DIR__.'/localBackup/');

/**
* Initialise Transfer Class
Expand Down
30 changes: 15 additions & 15 deletions src/Transfer/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Cache stores a local version of all data copied over from the source, This can be used as reference point for
* previous transfers and also help the destination to determine what needs to be updated, modified,
* previous transfers and also help the destination to determine what needs to be updated, modified,
* added or removed. It is also used for debugging and validation purposes.
*/
class Cache
Expand All @@ -18,15 +18,15 @@ public function __construct()

/**
* Add Resource
*
*
* Places the resource in the cache, in the cache backend this also gets assigned a unique ID.
*
* @param Resource $resource
*
* @param resource $resource
* @return void
*/
public function add($resource)
{
if (!$resource->getInternalId()) {
if (! $resource->getInternalId()) {
$resourceId = uniqid();
if (isset($this->cache[$resource->getName()][$resourceId])) {
$resourceId = uniqid();
Expand All @@ -45,16 +45,16 @@ public function addAll(array $resources)

/**
* Update Resource
*
*
* Updates the resource in the cache, if the resource does not exist in the cache an exception is thrown.
* Use Add to add a new resource to the cache.
*
* @param Resource $resource
*
* @param resource $resource
* @return void
*/
public function update($resource)
{
if (!in_array($resource, $this->cache[$resource->getName()])) {
if (! in_array($resource, $this->cache[$resource->getName()])) {
throw new \Exception('Resource does not exist in cache');
}

Expand All @@ -70,15 +70,15 @@ public function updateAll($resources)

/**
* Remove Resource
*
*
* Removes the resource from the cache, if the resource does not exist in the cache an exception is thrown.
*
* @param Resource $resource
*
* @param resource $resource
* @return void
*/
public function remove($resource)
{
if (!in_array($resource, $this->cache[$resource->getName()])) {
if (! in_array($resource, $this->cache[$resource->getName()])) {
throw new \Exception('Resource does not exist in cache');
}

Expand Down Expand Up @@ -112,9 +112,9 @@ public function getAll()

/**
* Wipe Cache
*
*
* Removes all resources from the cache.
*
*
* @return void
*/
public function wipe()
Expand Down
31 changes: 14 additions & 17 deletions src/Transfer/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
use Utopia\Transfer\Resources\Auth\Membership;
use Utopia\Transfer\Resources\Auth\User;
use Utopia\Transfer\Resources\Database\Attribute;
use Utopia\Transfer\Resources\Database\Attributes\Boolean;
use Utopia\Transfer\Resources\Database\Attributes\DateTime;
use Utopia\Transfer\Resources\Database\Attributes\Decimal;
use Utopia\Transfer\Resources\Database\Attributes\Email;
use Utopia\Transfer\Resources\Database\Attributes\Enum;
use Utopia\Transfer\Resources\Database\Attributes\Decimal;
use Utopia\Transfer\Resources\Database\Attributes\Integer;
use Utopia\Transfer\Resources\Database\Attributes\IP;
use Utopia\Transfer\Resources\Database\Attributes\Relationship;
use Utopia\Transfer\Resources\Database\Attributes\Text;
Expand Down Expand Up @@ -66,7 +64,7 @@ public static function getName(): string
/**
* Get Supported Resources
*/
static function getSupportedResources(): array
public static function getSupportedResources(): array
{
return [
// Auth
Expand Down Expand Up @@ -216,7 +214,7 @@ public function report(array $resources = []): array
protected function import(array $resources, callable $callback): void
{
foreach ($resources as $resource) {
/** @var Resource $resource */
/** @var resource $resource */
switch ($resource->getGroup()) {
case Transfer::GROUP_DATABASES:
$responseResource = $this->importDatabaseResource($resource);
Expand Down Expand Up @@ -307,15 +305,15 @@ public function createAttribute(Attribute $attribute): void
$databaseService->createStringAttribute($attribute->getCollection()->getDatabase()->getId(), $attribute->getCollection()->getId(), $attribute->getKey(), $attribute->getSize(), $attribute->getRequired(), $attribute->getDefault(), $attribute->getArray());
break;
case Attribute::TYPE_INTEGER:
/** @var Integer $attribute */
/** @var int $attribute */
$databaseService->createIntegerAttribute($attribute->getCollection()->getDatabase()->getId(), $attribute->getCollection()->getId(), $attribute->getKey(), $attribute->getRequired(), $attribute->getMin(), $attribute->getMax() ?? null, $attribute->getDefault(), $attribute->getArray());
break;
case Attribute::TYPE_FLOAT:
/** @var Decimal $attribute */
$databaseService->createFloatAttribute($attribute->getCollection()->getDatabase()->getId(), $attribute->getCollection()->getId(), $attribute->getKey(), $attribute->getRequired(), null, null, $attribute->getDefault(), $attribute->getArray());
break;
case Attribute::TYPE_BOOLEAN:
/** @var Boolean $attribute */
/** @var bool $attribute */
$databaseService->createBooleanAttribute($attribute->getCollection()->getDatabase()->getId(), $attribute->getCollection()->getId(), $attribute->getKey(), $attribute->getRequired(), $attribute->getDefault(), $attribute->getArray());
break;
case Attribute::TYPE_DATETIME:
Expand Down Expand Up @@ -412,7 +410,6 @@ public function importFileResource(Resource $resource): Resource
/**
* Import File Data
*
* @param File $file
*
* @returns File
*/
Expand Down Expand Up @@ -501,7 +498,7 @@ public function importAuthResource(Resource $resource): Resource
}

if ($resource->getDisabled()) {
$userService->updateStatus($resource->getId(), !$resource->getDisabled());
$userService->updateStatus($resource->getId(), ! $resource->getDisabled());
}

break;
Expand All @@ -525,7 +522,7 @@ public function importAuthResource(Resource $resource): Resource
}
}

public function importPasswordUser(User $user): array|null
public function importPasswordUser(User $user): ?array
{
$auth = new Users($this->client);
$hash = $user->getPasswordHash();
Expand All @@ -544,23 +541,23 @@ public function importPasswordUser(User $user): array|null
$hash->getSalt(),
$hash->getSeparator(),
$hash->getSigningKey(),
empty($user->getUsername()) ? null : $user->getUsername()
empty($user->getUsername()) ? null : $user->getUsername()
);
break;
case Hash::ALGORITHM_BCRYPT:
$result = $auth->createBcryptUser(
$user->getId(),
$user->getEmail(),
$hash->getHash(),
empty($user->getUsername()) ? null : $user->getUsername()
empty($user->getUsername()) ? null : $user->getUsername()
);
break;
case Hash::ALGORITHM_ARGON2:
$result = $auth->createArgon2User(
$user->getId(),
$user->getEmail(),
$hash->getHash(),
empty($user->getUsername()) ? null : $user->getUsername()
empty($user->getUsername()) ? null : $user->getUsername()
);
break;
case Hash::ALGORITHM_SHA256:
Expand All @@ -569,15 +566,15 @@ public function importPasswordUser(User $user): array|null
$user->getEmail(),
$hash->getHash(),
'sha256',
empty($user->getUsername()) ? null : $user->getUsername()
empty($user->getUsername()) ? null : $user->getUsername()
);
break;
case Hash::ALGORITHM_PHPASS:
$result = $auth->createPHPassUser(
$user->getId(),
$user->getEmail(),
$hash->getHash(),
empty($user->getUsername()) ? null : $user->getUsername()
empty($user->getUsername()) ? null : $user->getUsername()
);
break;
case Hash::ALGORITHM_SCRYPT:
Expand All @@ -590,7 +587,7 @@ public function importPasswordUser(User $user): array|null
$hash->getPasswordMemory(),
$hash->getPasswordParallel(),
$hash->getPasswordLength(),
empty($user->getUsername()) ? null : $user->getUsername()
empty($user->getUsername()) ? null : $user->getUsername()
);
break;
case Hash::ALGORITHM_PLAINTEXT:
Expand All @@ -599,7 +596,7 @@ public function importPasswordUser(User $user): array|null
$user->getEmail(),
$user->getPhone(),
$hash->getHash(),
empty($user->getUsername()) ? null : $user->getUsername()
empty($user->getUsername()) ? null : $user->getUsername()
);
break;
}
Expand Down
34 changes: 17 additions & 17 deletions src/Transfer/Destinations/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public function __construct(string $path)
{
$this->path = $path;

if (!\file_exists($this->path)) {
if (! \file_exists($this->path)) {
mkdir($this->path, 0777, true);
mkdir($this->path . '/files', 0777, true);
mkdir($this->path . '/deployments', 0777, true);
mkdir($this->path.'/files', 0777, true);
mkdir($this->path.'/deployments', 0777, true);
}
}

Expand All @@ -42,7 +42,7 @@ public static function getName(): string
/**
* Get Supported Resources
*/
static function getSupportedResources(): array
public static function getSupportedResources(): array
{
return [
Resource::TYPE_ATTRIBUTE,
Expand All @@ -58,7 +58,7 @@ static function getSupportedResources(): array
Resource::TYPE_INDEX,
Resource::TYPE_TEAM,
Resource::TYPE_MEMBERSHIP,
Resource::TYPE_USER
Resource::TYPE_USER,
];
}

Expand All @@ -74,9 +74,9 @@ public function report(array $resources = []): array
}

// Check we can write to the file
if (!\is_writable($this->path . '/backup.json')) {
$report[Transfer::GROUP_DATABASES][] = 'Unable to write to file: ' . $this->path;
throw new \Exception('Unable to write to file: ' . $this->path);
if (! \is_writable($this->path.'/backup.json')) {
$report[Transfer::GROUP_DATABASES][] = 'Unable to write to file: '.$this->path;
throw new \Exception('Unable to write to file: '.$this->path);
}

return $report;
Expand All @@ -93,44 +93,44 @@ private function sync(): void
throw new \Exception('Unable to encode data to JSON, Are you accidentally encoding binary data?');
}

\file_put_contents($this->path . '/backup.json', \json_encode($this->data, JSON_PRETTY_PRINT));
\file_put_contents($this->path.'/backup.json', \json_encode($this->data, JSON_PRETTY_PRINT));
}

protected function import(array $resources, callable $callback): void
{
foreach ($resources as $resource) {
/** @var Resource $resource */
/** @var resource $resource */
switch ($resource->getName()) {
case 'Deployment':
/** @var Deployment $resource */
if ($resource->getStart() === 0) {
$this->data[$resource->getGroup()][$resource->getName()][$resource->getInternalId()] = $resource->asArray();
}

file_put_contents($this->path . 'deployments/' . $resource->getId() . '.tar.gz', $resource->getData(), FILE_APPEND);
file_put_contents($this->path.'deployments/'.$resource->getId().'.tar.gz', $resource->getData(), FILE_APPEND);
break;
case 'File':
/** @var File $resource */

// Handle folders
if (str_contains($resource->getFileName(), '/')) {
$folders = explode('/', $resource->getFileName());
$folderPath = $this->path . '/files';
$folderPath = $this->path.'/files';

foreach ($folders as $folder) {
$folderPath .= '/' . $folder;
$folderPath .= '/'.$folder;

if (!\file_exists($folderPath) && str_contains($folder, '.') === false) {
if (! \file_exists($folderPath) && str_contains($folder, '.') === false) {
mkdir($folderPath, 0777, true);
}
}
}

if ($resource->getStart() === 0 && \file_exists($this->path . '/files/' . $resource->getFileName())) {
unlink($this->path . '/files/' . $resource->getFileName());
if ($resource->getStart() === 0 && \file_exists($this->path.'/files/'.$resource->getFileName())) {
unlink($this->path.'/files/'.$resource->getFileName());
}

file_put_contents($this->path . '/files/' . $resource->getFileName(), $resource->getData(), FILE_APPEND);
file_put_contents($this->path.'/files/'.$resource->getFileName(), $resource->getData(), FILE_APPEND);
break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Transfer/Resources/Auth/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(
string $id = '',
string $email = '',
string $username = '',
?Hash $passwordHash = null,
Hash $passwordHash = null,
string $phone = '',
array $types = [self::TYPE_ANONYMOUS],
string $oauthProvider = '',
Expand Down
2 changes: 1 addition & 1 deletion src/Transfer/Resources/Database/Attributes/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Boolean extends Attribute
/**
* @param ?bool $default
*/
public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, ?bool $default = null)
public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, bool $default = null)
{
parent::__construct($key, $collection, $required, $array);
$this->default = $default;
Expand Down
2 changes: 1 addition & 1 deletion src/Transfer/Resources/Database/Attributes/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class DateTime extends Attribute
/**
* @param ?string $default
*/
public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, ?string $default = null)
public function __construct(string $key, Collection $collection, bool $required = false, bool $array = false, string $default = null)
{
parent::__construct($key, $collection, $required, $array);
$this->default = $default;
Expand Down
Loading