Skip to content

Commit

Permalink
php cs fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Mar 16, 2024
1 parent b3e5a2e commit e7f92ca
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 140 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/static.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Static analysis
on:
push:
branches:
- '[0-9]+.x'
- '[0-9]+.[0-9]+'
- '[0-9]+.[0-9]+.x'
pull_request:

jobs:
phpstan:
name: PHPStan
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: PHPStan
uses: docker://oskarstark/phpstan-ga
env:
REQUIRE_DEV: true
with:
args: analyze --no-progress

php-cs-fixer:
name: PHP-CS-Fixer
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --dry-run
12 changes: 7 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
composer.lock
vendor
tests/data
tests/migrations
.phpunit.result.cache
/.php-cs-fixer.cache
/.phpunit.result.cache
/composer.lock
/phpunit.xml
/tests/data
/tests/migrations
/vendor
12 changes: 12 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__);
$config = new PhpCsFixer\Config();

return $config->setFinder($finder)
->setRules([
'@Symfony' => true,
'array_syntax' => ['syntax' => 'short'],
]);
29 changes: 0 additions & 29 deletions .php_cs

This file was deleted.

35 changes: 14 additions & 21 deletions lib/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class Migrator
private $session;
private $versionCollection;
private $versionStorage;
private $actions = array(
private $actions = [
'up', 'down', 'top', 'bottom',
);
];

