Skip to content

Commit c0ab0a5

Browse files
committed
add phpstan and fix issues
1 parent e7f92ca commit c0ab0a5

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed

lib/Migrator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function initialize()
5757
* If $to is 0 then all migrations will be reverted.
5858
* If $to is null then all migrations will be executed.
5959
*
60-
* @param string|null $to Version to run until
60+
* @param string|int|boolean|null $to Version to run until
6161
*
6262
* @return VersionInterface[] Executed migrations
6363
*/

lib/MigratorUtil.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MigratorUtil
2424
*/
2525
public static function getClassNameFromFile($file)
2626
{
27-
$fp = fopen($file, 'r');
27+
$fp = fopen($file, 'rb');
2828

2929
$class = $namespace = $buffer = '';
3030
$i = 0;
@@ -39,15 +39,16 @@ public static function getClassNameFromFile($file)
3939
$buffer .= fgets($fp);
4040
}
4141
$tokens = @token_get_all($buffer);
42+
$tokensCount = count($tokens);
4243

4344
if (false === strpos($buffer, '{')) {
4445
continue;
4546
}
4647

47-
for (; $i < count($tokens); ++$i) {
48+
for (; $i < $tokensCount; ++$i) {
4849
if (T_NAMESPACE === $tokens[$i][0]) {
49-
for ($j = $i + 1; $j < count($tokens); ++$j) {
50-
if (\defined('T_NAME_QUALIFIED') && T_NAME_QUALIFIED === $tokens[$j][0] || T_STRING === $tokens[$j][0]) {
50+
for ($j = $i + 1; $j < $tokensCount; ++$j) {
51+
if (T_STRING === $tokens[$j][0] || (\defined('T_NAME_QUALIFIED') && T_NAME_QUALIFIED === $tokens[$j][0])) {
5152
$namespace .= '\\'.$tokens[$j][1];
5253
} elseif ('{' === $tokens[$j] || ';' === $tokens[$j]) {
5354
break;
@@ -56,7 +57,7 @@ public static function getClassNameFromFile($file)
5657
}
5758

5859
if (T_CLASS === $tokens[$i][0]) {
59-
for ($j = $i + 1; $j < count($tokens); ++$j) {
60+
for ($j = $i + 1; $j < $tokensCount; ++$j) {
6061
if ('{' === $tokens[$j]) {
6162
$class = $tokens[$i + 2][1];
6263
}
@@ -66,7 +67,7 @@ public static function getClassNameFromFile($file)
6667
}
6768

6869
if (!$class) {
69-
return;
70+
throw new \RuntimeException('Could not determine class for migration');
7071
}
7172

7273
return $namespace.'\\'.$class;

lib/VersionCollection.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function getLatestVersion()
8686
/**
8787
* Return the version after the given version.
8888
*
89-
* @param string $from
89+
* @param string|null $from
9090
*/
9191
public function getNextVersion($from)
9292
{
@@ -105,8 +105,6 @@ public function getNextVersion($from)
105105
return $timestamp;
106106
}
107107
}
108-
109-
return;
110108
}
111109

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

127125
return 0;
128126
}
129-
130-
private function normalizeTs($ts)
131-
{
132-
return $ts ? $ts : null;
133-
}
134127
}

lib/VersionStorage.php

+12-10
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111

1212
namespace PHPCR\Migrations;
1313

14+
use PHPCR\NodeInterface;
1415
use PHPCR\SessionInterface;
1516

1617
class VersionStorage
1718
{
1819
private $session;
1920
private $storageNodeName;
2021
private $initialized = false;
22+
/**
23+
* @var NodeInterface
24+
*/
25+
private $storageNode;
2126

2227
public function __construct(SessionInterface $session, $storageNodeName = 'phpcrmig:versions')
2328
{
@@ -31,8 +36,8 @@ public function init()
3136
return;
3237
}
3338

34-
$this->workspace = $this->session->getWorkspace();
35-
$nodeTypeManager = $this->workspace->getNodeTypeManager();
39+
$workspace = $this->session->getWorkspace();
40+
$nodeTypeManager = $workspace->getNodeTypeManager();
3641

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

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

50-
if ($rootNode->hasNode($this->storageNodeName)) {
51-
$storageNode = $rootNode->getNode($this->storageNodeName);
52-
} else {
53-
$storageNode = $rootNode->addNode($this->storageNodeName, 'phpcrmig:versions');
54-
}
55-
56-
$this->storageNode = $storageNode;
55+
$this->storageNode = ($rootNode->hasNode($this->storageNodeName))
56+
? $rootNode->getNode($this->storageNodeName)
57+
: $rootNode->addNode($this->storageNodeName, 'phpcrmig:versions')
58+
;
5759
}
5860

5961
public function getPersistedVersions()
@@ -85,7 +87,7 @@ public function getCurrentVersion()
8587
$versions = (array) $this->storageNode->getNodeNames();
8688

8789
if (!$versions) {
88-
return;
90+
return null;
8991
}
9092

9193
asort($versions);

phpstan.neon.dist

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: 5
3+
reportUnmatchedIgnoredErrors: false
4+
paths:
5+
- lib/

0 commit comments

Comments
 (0)