Created
October 29, 2012 13:54
-
-
Save weierophinney/3973688 to your computer and use it in GitHub Desktop.
static helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| abstract class Helper | |
| { | |
| public static function createHash(Paste $paste, PasteService $service) | |
| { | |
| $seed = $paste->language . $paste->timestamp . $paste->content; | |
| do { | |
| $seed . = uniqid(); | |
| $hash = hash('sha1', $seed); | |
| } while ($service->exists($hash)); | |
| return $hash; | |
| } | |
| } | |
| class MongoPasteService implements PasteService | |
| { | |
| protected $collection; | |
| public function __construct(MongoCollection $collection) | |
| { | |
| $this->collection = $collection; | |
| } | |
| public function create(Paste $paste) | |
| { | |
| $hash = Helper::createHash($paste, $this); | |
| $paste->hash = $hash; | |
| $this->collection->save((array) $paste); | |
| return $paste; | |
| } | |
| public function exists($hash) | |
| { | |
| /* .. check if paste exists .. */ | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would have made the same, or at least a very similar, comment.
As I see it, functions introduce the same issues as static methods; they induce a hard and untestable coupling between two structural elements.
For example: if I were to write a unit test for the
createmethod in your example I would be required to test whether the has is correctly generated and thus write testing code that checks thecreateHash()static method. The same thing would happen ifcreateHash()would be a function and thus the same comments apply.Please note that this comment even applies to Traits; these are untestable, deeply coupled bits of code.
With regards to LSP and functions; in my opinion LSP is only applicable to objects as you cannot substitute methods or functions (monkeypatching not included in this statement); just objects. To even be a bit bolder: LSP applies to object oriented programming, (userland) functions are to be considered procedural programming. As such these two are hard to unite.