public function __construct(
SessionInterface $session,
Expand Down Expand Up @@ -58,14 +58,13 @@ public function initialize()
* If $to is null then all migrations will be executed.
*
* @param string|null $to Version to run until
* @param OutputInterface $output
*
* @return VersionInterface[] Executed migrations
*/
public function migrate($to, OutputInterface $output)
{
if (false === $to) {
return array();
return [];
}

$from = $this->versionStorage->getCurrentVersion();
Expand All @@ -76,17 +75,17 @@ public function migrate($to, OutputInterface $output)
$versionsToExecute = $this->versionCollection->getVersions($from, $to, $direction);

if (!$versionsToExecute) {
return array();
return [];
}

$position = 0;
$output->writeln(sprintf('<comment>%s</comment> %d version(s):', ($direction == 'up' ? 'Upgrading' : 'Reverting'), count($versionsToExecute)));
$output->writeln(sprintf('<comment>%s</comment> %d version(s):', 'up' == $direction ? 'Upgrading' : 'Reverting', count($versionsToExecute)));
foreach ($versionsToExecute as $timestamp => $version) {
$position++;
$output->writeln(sprintf(' %s [<info>%d/%d</info>]: %s', $direction == 'up' ? '+' : '-', $position, count($versionsToExecute), $timestamp));
++$position;
$output->writeln(sprintf(' %s [<info>%d/%d</info>]: %s', 'up' == $direction ? '+' : '-', $position, count($versionsToExecute), $timestamp));
$version->$direction($this->session);

if ($direction === 'down') {
if ('down' === $direction) {
$this->versionStorage->remove($timestamp);
} else {
$this->versionStorage->add($timestamp);
Expand Down Expand Up @@ -117,34 +116,28 @@ private function resolveTo($to, $from)
$to = strtolower($to);
}

if ($to === 'down') {
if ('down' === $to) {
$to = $this->versionCollection->getPreviousVersion($from);
}

if ($to === 'up') {
if ('up' === $to) {
$to = $this->versionCollection->getNextVersion($from);
}

if ($to === 'bottom') {
if ('bottom' === $to) {
$to = 0;
}

if ($to === 'top' || null === $to) {
if ('top' === $to || null === $to) {
$to = $this->versionCollection->getLatestVersion();
}

if (0 !== $to && false === strtotime($to)) {
throw new MigratorException(sprintf(
'Unknown migration action "%s". Known actions: "%s"',
$to,
implode('", "', $this->actions)
));
throw new MigratorException(sprintf('Unknown migration action "%s". Known actions: "%s"', $to, implode('", "', $this->actions)));
}

if (0 !== $to && !$this->versionCollection->has($to)) {
throw new MigratorException(sprintf(
'Unknown version "%s"', $to
));
throw new MigratorException(sprintf('Unknown version "%s"', $to));
}

return $to;
Expand Down
26 changes: 13 additions & 13 deletions lib/MigratorUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,40 @@ public static function getClassNameFromFile($file)
}

// Read entire lines to prevent keyword truncation
for ($line = 0; $line <= 20; $line++) {
for ($line = 0; $line <= 20; ++$line) {
$buffer .= fgets($fp);
}
$tokens = @token_get_all($buffer);

if (strpos($buffer, '{') === false) {
if (false === strpos($buffer, '{')) {
continue;
}

for (;$i < count($tokens);$i++) {
if ($tokens[$i][0] === T_NAMESPACE) {
for ($j = $i + 1;$j < count($tokens); $j++) {
if (\defined('T_NAME_QUALIFIED') && $tokens[$j][0] === T_NAME_QUALIFIED || $tokens[$j][0] === T_STRING) {
$namespace .= '\\' . $tokens[$j][1];
} elseif ($tokens[$j] === '{' || $tokens[$j] === ';') {
for (; $i < count($tokens); ++$i) {
if (T_NAMESPACE === $tokens[$i][0]) {
for ($j = $i + 1; $j < count($tokens); ++$j) {
if (\defined('T_NAME_QUALIFIED') && T_NAME_QUALIFIED === $tokens[$j][0] || T_STRING === $tokens[$j][0]) {
$namespace .= '\\'.$tokens[$j][1];
} elseif ('{' === $tokens[$j] || ';' === $tokens[$j]) {
break;
}
}
}

if ($tokens[$i][0] === T_CLASS) {
for ($j = $i + 1;$j < count($tokens);$j++) {
if ($tokens[$j] === '{') {
if (T_CLASS === $tokens[$i][0]) {
for ($j = $i + 1; $j < count($tokens); ++$j) {
if ('{' === $tokens[$j]) {
$class = $tokens[$i + 2][1];
}
}
}
}
};
}

if (!$class) {
return;
}

return $namespace . '\\' . $class;
return $namespace.'\\'.$class;
}
}
14 changes: 7 additions & 7 deletions lib/VersionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ public function getAllVersions()
public function getVersions($from, $to)
{
$direction = $from > $to ? 'down' : 'up';
$result = array();
$result = [];
$versions = $this->versions;

if ($from == $to) {
return array();
return [];
}

if ($direction === 'up') {
if ('up' === $direction) {
ksort($versions, SORT_STRING);
} else {
krsort($versions, SORT_STRING);
}

$found = $from === null ? true : false;
$found = null === $from ? true : false;
foreach ($versions as $timestamp => $version) {
if ($timestamp == $from) {
$found = true;

if ($direction == 'down') {
if ('down' == $direction) {
$result[$timestamp] = $version;
}

Expand All @@ -64,7 +64,7 @@ public function getVersions($from, $to)
}

if ($timestamp == $to) {
if ($direction == 'up') {
if ('up' == $direction) {
$result[$timestamp] = $version;
}
break;
Expand Down Expand Up @@ -92,7 +92,7 @@ public function getNextVersion($from)
{
$found = false;
foreach (array_keys($this->versions) as $timestamp) {
if ($from === null) {
if (null === $from) {
return $timestamp;
}

Expand Down
11 changes: 3 additions & 8 deletions lib/VersionFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ class VersionFinder
public function __construct(array $paths)
{
if (empty($paths)) {
throw new \RuntimeException(
'No paths were provided to the version finder.'
);
throw new \RuntimeException('No paths were provided to the version finder.');
}
$this->paths = $paths;
}
Expand All @@ -35,7 +33,7 @@ public function getCollection()
return $this->collection;
}

$versions = array();
$versions = [];
$finder = new Finder();
$finder->name('/Version[0-9]{12}.php/');
$finder->files();
Expand All @@ -52,10 +50,7 @@ public function getCollection()
$version = new $classFqn();

if (!$version instanceof VersionInterface) {
throw new MigratorException(sprintf(
'Version class "%s" must implement VersionInterface',
$className
));
throw new MigratorException(sprintf('Version class "%s" must implement VersionInterface', $className));
}

$versionTimestamp = substr($versionFile->getBaseName(), 7, 12);
Expand Down
4 changes: 0 additions & 4 deletions lib/VersionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,11 @@ interface VersionInterface
{
/**
* Migrate the repository up.
*
* @param SessionInterface $session
*/
public function up(SessionInterface $session);

/**
* Migrate the system down.
*
* @param SessionInterface $session
*/
public function down(SessionInterface $session);
}
10 changes: 5 additions & 5 deletions lib/VersionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function init()
[phpcrmig:versions] > nt:base
+* (phpcrmig:version)
EOT
, true);
, true);
}

$rootNode = $this->session->getRootNode();
Expand All @@ -61,21 +61,21 @@ public function getPersistedVersions()
$this->init();

$versionNodes = $this->storageNode->getNodes();
$versions = array();
$versions = [];

foreach ($versionNodes as $versionNode) {
$versions[$versionNode->getName()] = array(
$versions[$versionNode->getName()] = [
'name' => $versionNode->getName(),
'executed' => $versionNode->getPropertyValue('jcr:created'),
);
];
}

return $versions;
}

public function hasVersioningNode()
{
return $this->session->nodeExists('/' . $this->storageNodeName);
return $this->session->nodeExists('/'.$this->storageNodeName);
}

public function getCurrentVersion()
Expand Down
6 changes: 3 additions & 3 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class BaseTestCase extends TestCase

public function initPhpcr()
{
$path = __DIR__ . '/data';
$path = __DIR__.'/data';
$sfFs = new Filesystem();
$sfFs->remove($path);
$factory = new RepositoryFactoryFilesystem();
$repository = $factory->getRepository(array(
$repository = $factory->getRepository([
'path' => $path,
'search.enabled' => false,
));
]);
$credentials = new SimpleCredentials('admin', 'admin');
$this->session = $repository->login($credentials);
}
Expand Down
Loading

0 comments on commit e7f92ca

Please sign in to comment.