diff --git a/example/create_basic_chain.php b/example/create_basic_chain.php index 99a3cf5..72977ed 100644 --- a/example/create_basic_chain.php +++ b/example/create_basic_chain.php @@ -1,8 +1,9 @@ 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"; @@ -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)); diff --git a/example/load_chain.php b/example/load_chain.php index 4cbff96..1be0b81 100644 --- a/example/load_chain.php +++ b/example/load_chain.php @@ -1,14 +1,15 @@ isset($chain[$key - 1]) ? $chain[$key - 1] : null + 'previous' => isset($chain[$key - 1]) ? $chain[$key - 1] : null, ] )); } @@ -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"; } diff --git a/src/OhMyBrew/Blockchain/Block.php b/src/OhMyBrew/Blockchain/Block.php index a8de908..54151ed 100644 --- a/src/OhMyBrew/Blockchain/Block.php +++ b/src/OhMyBrew/Blockchain/Block.php @@ -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 { @@ -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(); @@ -38,7 +38,7 @@ public function __construct(?array $state, ?Block $previous = null) /** * Get the block index. * - * @return integer + * @return int */ public function getIndex() : int { @@ -48,7 +48,7 @@ public function getIndex() : int /** * Get the nonce result from mining. * - * @return null|integer + * @return null|int */ public function getNonce() : ?int { @@ -58,7 +58,7 @@ public function getNonce() : ?int /** * Get the difficulty for the mining. * - * @return integer + * @return int */ public function getDifficulty() : int { @@ -68,7 +68,7 @@ public function getDifficulty() : int /** * Get the timestamp of the block creation. * - * @return integer + * @return int */ public function getTimestamp() : int { @@ -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']; } @@ -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)) { @@ -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 */ @@ -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(), ]; } @@ -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 @@ -241,7 +242,7 @@ public function configureOptions(OptionsResolver $resolver) : void ); $resolver->setAllowedTypes( 'previous', - ['null', Block::class] + ['null', self::class] ); $resolver->setDefault( diff --git a/src/OhMyBrew/Blockchain/Blockchain.php b/src/OhMyBrew/Blockchain/Blockchain.php index f1a51b4..f95e772 100644 --- a/src/OhMyBrew/Blockchain/Blockchain.php +++ b/src/OhMyBrew/Blockchain/Blockchain.php @@ -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 { @@ -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; } @@ -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'); @@ -139,6 +139,7 @@ public function addBlock(Block $block, ?bool $validateChain = false) : ?Blockcha } $this->chain[] = $block; + return $this; } @@ -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, ]); } } diff --git a/tests/BlockTest.php b/tests/BlockTest.php index f13d3dc..1377e74 100644 --- a/tests/BlockTest.php +++ b/tests/BlockTest.php @@ -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() @@ -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()); diff --git a/tests/BlockchainTest.php b/tests/BlockchainTest.php index 578239c..b6f6bc3 100644 --- a/tests/BlockchainTest.php +++ b/tests/BlockchainTest.php @@ -2,7 +2,7 @@ namespace OhMyBrew\Blockchain; -use \ReflectionClass; +use ReflectionClass; class BlockchainTest extends \PHPUnit\Framework\TestCase { @@ -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() @@ -76,7 +76,7 @@ public function shouldNotAddInvalidBlockToChain() /** * @test - * + * * Should add block if valid block. */ public function shouldAddValidBlockToChain() @@ -96,7 +96,7 @@ public function shouldAddValidBlockToChain() /** * @test - * + * * Should validate chain. */ public function shouldValidateChain() @@ -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() @@ -165,7 +165,7 @@ public function shouldInvalidateChain() /** * @test - * + * * Should confirm blocks are the same. */ public function shouldConfirmBlocksAreTheSame() @@ -183,7 +183,7 @@ public function shouldConfirmBlocksAreTheSame() /** * @test - * + * * Input of chain should match output */ public function shouldMatchInputAndOutput() @@ -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, ] )); }