Skip to content

Commit

Permalink
Upgraded to PHP 8.1. Replaced PHPUnit with Pest. Introduced Rector
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmiu committed Nov 19, 2023
1 parent b6f37e4 commit f8f04e4
Show file tree
Hide file tree
Showing 24 changed files with 830 additions and 728 deletions.
24 changes: 24 additions & 0 deletions .github/ISSUE_TEMPLATE/Bug.md
@@ -0,0 +1,24 @@
---
name: 🐛 Bug
about: Did you encounter a bug?
---

### Bug Report

<!-- Fill in the relevant information below to help triage your issue. -->

| Q | A
|------------ | ------
| BC Break | yes/no
| Version | x.y.z

#### Summary

<!-- Provide a summary describing the problem you are experiencing. -->

#### How to reproduce

<!--
Provide steps to reproduce the issue.
If possible, also add a code snippet.
-->
21 changes: 21 additions & 0 deletions .github/ISSUE_TEMPLATE/Feature_Request.md
@@ -0,0 +1,21 @@
---
name: 🎉 Feature Request
about: Do you have a new feature in mind?
---

### Feature Request

<!-- Fill in the relevant information below to help triage your issue. -->

| Q | A
|------------ | ------
| New Feature | yes/no
| BC Break | yes/no

#### Scenario / Use-case

<!-- Provide an explain in which scenario the feature would be helpful. -->

#### Summary

<!-- Provide a summary of the feature you would like to see implemented. -->
8 changes: 8 additions & 0 deletions .github/ISSUE_TEMPLATE/Question.md
@@ -0,0 +1,8 @@
---
name: ❓ Question
about: Are you unsure about something?
---

### Question

<!-- Fill in the relevant information below to help triage your issue. -->
51 changes: 51 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,51 @@
name: CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
strategy:
matrix:
php: ['8.1', '8.2', '8.3']
include:
- php: '8.1'
send-to-scrutinizer: 'yes'

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Setup PHP with fail-fast
uses: shivammathur/setup-php@v2
with:
send-to-scrutinizer: 'no'
phpunit-flags: '--no-coverage'
php-version: ${{ matrix.php }}
coverage: xdebug
env:
fail-fast: true
- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run test suite
run: |
mkdir -p build/logs
composer run stan
composer run test
28 changes: 28 additions & 0 deletions .github/workflows/docs.yml
@@ -0,0 +1,28 @@
name: Publish docs

on:
push:
tags:
- '*.*.*'
jobs:
docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1

