From 7ffcb763e95e1e69f88cf76885667ffb98c29ce7 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Mon, 14 Mar 2016 14:22:37 +0100 Subject: [PATCH] Adding comments and parameters --- Utils/Compression/CompressionInterface.php | 54 +++++ Utils/Compression/LZW.php | 62 ++++-- Utils/Encoding/EncodingInterface.php | 54 +++++ Utils/Encoding/Gray.php | 34 ++- Utils/Git/Author.php | 64 +++++- Utils/Git/Branch | 56 ++++- Utils/Git/Commit.php | 244 ++++++++++++++++++++- Utils/Git/Git.php | 71 +++++- Utils/Git/Repository.php | 145 +++++++++++- Utils/MultiMap.php | 18 ++ Utils/RnG/ArrayRandomize.php | 73 ++++++ Utils/RnG/LinearCongruentialGenerator.php | 60 +++++ 12 files changed, 895 insertions(+), 40 deletions(-) create mode 100644 Utils/RnG/ArrayRandomize.php create mode 100644 Utils/RnG/LinearCongruentialGenerator.php diff --git a/Utils/Compression/CompressionInterface.php b/Utils/Compression/CompressionInterface.php index e69de29bb..5fea09404 100644 --- a/Utils/Compression/CompressionInterface.php +++ b/Utils/Compression/CompressionInterface.php @@ -0,0 +1,54 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Utils\Compression; + +/** + * Compression Interface + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +interface CompressionInterface +{ + /** + * Compresses source text + * + * @param string $source Source text to compress + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function compress(string $source) : string; + + /** + * Decompresses text + * + * @param string $compressed Compressed text to decompress + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function decompress(string $compressed) : string; +} \ No newline at end of file diff --git a/Utils/Compression/LZW.php b/Utils/Compression/LZW.php index 7af511acb..1cf0dfca8 100644 --- a/Utils/Compression/LZW.php +++ b/Utils/Compression/LZW.php @@ -1,10 +1,41 @@ + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Utils\Compression; +/** + * LZW compression class + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ class LZW implements CompressionInterface { - public static function compress($unc) { - $i;$c;$wc; - $w = ""; + + /** + * {@inheritdoc} + */ + public function compress(string $source) : string + { + $wc = ''; + $w = ''; $dictionary = []; $result = []; $dictSize = 256; @@ -13,8 +44,8 @@ class LZW implements CompressionInterface $dictionary[chr($i)] = $i; } - for ($i = 0; $i < strlen($unc); $i++) { - $c = $unc[$i]; + for ($i = 0; $i < strlen($source); $i++) { + $c = $source[$i]; $wc = $w.$c; if (array_key_exists($w. $c, $dictionary)) { @@ -26,29 +57,32 @@ class LZW implements CompressionInterface } } - if ($w !== "") { + if ($w !== '') { array_push($result,$dictionary[$w]); } - return implode(",",$result); + return implode(',',$result); } - public static function decompress($com) { - $com = explode(",",$com); - $i;$w;$k;$result; + /** + * {@inheritdoc} + */ + public function decompress(string $compressed) : string + { + $compressed = explode(',', $compressed); $dictionary = []; - $entry = ""; + $entry = ''; $dictSize = 256; for ($i = 0; $i < 256; $i++) { $dictionary[$i] = chr($i); } - $w = chr($com[0]); + $w = chr($compressed[0]); $result = $w; - for ($i = 1; $i < count($com);$i++) { - $k = $com[$i]; + for ($i = 1; $i < count($compressed);$i++) { + $k = $compressed[$i]; if ($dictionary[$k]) { $entry = $dictionary[$k]; diff --git a/Utils/Encoding/EncodingInterface.php b/Utils/Encoding/EncodingInterface.php index e69de29bb..ca331d55d 100644 --- a/Utils/Encoding/EncodingInterface.php +++ b/Utils/Encoding/EncodingInterface.php @@ -0,0 +1,54 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Utils\Encoding; + +/** + * Encoding Interface + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +interface EncodingInterface +{ + /** + * Encode source text + * + * @param string $source Source text to decode + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function decode($source); + + /** + * Dedecodes text + * + * @param string $decoded decoded text to dedecode + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function decode($decoded); +} \ No newline at end of file diff --git a/Utils/Encoding/Gray.php b/Utils/Encoding/Gray.php index c82e9fa4c..ea6ca3475 100644 --- a/Utils/Encoding/Gray.php +++ b/Utils/Encoding/Gray.php @@ -1,11 +1,43 @@ + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Utils\Encoding; -class Gray { +/** + * Gray encoding class + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +final class Gray { + /** + * {@inheritdoc} + */ public static function encode(int $source) : int { return $source ^ ($source >> 1); } + /** + * {@inheritdoc} + */ public static function decode(int $gray) : int { $source = $gray; diff --git a/Utils/Git/Author.php b/Utils/Git/Author.php index 23234fd77..44d66ab6f 100644 --- a/Utils/Git/Author.php +++ b/Utils/Git/Author.php @@ -1,23 +1,85 @@ + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ namespace phpOMS\Utils\Git; +/** + * Gray encoding class + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ class Author { + /** + * Name. + * + * @var string + * @since 1.0.0 + */ private $name = ''; + + /** + * Email. + * + * @var string + * @since 1.0.0 + */ private $email = ''; + /** + * Constructor + * + * @param string $name Author name + * @param string $email Author email + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function __construct(string $name, string $email) { $this->name = $name; $this->email = $email; } + /** + * Get name + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getName() : string { return $name; } + /** + * Get email + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getEmail() : string { return $email; diff --git a/Utils/Git/Branch b/Utils/Git/Branch index c7a0af4fc..081890ac3 100644 --- a/Utils/Git/Branch +++ b/Utils/Git/Branch @@ -1,21 +1,75 @@ + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ namespace phpOMS\Utils\Git; +/** + * Gray encoding class + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ class Branch { + /** + * Name. + * + * @var string + * @since 1.0.0 + */ private $name = ''; + /** + * Constructor + * + * @param string $name Branch name + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function __construct(string $name) { $this->name = $name; } + /** + * Set branch name + * + * @param string $name Branch name + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function setName(string $name) { $this->name = $name; } + /** + * Get name + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getName() : string { return $this->name; diff --git a/Utils/Git/Commit.php b/Utils/Git/Commit.php index b7e101c27..031f146cf 100644 --- a/Utils/Git/Commit.php +++ b/Utils/Git/Commit.php @@ -1,35 +1,143 @@ + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ namespace phpOMS\Utils\Git; +/** + * Gray encoding class + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ class Commit { + /** + * Hash. + * + * @var string + * @since 1.0.0 + */ private $id = ''; + + /** + * Author. + * + * @var Author + * @since 1.0.0 + */ private $author = null; + + /** + * Branch. + * + * @var Branch + * @since 1.0.0 + */ private $branch = null; + + /** + * Tag. + * + * @var Tag + * @since 1.0.0 + */ private $tag = null; + + /** + * Commit date. + * + * @var \DateTime + * @since 1.0.0 + */ private $date = null; + + /** + * Repository. + * + * @var Repository + * @since 1.0.0 + */ private $repository = null; + + /** + * Commit message. + * + * @var string + * @since 1.0.0 + */ private $message = ''; + + /** + * Files. + * + * @var string[] + * @since 1.0.0 + */ private $files = []; + /** + * Constructor + * + * @param string $id Commit hash + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function __construct(string $id = '') { $author = new Author(); $branch = new Branch(); $tag = new Tag(); if(!empty($id)) { - + // todo: fill base info } } + /** + * Add file to commit. + * + * @param string $path File path + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function addFile(string $path) { if(!isset($this->files[$path])) { $this->files[$path] = []; } } - public function addChange(string $path, int $line, string $old, string $new) + /** + * Add change. + * + * @param string $path File path + * @param int $line Line number + * @param string $old Old line + * @param string $new New line + * + * @throws + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + private function addChange(string $path, int $line, string $old, string $new) { if(!isset($this->files[$path])) { throw new \Exception(); @@ -42,64 +150,180 @@ class Commit } } + /** + * Set commit message. + * + * @param string $path File path + * + * @throws + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function setMessage(string $message) { $this->message = $message; } + /** + * Get commit message. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getMessage() : string { return $this->message; } - public function getFiles() : array { + /** + * Get files of this commit. + * + * @return string[] + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getFiles() : array + { return $this->files; } - public function removeFile(string $path) { + /** + * Get files of this commit. + * + * @param string $path File path + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function removeFile(string $path) : bool + { + if(isset($this->files[$path])) { + unset($this->files[$path]); + return true; + } + + return false; } + /** + * Set commit author. + * + * @param Author $author Commit author + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function setAuthor(Author $author) { $this->author = $author; } + /** + * Get commit author. + * + * @return Author + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getAuthor() : Author { return $this->author; } + /** + * Set commit branch. + * + * @param Branch $branch Commit branch + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function setBranch(Branch $branch) { $this->branch = $branch; } + /** + * Get commit branch. + * + * @return Branch + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getBranch() : Branch { return $this->branch; } + /** + * Set commit tag. + * + * @param Repository $tag Commit tag + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function setTag(Tag $tag) { $this->tag = $tag; } + /** + * Get commit tag. + * + * @return Tag + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getTag() : Tag { return $this->tag; } + /** + * Get commit date. + * + * @return \DateTime + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getDate() : \DateTime { - return $this->date; + return $this->date ?? new \DateTime('now'); } - public function setRepository(Reporsitory $repository) + /** + * Set commit repository. + * + * @param Repository $repository Commit repository + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function setRepository(Repository $repository) { $this->repository = $repository; } - public function getRepository() : Reporsitory + /** + * Get commit repository. + * + * @return Repository + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getRepository() : Repository { - retrun $this->repository; + return $this->repository; } -} \ No newline at end of file +} diff --git a/Utils/Git/Git.php b/Utils/Git/Git.php index 051ec2005..5bed695e2 100644 --- a/Utils/Git/Git.php +++ b/Utils/Git/Git.php @@ -1,19 +1,80 @@ + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ namespace phpOMS\Utils\Git; +/** + * Gray encoding class + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ class Git { + /** + * Git path. + * + * @var string + * @since 1.0.0 + */ protected static $bin = '/usr/bin/git'; - public static function setBin(string $path) { - self::$bin = $path; + /** + * Set git binary. + * + * @param string $path Git path + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function setBin(string $path) + { + if(realpath($path) === false) { + throw new \PathException($path) + } + + self::$bin = realpath($path); } - public static function getBin() : string { + /** + * Get git binary. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function getBin() : string + { return self::$bin; } - public static function test() : bool { + /** + * Test git. + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function test() : bool + { $pipes = []; $resource = proc_open(Git::getBin(), [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); diff --git a/Utils/Git/Repository.php b/Utils/Git/Repository.php index e5eb399b2..8811b063d 100644 --- a/Utils/Git/Repository.php +++ b/Utils/Git/Repository.php @@ -1,18 +1,79 @@ + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ namespace phpOMS\Utils\Git; +/** + * Repository class + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ class Repository { + /** + * Repository path. + * + * @var string + * @since 1.0.0 + */ private $path = ''; + + /** + * Bare repository. + * + * @var bool + * @since 1.0.0 + */ private $bare = false; + + /** + * Env variables. + * + * @var array + * @since 1.0.0 + */ private $envOptions = []; + /** + * Constructor + * + * @param string $path Repository path + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function __construct(string $path) { $this->setPath($path); } + /** + * Create repository + * + * @param string $source Create repository from source (optional, can be remote) + * @param bool $bare Bare repository + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function create(string $source = null, bool $bare = false) { if (!is_dir($this->path) || file_exists($this->path . '/.git')) { @@ -26,10 +87,18 @@ class Repository } } + /** + * Set repository path. + * + * @param string $path Path to repository + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ private function setPath(string $path) { - if (!is_dir($this->path)) { - throw new \Exception('Is not a directory'); + if (!is_dir($path)) { + throw new \PathException($path); } $this->path = realpath($path); @@ -46,7 +115,15 @@ class Repository } } - private function run($cmd) + /** + * Run git command. + * + * @param string $cmd Command to run + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + private function run(string $cmd) : string { $cmd = Git::getBin() . ' ' . $cmd; $pipes = []; @@ -81,11 +158,27 @@ class Repository return $stdout; } + /** + * Get directory path. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getDirectoryPath() : string { return $this->bare ? $this->path : $this->path . '/.git'; } + /** + * Get status. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function status() : string { return $this->run('status'); @@ -259,12 +352,20 @@ class Repository $this->envOptions[$key] = $value; } - public function getMessage(string $commit) : string + public function getCommit(string $commit) : Commit { return $this->run('log --format=%B -n 1 ' . $commit); } - public function getCommitsCount() : string + /** + * Count Commits. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getCommitsCount(\DateTime $start = null, \DateTime $end = null, Author $author = null) : int { $result = $this->normalizeResult($this->run('shortlog -s -n --all')); @@ -276,16 +377,44 @@ class Repository str_replace('\t', '|', trim($result)); } - public function getCommitsBy(string $author, \DateTime $start, \DateTime $end) : string + /** + * Get commits by author. + * + * @param Author $author Commits by author + * @param \DateTime $start Commits from + * @param \DateTime $end Commits to + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getCommitsBy(Author $author, \DateTime $start = null, \DateTime $end) : array { - return $this->run('git log --before="' . $end->format('Y-m-d') . '" --after="' . $start->format('Y-m-d') . '" --author="' . $author . '" --reverse --pretty=format:"%cd %h %s" --date=short'); + return $this->run('git log --before="' . $end->format('Y-m-d') . '" --after="' . $start->format('Y-m-d') . '" --author="' . $author->getName() . '" --reverse --pretty=format:"%cd %h %s" --date=short'); } + /** + * Get remote. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getRemote() : string { return $this->run('config --get remote.origin.url'); } + /** + * Get newest commit. + * + * @return Commit + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getNewest() : string { return $this->run('log --name-status HEAD^..HEAD'); diff --git a/Utils/MultiMap.php b/Utils/MultiMap.php index 191d9fa22..73e19f428 100644 --- a/Utils/MultiMap.php +++ b/Utils/MultiMap.php @@ -45,10 +45,28 @@ class MultiMap implements \Countable */ private $keys = []; + /** + * UID. + * + * @var int[] + * @since 1.0.0 + */ private $uids = []; + /** + * Key type. + * + * @var int + * @since 1.0.0 + */ private $keyType = KeyType::LOOSE; + /** + * Order type. + * + * @var int + * @since 1.0.0 + */ private $orderType = OrderType::LOOSE; /** diff --git a/Utils/RnG/ArrayRandomize.php b/Utils/RnG/ArrayRandomize.php new file mode 100644 index 000000000..90dcb372a --- /dev/null +++ b/Utils/RnG/ArrayRandomize.php @@ -0,0 +1,73 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\Utils\RnG; + +/** + * Array randomizer class + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class ArrayRandomize +{ + /** + * Yates array shuffler. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function yates(array $arr) : array + { + $shuffled = []; + + while($arr){ + $rnd = array_rand($arr); + $shuffled[] = $arr[$rnd]; + array_splice($arr, $rnd, 1); + } + + return $shuffled; + } + + /** + * Knuths array shuffler. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function knuth(array $arr) : array + { + $shuffled = []; + + for($i = count($arr)-1; $i > 0; $i--){ + $rnd = mt_rand(0, $i); + $shuffled[$i] = $arr[$rnd]; + $shuffled[$rand] = $arr[$i]; + } + + return $shuffled; + } +} \ No newline at end of file diff --git a/Utils/RnG/LinearCongruentialGenerator.php b/Utils/RnG/LinearCongruentialGenerator.php new file mode 100644 index 000000000..e43a1157b --- /dev/null +++ b/Utils/RnG/LinearCongruentialGenerator.php @@ -0,0 +1,60 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ + +namespace phpOMS\Utils\RnG; + +/** + * Linear congruential generator class + * + * @category Framework + * @package phpOMS\Asset + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class LinearCongruentialGenerator +{ + /** + * BSD random number + * + * @return \Closure + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function bsd(int $seed) + { + return function() use(&$seed) { + return $seed = (1103515245 * $seed + 12345) % (1 << 31); + } + } + + /** + * MS random number + * + * @return \Closure + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function msvcrt(int $seed) { + return function() use (&$seed) { + return ($seed = (214013 * $seed + 2531011) % (1 << 31)) >> 16; + }; + } +}