Skip to content

Commit

Permalink
add phpstan and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Mar 16, 2024
1 parent e7f92ca commit c0ab0a5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lib/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function initialize()
* If $to is 0 then all migrations will be reverted.
* If $to is null then all migrations will be executed.
*
* @param string|null $to Version to run until
* @param string|int|boolean|null $to Version to run until
*
* @return VersionInterface[] Executed migrations
*/
Expand Down
13 changes: 7 additions & 6 deletions lib/MigratorUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MigratorUtil
*/
public static function getClassNameFromFile($file)
{
$fp = fopen($file, 'r');
$fp = fopen($file, 'rb');

$class = $namespace = $buffer = '';
$i = 0;
Expand All @@ -39,15 +39,16 @@ public static function getClassNameFromFile($file)
$buffer .= fgets($fp);
}
$tokens = @token_get_all($buffer);
$tokensCount = count($tokens);

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

for (; $i < count($tokens); ++$i) {
for (; $i < $tokensCount; ++$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]) {
for ($j = $i + 1; $j < $tokensCount; ++$j) {
if (T_STRING === $tokens[$j][0] || (\defined('T_NAME_QUALIFIED') && T_NAME_QUALIFIED === $tokens[$j][0])) {
$namespace .= '\\'.$tokens[$j][1];
} elseif ('{' === $tokens[$j] || ';' === $tokens[$j]) {
break;
Expand All @@ -56,7 +57,7 @@ public static function getClassNameFromFile($file)
}

if (T_CLASS === $tokens[$i][0]) {
for ($j = $i + 1; $j < count($tokens); ++$j) {
for ($j = $i + 1; $j < $tokensCount; ++$j) {
if ('{' === $tokens[$j]) {
$class = $tokens[$i + 2][1];
}
Expand All @@ -66,7 +67,7 @@ public static function getClassNameFromFile($file)
}

if (!$class) {
return;
throw new \RuntimeException('Could not determine class for migration');
}

return $namespace.'\\'.$class;
Expand Down
9 changes: 1 addition & 8 deletions lib/VersionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function getLatestVersion()
/**
* Return the version after the given version.
*
* @param string $from
* @param string|null $from
*/
public function getNextVersion($from)
{
Expand All @@ -105,8 +105,6 @@ public function getNextVersion($from)
return $timestamp;
}
}

return;
}

/**
Expand All @@ -126,9 +124,4 @@ public function getPreviousVersion($from)

return 0;
}

private function normalizeTs($ts)
{
return $ts ? $ts : null;
}
}
22 changes: 12 additions & 10 deletions lib/VersionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@

namespace PHPCR\Migrations;

use PHPCR\NodeInterface;
use PHPCR\SessionInterface;

class VersionStorage
{
private $session;
private $storageNodeName;
private $initialized = false;
/**
* @var NodeInterface
*/
private $storageNode;

public function __construct(SessionInterface $session, $storageNodeName = 'phpcrmig:versions')
{
Expand All @@ -31,8 +36,8 @@ public function init()
return;
}

$this->workspace = $this->session->getWorkspace();
$nodeTypeManager = $this->workspace->getNodeTypeManager();
$workspace = $this->session->getWorkspace();
$nodeTypeManager = $workspace->getNodeTypeManager();

if (!$nodeTypeManager->hasNodeType('phpcrmig:version')) {
$nodeTypeManager->registerNodeTypesCnd(<<<EOT
Expand All @@ -47,13 +52,10 @@ public function init()

$rootNode = $this->session->getRootNode();

if ($rootNode->hasNode($this->storageNodeName)) {
$storageNode = $rootNode->getNode($this->storageNodeName);
} else {
$storageNode = $rootNode->addNode($this->storageNodeName, 'phpcrmig:versions');
}

$this->storageNode = $storageNode;
$this->storageNode = ($rootNode->hasNode($this->storageNodeName))
? $rootNode->getNode($this->storageNodeName)
: $rootNode->addNode($this->storageNodeName, 'phpcrmig:versions')
;
}

public function getPersistedVersions()
Expand Down Expand Up @@ -85,7 +87,7 @@ public function getCurrentVersion()
$versions = (array) $this->storageNode->getNodeNames();

if (!$versions) {
return;
return null;
}

asort($versions);
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 5
reportUnmatchedIgnoredErrors: false
paths:
- lib/

0 comments on commit c0ab0a5

Please sign in to comment.