Skip to content
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
13 changes: 13 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directories:
- "/"
schedule:
interval: "weekly"
cooldown:
default-days: 5
groups:
github-actions:
patterns:
- "*"
53 changes: 53 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: tests

on:
push:
branches:
- main
- 1.x
- 2.x
pull_request:
branches:
- main
- 1.x
- 2.x

permissions:
contents: read

jobs:
tests:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php-version: ['8.3', '8.4', '8.5']

steps:
- name: Checkout Code
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
with:
persist-credentials: false

- name: Setup PHP
uses: shivammathur/setup-php@f3e473d116dcccaddc5834248c87452386958240 # v2
with:
php-version: ${{ matrix.php-version }}
tools: composer:v2
coverage: none

- name: Install Dependencies
run: composer install --no-interaction --prefer-dist --optimize-autoloader

- name: Run Static Analysis
run: composer analyse

- name: Run Lint Check
run: composer lint:check

- name: Run Type Coverage
run: composer test:types

- name: Run Unit Tests
run: composer test:unit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor
composer.lock
45 changes: 43 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,51 @@
"src/functions.php"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"require": {
"php": "^8.2"
"php": "^8.3"
},
"require-dev": {
"laravel/pao": "^1.1",
"laravel/pint": "^1.29",
"pestphp/pest": "^4.7",
"pestphp/pest-plugin-type-coverage": "^4.0",
"phpstan/phpstan": "^2.2"
},
"bin": [
"cpx"
]
],
"scripts": {
"analyse": [
"phpstan analyse"
],
"lint": [
"pint --parallel"
],
"lint:check": [
"pint --parallel --test"
],
"test:types": [
"pest --type-coverage --min=100"
],
"test:unit": [
"pest --parallel"
],
"test": [
"@analyse",
"@lint:check",
"@test:types",
"@test:unit"
]
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
},
"sort-packages": true
}
}
5 changes: 0 additions & 5 deletions cpx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ if (isset($GLOBALS['_composer_autoload_path'])) {
unset($file);
}

function printColor(string $message, string $color = "\033[1;32m"): void
{
echo $color . $message . "\033[0m" . PHP_EOL;
}

array_shift($argv);
$console = Console::parse($argv ?? []);

Expand Down
6 changes: 4 additions & 2 deletions files/psysh-config.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use Cpx\PhpExecutionHelper;

Cpx\PhpExecutionHelper::init(getcwd());
require_once __DIR__.'/../vendor/autoload.php';

PhpExecutionHelper::init(getcwd());

return [];
4 changes: 4 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 7
paths:
- src
17 changes: 17 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/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">src</directory>
</include>
</source>
</phpunit>
27 changes: 27 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"preset": "laravel",
"rules": {
"blank_line_before_statement": {
"statements": [
"break",
"continue",
"declare",
"if",
"return",
"throw",
"try"
]
},
"global_namespace_import": {
"import_classes": true
},
"trailing_comma_in_multiline": {
"elements": [
"arguments",
"arrays",
"match",
"parameters"
]
}
}
}
21 changes: 11 additions & 10 deletions src/ClassAliasAutoloader.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

declare(strict_types=1);

namespace Cpx;

use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;

