Skip to content

Commit

Permalink
Merge pull request #3 from sstalle/bitwise-shift-checks
Browse files Browse the repository at this point in the history
Added bitwise shift checks
  • Loading branch information
sstalle committed Jul 29, 2015
2 parents 5eed906 + 5e6fa6a commit a9f40a3
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Infrastructure/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ContainerBuilder
'visitor.hexadecimalNumberString' => '\\Sstalle\\php7cc\\NodeVisitor\\HexadecimalNumberStringVisitor',
'visitor.escapedUnicodeCodepoint' => '\\Sstalle\\php7cc\\NodeVisitor\\EscapedUnicodeCodepointVisitor',
'visitor.arrayOrObjectValueAssignmentByReference' => '\\Sstalle\\php7cc\\NodeVisitor\\ArrayOrObjectValueAssignmentByReferenceVisitor',
'visitor.bitwiseShift' => '\\Sstalle\\php7cc\\NodeVisitor\\BitwiseShiftVisitor',
);

/**
Expand Down
36 changes: 36 additions & 0 deletions src/NodeVisitor/BitwiseShiftVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Sstalle\php7cc\NodeVisitor;

use PhpParser\Node;

class BitwiseShiftVisitor extends AbstractVisitor
{

const MIN_INT_SIZE = 32;

public function enterNode(Node $node)
{
$isLeftShift = $node instanceof Node\Expr\BinaryOp\ShiftLeft;
$isRightShift = $node instanceof Node\Expr\BinaryOp\ShiftRight;
if (!$isLeftShift && !$isRightShift) {
return;
}

$rightOperand = $node->right;
if ($rightOperand instanceof Node\Expr\UnaryMinus && $rightOperand->expr instanceof Node\Scalar\LNumber
&& $rightOperand->expr->value > 0
) {
$this->addContextMessage(
'Bitwise shift by a negative number',
$node
);
} elseif ($rightOperand instanceof Node\Scalar\LNumber && $rightOperand->value >= static::MIN_INT_SIZE) {
$this->addContextMessage(
sprintf('Bitwise shift by %d bits', $rightOperand->value),
$node
);
}
}

}
1 change: 1 addition & 0 deletions test/code/ContextCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function testMessages($name, $code, $expectedMessages)
'\\Sstalle\\php7cc\\NodeVisitor\\HexadecimalNumberStringVisitor',
'\\Sstalle\\php7cc\\NodeVisitor\\EscapedUnicodeCodepointVisitor',
'\\Sstalle\\php7cc\\NodeVisitor\\ArrayOrObjectValueAssignmentByReferenceVisitor',
'\\Sstalle\\php7cc\\NodeVisitor\\BitwiseShiftVisitor',
) as $visitorClass) {
$traverser->addVisitor(new $visitorClass());
}
Expand Down
25 changes: 25 additions & 0 deletions test/resource/bitwiseShift/bitwiseShiftByNegativeBinaryNumber.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Bitwise shift by a negative binary number
-----
<?php $a >> -0b10;
-----
Bitwise shift by a negative number
-----
<?php 1 << -0b10;
-----
Bitwise shift by a negative number
-----
<?php $a >> 0b0;
-----

-----
<?php 1 << 0b0;
-----

-----
<?php $a >> 0b1010;
-----

-----
<?php 1 << 0b1010;
-----

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Bitwise shift by a negative decimal number
-----
<?php $a >> -2;
-----
Bitwise shift by a negative number
-----
<?php 1 << -2;
-----
Bitwise shift by a negative number
-----
<?php $a >> 0;
-----

-----
<?php 1 << 0;
-----

-----
<?php $a >> 10;
-----

-----
<?php 1 << 10;
-----

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Bitwise shift by a negative hexadecimal number
-----
<?php $a >> -0x2;
-----
Bitwise shift by a negative number
-----
<?php 1 << -0x2;
-----
Bitwise shift by a negative number
-----
<?php $a >> 0x0;
-----

-----
<?php 1 << 0x0;
-----

-----
<?php $a >> 0xa;
-----

-----
<?php 1 << 0xa;
-----

25 changes: 25 additions & 0 deletions test/resource/bitwiseShift/bitwiseShiftByNegativeOctalNumber.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Bitwise shift by a negative octal number
-----
<?php $a >> -02;
-----
Bitwise shift by a negative number
-----
<?php 1 << -02;
-----
Bitwise shift by a negative number
-----
<?php $a >> 00;
-----

-----
<?php 1 << 00;
-----

-----
<?php $a >> 012;
-----

-----
<?php 1 << 012;
-----

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Bitwise shift by a binary number of bits greater or equal to minimum int size constant
-----
<?php $a >> 0b100000;
-----
Bitwise shift by 32 bits
-----
<?php 1 << 0b100000;
-----
Bitwise shift by 32 bits
-----
<?php $a >> 0b1000000;
-----
Bitwise shift by 64 bits
-----
<?php 1 << 0b1000000;
-----
Bitwise shift by 64 bits
-----
<?php $a >> 0b10000;
-----

-----
<?php 1 << 0b10000;
-----

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Bitwise shift by a decimal number of bits greater or equal to minimum int size constant
-----
<?php $a >> 32;
-----
Bitwise shift by 32 bits
-----
<?php 1 << 32;
-----
Bitwise shift by 32 bits
-----
<?php $a >> 64;
-----
Bitwise shift by 64 bits
-----
<?php 1 << 64;
-----
Bitwise shift by 64 bits
-----
<?php $a >> 16;
-----

-----
<?php 1 << 16;
-----
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Bitwise shift by a hexadecimal number of bits greater or equal to minimum int size constant
-----
<?php $a >> 0x20;
-----
Bitwise shift by 32 bits
-----
<?php 1 << 0x20;
-----
Bitwise shift by 32 bits
-----
<?php $a >> 0x40;
-----
Bitwise shift by 64 bits
-----
<?php 1 << 0x40;
-----
Bitwise shift by 64 bits
-----
<?php $a >> 0x10;
-----

-----
<?php 1 << 0x10;
-----

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Bitwise shift by an octal number of bits greater or equal to minimum int size constant
-----
<?php $a >> 040;
-----
Bitwise shift by 32 bits
-----
<?php 1 << 040;
-----
Bitwise shift by 32 bits
-----
<?php $a >> 0100;
-----
Bitwise shift by 64 bits
-----
<?php 1 << 0100;
-----
Bitwise shift by 64 bits
-----
<?php $a >> 020;
-----

-----
<?php 1 << 020;
-----

0 comments on commit a9f40a3

Please sign in to comment.