Skip to content

Commit

Permalink
Merge pull request #1 from ohmybrew/analysis-8P0yao
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
gnikyt committed Feb 23, 2018
2 parents 854d966 + 8b65130 commit eca70da
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 52 deletions.
17 changes: 9 additions & 8 deletions example/create_basic_chain.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php
require(__DIR__.'/../vendor/autoload.php');

use OhMyBrew\Blockchain\Blockchain;
require __DIR__.'/../vendor/autoload.php';

use OhMyBrew\Blockchain\Block;
use OhMyBrew\Blockchain\Blockchain;

// Get the number of runs
if (!isset($argv[1])) {
Expand All @@ -11,10 +12,10 @@
$runs = intval($argv[1]);

// Color setup
$RED="\033[0;31m";
$BLUE="\033[0;34m";
$YELLOW="\033[1;33m";
$NC="\033[0m";
$RED = "\033[0;31m";
$BLUE = "\033[0;34m";
$YELLOW = "\033[1;33m";
$NC = "\033[0m";

// Create chain
$bc = new Blockchain();
Expand All @@ -24,7 +25,7 @@
for ($i = 0; $i < $runs; $i++) {
// Build block
$block = $bc->buildBlock(5, "Hello World {$i}");
echo $RED.">>> ".($i === 0 ? "GENESIS block" : "Block #{$i}") . " built{$NC}\n";
echo $RED.'>>> '.($i === 0 ? 'GENESIS block' : "Block #{$i}")." built{$NC}\n";

// Mine it and create a hash
echo "{$BLUE}Mining...{$NC}\n";
Expand All @@ -40,7 +41,7 @@

// Done, output results
$bcHash = hash('sha256', json_encode($bc->getChain()));
echo "Blockchain hash is ".$bcHash." with ".intval($argv[1])." valid blocks added to the chain.\n";
echo 'Blockchain hash is '.$bcHash.' with '.intval($argv[1])." valid blocks added to the chain.\n";

// Write chain to file
file_put_contents(__DIR__."/{$bcHash}.json", json_encode($bc->getChain(), JSON_PRETTY_PRINT));
17 changes: 9 additions & 8 deletions example/load_chain.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php
require(__DIR__.'/../vendor/autoload.php');

use OhMyBrew\Blockchain\Blockchain;
require __DIR__.'/../vendor/autoload.php';

use OhMyBrew\Blockchain\Block;
use OhMyBrew\Blockchain\Blockchain;

// Color setup
$RED="\033[0;31m";
$BLUE="\033[0;34m";
$YELLOW="\033[1;33m";
$NC="\033[0m";
$RED = "\033[0;31m";
$BLUE = "\033[0;34m";
$YELLOW = "\033[1;33m";
$NC = "\033[0m";

// Locate the blockchain data and decode it
if (!isset($argv[1])) {
Expand All @@ -24,7 +25,7 @@
$chain[] = new Block(array_merge(
$blockData,
[
'previous' => isset($chain[$key - 1]) ? $chain[$key - 1] : null
'previous' => isset($chain[$key - 1]) ? $chain[$key - 1] : null,
]
));
}
Expand All @@ -36,7 +37,7 @@
// Verify the chain
if ($bc->isValidChain()) {
echo "Chain is: {$BLUE}Valid{$NC}\n";
echo "Blockchain hash is ".hash('sha256', json_encode($chain)) . " which matches the file input\n";
echo 'Blockchain hash is '.hash('sha256', json_encode($chain))." which matches the file input\n";
} else {
echo "Chain is: {$RED}Invalid{$NC}\n";
}
37 changes: 19 additions & 18 deletions src/OhMyBrew/Blockchain/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace OhMybrew\Blockchain;

use \JsonSerializable;
use Symfony\Component\OptionsResolver\OptionsResolver;
use JsonSerializable;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class Block implements JsonSerializable
{
Expand All @@ -19,7 +19,7 @@ class Block implements JsonSerializable
*
* @return Block
*/
public function __construct(?array $state, ?Block $previous = null)
public function __construct(?array $state, ?self $previous = null)
{
if (!isset(self::$resolver)) {
self::$resolver = new OptionsResolver();
Expand All @@ -38,7 +38,7 @@ public function __construct(?array $state, ?Block $previous = null)
/**
* Get the block index.
*
* @return integer
* @return int
*/
public function getIndex() : int
{
Expand All @@ -48,7 +48,7 @@ public function getIndex() : int
/**
* Get the nonce result from mining.
*
* @return null|integer
* @return null|int
*/
public function getNonce() : ?int
{
Expand All @@ -58,7 +58,7 @@ public function getNonce() : ?int
/**
* Get the difficulty for the mining.
*
* @return integer
* @return int
*/
public function getDifficulty() : int
{
Expand All @@ -68,7 +68,7 @@ public function getDifficulty() : int
/**
* Get the timestamp of the block creation.
*
* @return integer
* @return int
*/
public function getTimestamp() : int
{
Expand Down Expand Up @@ -106,11 +106,11 @@ public function getPreviousHash() : ?string
}

/**
* Get the previous block, if available
* Get the previous block, if available.
*
* @return null|Block
*/
public function getPrevious() : ?Block
public function getPrevious() : ?self
{
return $this->state['previous'];
}
Expand Down Expand Up @@ -147,7 +147,7 @@ public function generateHash(?bool $assign = false) : string
*
* @return Block
*/
public function mine() : Block
public function mine() : self
{
$nonce = 0;
while (!$this->validateNonce($nonce)) {
Expand All @@ -161,7 +161,7 @@ public function mine() : Block
/**
* Validates an nonce.
*
* @param null|integer $nonce The new nonce value to test
* @param null|int $nonce The new nonce value to test
*
* @return bool
*/
Expand Down Expand Up @@ -203,13 +203,13 @@ public function jsonSerialize() : array
protected function jsonSerializeData() : array
{
return [
'index' => $this->getIndex(),
'nonce' => $this->getNonce(),
'difficulty' => $this->getDifficulty(),
'timestamp' => $this->getTimestamp(),
'data' => $this->getData(),
'index' => $this->getIndex(),
'nonce' => $this->getNonce(),
'difficulty' => $this->getDifficulty(),
'timestamp' => $this->getTimestamp(),
'data' => $this->getData(),
'previous_hash' => $this->getPreviousHash(),
'hash' => $this->getHash()
'hash' => $this->getHash(),
];
}

Expand All @@ -230,6 +230,7 @@ protected function hashAlgo() : string
* Typing out and remembering a list of arguments for the constructor.
*
* @param OptionsResolver $resolver
*
* @return void
*/
public function configureOptions(OptionsResolver $resolver) : void
Expand All @@ -241,7 +242,7 @@ public function configureOptions(OptionsResolver $resolver) : void
);
$resolver->setAllowedTypes(
'previous',
['null', Block::class]
['null', self::class]
);

$resolver->setDefault(
Expand Down
15 changes: 8 additions & 7 deletions src/OhMyBrew/Blockchain/Blockchain.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace OhMyBrew\Blockchain;

use \Exception;
use \IteratorAggregate;
use \ArrayIterator;
use OhMyBrew\Blockchain\Block;
use ArrayIterator;
use Exception;
use IteratorAggregate;

class Blockchain implements IteratorAggregate
{
Expand Down Expand Up @@ -53,6 +52,7 @@ public function getIterator() : ArrayIterator
public function getPreviousBlock(?int $index = null) : ?Block
{
$target = is_null($index) ? count($this->getChain()) - 1 : $index - 1;

return $target >= 0 ? $this->getChain()[$target] : null;
}

Expand Down Expand Up @@ -128,7 +128,7 @@ public function isValid() : bool
*
* @return Blockchain|Exception
*/
public function addBlock(Block $block, ?bool $validateChain = false) : ?Blockchain
public function addBlock(Block $block, ?bool $validateChain = false) : ?self
{
if (!$this->isValidBlock($block)) {
throw new Exception('Block not valid, cannot add block to chain');
Expand All @@ -139,6 +139,7 @@ public function addBlock(Block $block, ?bool $validateChain = false) : ?Blockcha
}

$this->chain[] = $block;

return $this;
}

Expand All @@ -155,8 +156,8 @@ public function buildBlock(int $difficulty, string $data, ?string $blockClass =
{
return new $blockClass([
'difficulty' => $difficulty,
'previous' => $this->getPreviousBlock(),
'data' => $data
'previous' => $this->getPreviousBlock(),
'data' => $data,
]);
}
}
6 changes: 3 additions & 3 deletions tests/BlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function shouldRequireArrayOnConstruct()
* @test
* @expectedException Symfony\Component\OptionsResolver\Exception\MissingOptionsException
* @expectedExcepionMessage The required option "difficulty" is missing.
*
*
* Should require difficulty set for mining.
*/
public function shouldRequireDifficulty()
Expand Down Expand Up @@ -139,8 +139,8 @@ public function shouldMineBlock()
{
$block = new Block([
'difficulty' => 1,
'data' => 'Hello World',
'timestamp' => 1519403271 // So we can keep the mining result constant for this data/block
'data' => 'Hello World',
'timestamp' => 1519403271, // So we can keep the mining result constant for this data/block
]);

$this->assertFalse($block->isMined());
Expand Down
16 changes: 8 additions & 8 deletions tests/BlockchainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace OhMyBrew\Blockchain;

use \ReflectionClass;
use ReflectionClass;

class BlockchainTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -64,7 +64,7 @@ public function shouldCreateBlock()
* @test
* @expectedException \Exception
* @exceptedExceptionMessage Block not valid, cannot add block to chain
*
*
* Should not add block to chain who hasnt been mined or invalid.
*/
public function shouldNotAddInvalidBlockToChain()
Expand All @@ -76,7 +76,7 @@ public function shouldNotAddInvalidBlockToChain()

/**
* @test
*
*
* Should add block if valid block.
*/
public function shouldAddValidBlockToChain()
Expand All @@ -96,7 +96,7 @@ public function shouldAddValidBlockToChain()

/**
* @test
*
*
* Should validate chain.
*/
public function shouldValidateChain()
Expand All @@ -117,7 +117,7 @@ public function shouldValidateChain()
* @test
* @expectedException \Exception
* @expectedExceptionMessage Blockchain is invalid, cannot add block to chain
*
*
* Should invalidate chain (tampering maybe?)
*/
public function shouldInvalidateChain()
Expand Down Expand Up @@ -165,7 +165,7 @@ public function shouldInvalidateChain()

/**
* @test
*
*
* Should confirm blocks are the same.
*/
public function shouldConfirmBlocksAreTheSame()
Expand All @@ -183,7 +183,7 @@ public function shouldConfirmBlocksAreTheSame()

/**
* @test
*
*
* Input of chain should match output
*/
public function shouldMatchInputAndOutput()
Expand All @@ -193,7 +193,7 @@ public function shouldMatchInputAndOutput()
$chain[] = new Block(array_merge(
$blockData,
[
'previous' => isset($chain[$key - 1]) ? $chain[$key - 1] : null
'previous' => isset($chain[$key - 1]) ? $chain[$key - 1] : null,
]
));
}
Expand Down

0 comments on commit eca70da

Please sign in to comment.