- name: Build docs
run: |
curl -OS https://couscous.io/couscous.phar
php couscous.phar generate --target=build/docs/ ./docs
- name: FTP Deployer
uses: sand4rt/ftp-deployer@v1.1
with:
host: ${{ secrets.DOCS_FTP_HOST }}
username: ${{ secrets.DOCS_FTP_USER }}
password: ${{ secrets.DOCS_FTP_PASSWORD }}
remote_folder: upload
# The local folder location
local_folder: build/docs/
# Remove existing files inside FTP remote folder
cleanup: false # optional
1 change: 1 addition & 0 deletions .php_cs.cache
@@ -0,0 +1 @@
{"php":"7.2.4","version":"2.16.1","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true},"hashes":{"src\\Container\\ContainerInterface.php":1583588918,"src\\Container\\Local.php":71709479,"src\\Exception\\InvalidContainerException.php":1907413747,"src\\Exception\\InvalidResultException.php":466931389,"src\\Handler.php":534695839,"src\\HandlerAggregate.php":1499917949,"src\\Result\\Collection.php":2897468334,"src\\Result\\File.php":1434642790,"src\\UploadHandlerInterface.php":2299182802,"src\\Util\\Arr.php":1378696061,"src\\Result\\ResultInterface.php":844276353,"src\\Util\\Helper.php":3774982220}}
36 changes: 19 additions & 17 deletions composer.json
Expand Up @@ -19,13 +19,16 @@
}
],
"require": {
"php": ">=7.1",
"siriusphp/validation": "~3.0"
"php": ">=8.1",
"siriusphp/validation": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "~8.5",
"laminas/laminas-diactoros": "^2.2",
"symfony/http-foundation": "^4.4"
"laminas/laminas-diactoros": "^3.3",
"symfony/http-foundation": "^6.3",
"pestphp/pest": "^2.24",
"pestphp/pest-plugin-drift": "^2.5",
"symfony/mime": "^6.3",
"phpstan/phpstan": "^1.10"
},
"suggest": {
"league/flysystem": "To upload to different destinations, not just to the local file system",
Expand All @@ -37,26 +40,25 @@
}
},
"scripts": {
"cs": [
"php phpcs.phar --standard=PSR2 ./src"
],
"md": [
"php phpmd.phar ./src xml phpmd.xml"
],
"cbf": [
"php phpcbf.phar ./src --standard=PSR2 -w"
"stan": [
"php vendor/bin/phpstan analyse"
],
"csfix": [
"php php-cs-fixer.phar fix ./src --rules=@PSR2"
"tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --standard=PSR-2 src"
],
"test": [
"php vendor/bin/pest"
],
"build-docs": [
"php couscous.phar generate --target=build/docs/ ./docs"
],
"docs": [
"cd docs && php ../couscous.phar preview"
],
"test": [
"vendor/bin/phpunit -c phpunit.xml"
]
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
5 changes: 5 additions & 0 deletions phpstan.neon
@@ -0,0 +1,5 @@
parameters:
level: 8
checkGenericClassInNonGenericObjectType: false
paths:
- src
18 changes: 18 additions & 0 deletions phpunit.xml
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
17 changes: 5 additions & 12 deletions src/Container/ContainerInterface.php
Expand Up @@ -9,35 +9,28 @@ interface ContainerInterface
/**
* Check if the container is writable
*/
public function isWritable();
public function isWritable(): bool;

/**
* This will check if a file is in the container
*
* @param string $file
*/
public function has($file);
public function has(string $file): bool;

/**
* Saves the $content string as a file
*
* @param string $file
* @param string $content
*/
public function save($file, $content);
public function save(string $file, string $content): bool;

/**
* Delete the file from the container
*
* @param string $file
*/
public function delete($file);
public function delete(string $file): bool;

/**
* Moves a temporary uploaded file to a destination in the container
*
* @param string $localFile local path
* @param string $destination
*/
public function moveUploadedFile($localFile, $destination);
public function moveUploadedFile(string $localFile, string $destination): bool;
}
50 changes: 17 additions & 33 deletions src/Container/Local.php
Expand Up @@ -5,24 +5,25 @@

class Local implements ContainerInterface
{
protected $baseDirectory;
protected string $baseDirectory;

public function __construct($baseDirectory)
public function __construct(string $baseDirectory)
{
$this->baseDirectory = $this->normalizePath($baseDirectory) . DIRECTORY_SEPARATOR;
$this->ensureDirectory($this->baseDirectory);
}

protected function normalizePath($path)
protected function normalizePath(string $path): string
{
$path = dirname(rtrim($path, '\\/') . DIRECTORY_SEPARATOR . 'xxx');

return rtrim($path, DIRECTORY_SEPARATOR);
}

protected function ensureDirectory($directory):bool
protected function ensureDirectory(string $directory): bool
{
if (!file_exists($directory)) {
if ( ! is_dir($directory)) {
echo $directory, PHP_EOL;
mkdir($directory, 0755, true);
}

Expand All @@ -32,47 +33,35 @@ protected function ensureDirectory($directory):bool
/**
* Check if the container is writable
*/
public function isWritable():bool
public function isWritable(): bool
{
return is_writable($this->baseDirectory);
}

/**
* This will check if a file is in the container
*
* @param string $file
* @param string $file
*
* @return bool
*/
public function has($file):bool
public function has(string $file): bool
{
return $file && file_exists($this->baseDirectory . $file);
}

/**
* Saves the $content string as a file
*
* @param string $file
* @param string $content
* @return bool
*/
public function save($file, $content):bool
public function save(string $file, string $content): bool
{
$file = $this->normalizePath($file);
$dir = dirname($this->baseDirectory . $file);
$dir = dirname($this->baseDirectory . $file);
if ($this->ensureDirectory($dir)) {
return (bool) file_put_contents($this->baseDirectory . $file, $content);
return (bool)file_put_contents($this->baseDirectory . $file, $content);
}

return false;
}

/**
* Delete the file from the container
*
* @param string $file
* @return bool
*/
public function delete($file):bool
public function delete(string $file): bool
{
$file = $this->normalizePath($file);
if (file_exists($this->baseDirectory . $file)) {
Expand All @@ -82,24 +71,19 @@ public function delete($file):bool
return true;
}

/**
* Moves a temporary uploaded file to a destination in the container
*
* @param string $localFile local path
* @param string $destination
* @return bool
*/
public function moveUploadedFile($localFile, $destination):bool
public function moveUploadedFile(string $localFile, string $destination): bool
{
$dir = dirname($this->baseDirectory . $destination);
if (file_exists($localFile) && $this->ensureDirectory($dir)) {
if (is_readable($localFile)) {
// rename() would be good but this is better because $localFile may become 'unwritable'
$result = copy($localFile, $this->baseDirectory . $destination);
@unlink($localFile);

return $result;
}
}

return false;
}
}

0 comments on commit f8f04e4

Please sign in to comment.