diff --git a/.github/workflows/code-style.yml b/.github/workflows/code-style.yml new file mode 100644 index 0000000..22addf3 --- /dev/null +++ b/.github/workflows/code-style.yml @@ -0,0 +1,41 @@ +name: code-style + +on: + push: + branches: + - main + pull_request: + +jobs: + code-style: + runs-on: ubuntu-22.04 + + strategy: + fail-fast: true + matrix: + php: [ 8.2 ] + node-version: [ "20.x" ] + + steps: + - uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + + - 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 pint + run: ./vendor/bin/pint --test diff --git a/composer.json b/composer.json index 90a932a..ea27361 100644 --- a/composer.json +++ b/composer.json @@ -18,5 +18,8 @@ "psy/psysh": "*" }, "minimum-stability": "stable", - "prefer-stable": true + "prefer-stable": true, + "require-dev": { + "laravel/pint": "^1.19" + } } diff --git a/composer.lock b/composer.lock index 2a5a09e..8d63d0e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "206f824071dc8fdf6bc7dcd684dd5aab", + "content-hash": "1130d41e7e62baf860f3bb9f06a09765", "packages": [ { "name": "nikic/php-parser", @@ -928,7 +928,74 @@ "time": "2024-11-08T15:48:14+00:00" } ], - "packages-dev": [], + "packages-dev": [ + { + "name": "laravel/pint", + "version": "v1.19.0", + "source": { + "type": "git", + "url": "https://github.com/laravel/pint.git", + "reference": "8169513746e1bac70c85d6ea1524d9225d4886f0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/pint/zipball/8169513746e1bac70c85d6ea1524d9225d4886f0", + "reference": "8169513746e1bac70c85d6ea1524d9225d4886f0", + "shasum": "" + }, + "require": { + "ext-json": "*", + "ext-mbstring": "*", + "ext-tokenizer": "*", + "ext-xml": "*", + "php": "^8.1.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.66.0", + "illuminate/view": "^10.48.25", + "larastan/larastan": "^2.9.12", + "laravel-zero/framework": "^10.48.25", + "mockery/mockery": "^1.6.12", + "nunomaduro/termwind": "^1.17.0", + "pestphp/pest": "^2.36.0" + }, + "bin": [ + "builds/pint" + ], + "type": "project", + "autoload": { + "psr-4": { + "App\\": "app/", + "Database\\Seeders\\": "database/seeders/", + "Database\\Factories\\": "database/factories/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nuno Maduro", + "email": "enunomaduro@gmail.com" + } + ], + "description": "An opinionated code formatter for PHP.", + "homepage": "https://laravel.com", + "keywords": [ + "format", + "formatter", + "lint", + "linter", + "php" + ], + "support": { + "issues": "https://github.com/laravel/pint/issues", + "source": "https://github.com/laravel/pint" + }, + "time": "2024-12-30T16:20:10+00:00" + } + ], "aliases": [], "minimum-stability": "stable", "stability-flags": {}, diff --git a/index.php b/index.php index b120846..09a040c 100644 --- a/index.php +++ b/index.php @@ -2,12 +2,12 @@ use TweakPHP\Client\Loader; -require __DIR__ . '/vendor/autoload.php'; +require __DIR__.'/vendor/autoload.php'; $arguments = $argv; if (count($arguments) < 3) { - echo 'Invalid arguments' . PHP_EOL; + echo 'Invalid arguments'.PHP_EOL; exit(1); } @@ -23,7 +23,7 @@ function dd(...$args) $loader = Loader::load($arguments[1]); if ($loader === null) { - echo 'Invalid path' . PHP_EOL; + echo 'Invalid path'.PHP_EOL; exit(1); } @@ -34,8 +34,8 @@ function dd(...$args) 'execute', ]; -if (!in_array($arguments[2], $supportedCommands)) { - echo 'Invalid command' . PHP_EOL; +if (! in_array($arguments[2], $supportedCommands)) { + echo 'Invalid command'.PHP_EOL; exit(1); } @@ -46,13 +46,13 @@ function dd(...$args) 'version' => $loader->version(), 'php_version' => phpversion(), ]); - echo $info . PHP_EOL; + echo $info.PHP_EOL; break; case 'execute': if (count($arguments) < 4) { - echo 'Invalid arguments' . PHP_EOL; + echo 'Invalid arguments'.PHP_EOL; exit(1); } - echo $loader->execute(base64_decode($arguments[3])) . PHP_EOL; + echo $loader->execute(base64_decode($arguments[3])).PHP_EOL; break; -} \ No newline at end of file +} diff --git a/src/Loader.php b/src/Loader.php index bfa799c..483957a 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -12,7 +12,6 @@ class Loader { /** - * @param string $path * @return null|LoaderInterface */ public static function load(string $path) @@ -29,6 +28,10 @@ public static function load(string $path) return new WordPressLoader($path); } + if (PimcoreLoader::supports($path)) { + return new PimcoreLoader($path); + } + if (ComposerLoader::supports($path)) { return new ComposerLoader($path); } diff --git a/src/Loaders/BaseLoader.php b/src/Loaders/BaseLoader.php index 1750dd7..d58db38 100644 --- a/src/Loaders/BaseLoader.php +++ b/src/Loaders/BaseLoader.php @@ -11,12 +11,25 @@ abstract class BaseLoader implements LoaderInterface { protected Tinker $tinker; - public function init() + public function init(): void { $config = new Configuration([ 'configFile' => null, ]); $config->setUpdateCheck(Checker::NEVER); + $config->setRawOutput(true); + $config->setInteractiveMode(Configuration::INTERACTIVE_MODE_DISABLED); + $config->setColorMode(Configuration::COLOR_MODE_DISABLED); + $config->setTheme([ + 'prompt' => '', + ]); + $config->setVerbosity(Configuration::VERBOSITY_QUIET); + $config->setHistoryFile(defined('PHP_WINDOWS_VERSION_BUILD') ? 'null' : '/dev/null'); + $config->setRawOutput(false); + if (getenv('KUBERNETES_SERVICE_HOST') || defined('PHP_WINDOWS_VERSION_BUILD')) { + $config->setUsePcntl(false); + } + if (class_exists('Illuminate\Support\Collection') && class_exists('Laravel\Tinker\TinkerCaster')) { $config->getPresenter()->addCasters([ \Illuminate\Support\Collection::class => 'Laravel\Tinker\TinkerCaster::castCollection', @@ -32,15 +45,14 @@ public function init() \Illuminate\Foundation\Application::class => 'Laravel\Tinker\TinkerCaster::castApplication', ]); } - $config->setRawOutput(true); - $this->tinker = new Tinker(new CustomOutputModifier(), $config); + $this->tinker = new Tinker(new CustomOutputModifier, $config); } - public function execute(string $code) + public function execute(string $code): string { $output = $this->tinker->execute($code); - echo trim($output); + return trim($output); } -} \ No newline at end of file +} diff --git a/src/Loaders/ComposerLoader.php b/src/Loaders/ComposerLoader.php index 8a07505..5be2fa4 100644 --- a/src/Loaders/ComposerLoader.php +++ b/src/Loaders/ComposerLoader.php @@ -4,21 +4,14 @@ class ComposerLoader extends BaseLoader { - /** - * @param string $path - * @return bool - */ public static function supports(string $path): bool { - return file_exists($path . '/vendor/autoload.php'); + return file_exists($path.'/vendor/autoload.php'); } - /** - * @param string $path - */ public function __construct(string $path) { - require $path . '/vendor/autoload.php'; + require $path.'/vendor/autoload.php'; } public function name(): string @@ -26,11 +19,8 @@ public function name(): string return 'Composer Project'; } - /** - * @return string - */ public function version(): string { - return ""; + return ''; } } diff --git a/src/Loaders/LaravelLoader.php b/src/Loaders/LaravelLoader.php index c4227d8..19ba83c 100644 --- a/src/Loaders/LaravelLoader.php +++ b/src/Loaders/LaravelLoader.php @@ -2,56 +2,27 @@ namespace TweakPHP\Client\Loaders; -use Throwable; - class LaravelLoader extends ComposerLoader { private $app; - /** - * @param string $path - * @return bool - */ public static function supports(string $path): bool { - return file_exists($path . '/vendor/autoload.php') && file_exists($path . '/bootstrap/app.php'); + return file_exists($path.'/vendor/autoload.php') && file_exists($path.'/bootstrap/app.php'); } - /** - * @param string $path - */ public function __construct(string $path) { parent::__construct($path); - $this->app = require_once $path . '/bootstrap/app.php'; + $this->app = require_once $path.'/bootstrap/app.php'; $this->app->make('Illuminate\Contracts\Console\Kernel')->bootstrap(); - $classAliases = require $path . '/vendor/composer/autoload_classmap.php'; - $vendorPath = dirname($path . '/vendor/composer/autoload_classmap.php', 2); - foreach ($classAliases as $class => $path) { - if (!str_contains($class, '\\')) { - continue; - } - if (str_starts_with($path, $vendorPath)) { - continue; - } - try { - class_alias($class, class_basename($class)); - } catch (Throwable $e) { - } - } } - /** - * @return string - */ public function name(): string { return 'Laravel'; } - /** - * @return string - */ public function version(): string { return $this->app->version(); diff --git a/src/Loaders/LoaderInterface.php b/src/Loaders/LoaderInterface.php index b48db39..e445ea7 100644 --- a/src/Loaders/LoaderInterface.php +++ b/src/Loaders/LoaderInterface.php @@ -4,30 +4,13 @@ interface LoaderInterface { - /** - * @param string $path - * @return bool - */ public static function supports(string $path): bool; - /** - * @return string - */ public function name(): string; - /** - * @return string - */ public function version(): string; - /** - * @return void - */ - public function init(); + public function init(): void; - /** - * @param string $code - * @return void - */ - public function execute(string $code); + public function execute(string $code): string; } diff --git a/src/Loaders/PimcoreLoader.php b/src/Loaders/PimcoreLoader.php index e774ab0..329aacc 100644 --- a/src/Loaders/PimcoreLoader.php +++ b/src/Loaders/PimcoreLoader.php @@ -1,22 +1,22 @@ kernel->boot(); } - /** - * Find the application's Kernel class dynamically. - * - * @param string $path - * @return string - */ private function findKernelClass(string $path): string { - $kernelFile = $path . '/src/Kernel.php'; - if (!file_exists($kernelFile)) { - throw new \RuntimeException('Kernel.php file not found in the src directory.'); - } + require_once $path.'/src/Kernel.php'; - require_once $kernelFile; return 'App\\Kernel'; } - /** - * @return string - */ public function name(): string { return 'Symfony'; } - /** - * @return string - */ public function version(): string { - return \Symfony\Component\HttpKernel\Kernel::VERSION; + if (class_exists('Symfony\Component\HttpKernel\Kernel')) { + return \Symfony\Component\HttpKernel\Kernel::VERSION; + } + + return ''; } } diff --git a/src/Loaders/WordPressLoader.php b/src/Loaders/WordPressLoader.php index 9a34fbb..87f9883 100644 --- a/src/Loaders/WordPressLoader.php +++ b/src/Loaders/WordPressLoader.php @@ -6,23 +6,16 @@ class WordPressLoader extends BaseLoader { - /** - * @param string $path - * @return bool - */ public static function supports(string $path): bool { - return file_exists($path . '/wp-load.php'); + return file_exists($path.'/wp-load.php'); } - /** - * @param string $path - */ public function __construct(string $path) { - require_once $path . '/wp-load.php'; - require_once $path . '/wp-admin/includes/admin.php'; - require_once $path . '/wp-includes/pluggable.php'; + require_once $path.'/wp-load.php'; + require_once $path.'/wp-admin/includes/admin.php'; + require_once $path.'/wp-includes/pluggable.php'; } public function name(): string @@ -30,17 +23,16 @@ public function name(): string return 'WordPress'; } - /** - * @return string - */ public function version(): string { try { - return get_bloginfo('version'); + if (function_exists('get_bloginfo')) { + return get_bloginfo('version'); + } } catch (Throwable $e) { // } - return ""; + return ''; } } diff --git a/src/OutputModifiers/CustomOutputModifier.php b/src/OutputModifiers/CustomOutputModifier.php index 90fd116..6cf06aa 100644 --- a/src/OutputModifiers/CustomOutputModifier.php +++ b/src/OutputModifiers/CustomOutputModifier.php @@ -9,4 +9,4 @@ public function modify(string $output = ''): string // remove only the first tab from each line return preg_replace('/^ {2}/m', '', $output); } -} \ No newline at end of file +} diff --git a/src/OutputModifiers/OutputModifier.php b/src/OutputModifiers/OutputModifier.php index 8e45782..d2beadb 100644 --- a/src/OutputModifiers/OutputModifier.php +++ b/src/OutputModifiers/OutputModifier.php @@ -5,4 +5,4 @@ interface OutputModifier { public function modify(string $output = ''): string; -} \ No newline at end of file +} diff --git a/src/Tinker.php b/src/Tinker.php index b841ab0..865557c 100644 --- a/src/Tinker.php +++ b/src/Tinker.php @@ -2,6 +2,7 @@ namespace TweakPHP\Client; +use Psy\Configuration; use Psy\ExecutionLoopClosure; use Psy\Shell; use Symfony\Component\Console\Output\BufferedOutput; @@ -15,9 +16,9 @@ class Tinker protected OutputModifier $outputModifier; - public function __construct(OutputModifier $outputModifier, $config) + public function __construct(OutputModifier $outputModifier, Configuration $config) { - $this->output = new BufferedOutput(); + $this->output = new BufferedOutput; $this->shell = $this->createShell($this->output, $config); @@ -27,7 +28,7 @@ public function __construct(OutputModifier $outputModifier, $config) public function execute(string $phpCode): string { $phpCode = $this->removeComments($phpCode); - + $this->shell->addInput($phpCode); $closure = new ExecutionLoopClosure($this->shell); @@ -39,10 +40,8 @@ public function execute(string $phpCode): string return $this->outputModifier->modify($output); } - protected function createShell(BufferedOutput $output, $config): Shell + protected function createShell(BufferedOutput $output, Configuration $config): Shell { - $config->setHistoryFile(defined('PHP_WINDOWS_VERSION_BUILD') ? 'null' : '/dev/null'); - $shell = new Shell($config); $shell->setOutput($output); @@ -52,12 +51,12 @@ protected function createShell(BufferedOutput $output, $config): Shell public function removeComments(string $code): string { - $tokens = token_get_all("'); + $tokens = token_get_all("'); $result = ''; foreach ($tokens as $token) { if (is_array($token)) { - list($id, $text) = $token; + [$id, $text] = $token; if (in_array($id, [T_COMMENT, T_DOC_COMMENT, T_OPEN_TAG, T_CLOSE_TAG])) { continue;