class ClassAliasAutoloader
{
/** All of the discovered classes. */
/** @var array<string, string> */
protected array $classes = [];

public function __construct(
Expand All @@ -21,13 +23,13 @@ public function addAliases(string $autoloadRootDirectory): void
$classes = require "{$autoloadRootDirectory}/vendor/composer/autoload_classmap.php";

foreach ($classes as $class => $path) {
if (!str_contains($class, '\\')) {
if (! str_contains($class, '\\')) {
continue;
}

$name = basename(str_replace('\\', '/', $class));

if (!isset($this->classes[$name]) && class_exists($name)) {
if (! isset($this->classes[$name]) && class_exists($name)) {
$this->classes[$name] = $class;
}
}
Expand All @@ -38,12 +40,12 @@ public function addAliases(string $autoloadRootDirectory): void

foreach ($psr4 as $namespace => $directories) {
foreach ($directories as $directory) {
if (!file_exists($directory)) {
if (! file_exists($directory)) {
continue;
}
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory),
RecursiveIteratorIterator::LEAVES_ONLY
RecursiveIteratorIterator::LEAVES_ONLY,
);

foreach ($iterator as $file) {
Expand All @@ -53,13 +55,12 @@ public function addAliases(string $autoloadRootDirectory): void

$relativePath = str_replace($directory, '', $file->getPath());

if (!empty($relativePath)) {
$classNamespace .= strtr($relativePath, DIRECTORY_SEPARATOR, '\\') . '\\';
if (! empty($relativePath)) {
$classNamespace .= strtr($relativePath, DIRECTORY_SEPARATOR, '\\').'\\';
}

$basename = $file->getBasename('.php');
$class = str_replace('\\\\', '\\', $classNamespace . $basename);

$class = str_replace('\\\\', '\\', $classNamespace.$basename);

if (str_ends_with($basename, 'Test')) {
continue;
Expand Down
10 changes: 6 additions & 4 deletions src/Commands/AliasesCommand.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<?php

declare(strict_types=1);

namespace Cpx\Commands;

use Cpx\PackageAliases;

class AliasesCommand extends Command
{
public function __invoke()
public function __invoke(): void
{
$this->line('Aliased packages:' . PHP_EOL);
$this->line('Aliased packages:'.PHP_EOL);
$packages = PackageAliases::$packages;
usort($packages, fn ($a, $b) => strcmp($a['command'], $b['command']));
usort($packages, fn (array $a, array $b): int => strcmp($a['command'], $b['command']));

foreach ($packages as $package) {
$paddedCommand = str_pad($package['command'], 15);
$this->line(' ' . Command::COLOR_GREEN . 'cpx ' . $paddedCommand . Command::COLOR_RESET . ' ' . $package['description']);
$this->line(' '.Command::COLOR_GREEN.'cpx '.$paddedCommand.Command::COLOR_RESET.' '.$package['description']);
}
}
}
16 changes: 12 additions & 4 deletions src/Commands/CheckCommand.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
<?php

declare(strict_types=1);

namespace Cpx\Commands;

use Cpx\Console;
use Cpx\Exceptions\ConsoleException;

class CheckCommand extends Command
{
public function __invoke()
public function __invoke(): void
{
if (file_exists('vendor/bin/phpstan')) {
$command = 'vendor/bin/phpstan analyse';

return Console::parse($command)->exec();
Console::parse($command)->exec();

return;
}

if (file_exists('vendor/bin/psalm')) {
$command = 'vendor/bin/psalm';

return Console::parse($command)->exec();
Console::parse($command)->exec();

return;
}

if (file_exists('vendor/bin/phan')) {
$command = 'vendor/bin/phan';

return Console::parse($command)->exec();
Console::parse($command)->exec();

return;
}

throw new ConsoleException('No static analyzers found in the project.');
Expand Down
19 changes: 11 additions & 8 deletions src/Commands/CleanCommand.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

declare(strict_types=1);

namespace Cpx\Commands;

use Cpx\Package;
use Cpx\Metadata;
use Cpx\Package;
use Cpx\Utils;

class CleanCommand extends Command
{
public function __invoke()
public function __invoke(): void
{
$days = (int) ($this->console->getOption('days') ?? 30);

Expand All @@ -20,29 +23,29 @@ public function __invoke()

if ($this->console->hasOption('all') || $lastRun < $timeLimit) {
$package = Package::parse($packageKey);
$this->line(Command::COLOR_GREEN . "Removing unused package {$package}...");
$this->line(Command::COLOR_GREEN."Removing unused package {$package}...");
$package->delete();
unset($metadata->packages[$packageKey]);
$cleanedSomething = true;
}
}

foreach ($metadata->execCache as $sandboxDir => $packageMetadata) {
$lastRun = strtotime($packageMetadata->lastRunAt ?? '1970-01-01 00:00:00');
$lastRun = $packageMetadata['last_run'] ?? 0;

if ($this->console->hasOption('all') || $lastRun < $timeLimit) {
$packageDirectory = cpx_path(".exec_cache/{$sandboxDir}");
exec("rm -rf {$packageDirectory}");
$this->line(Command::COLOR_GREEN . "Removing exec sandbox cache {$sandboxDir}...");
Utils::deleteDirectory($packageDirectory);
$this->line(Command::COLOR_GREEN."Removing exec sandbox cache {$sandboxDir}...");
unset($metadata->execCache[$sandboxDir]);
$cleanedSomething = true;
}
}

$metadata->save();

if (!$cleanedSomething) {
$this->success("There were no packages to clean.");
if (! $cleanedSomething) {
$this->success('There were no packages to clean.');
}
}
}
Loading