From d6853a6eeebc3d797f2693d7f518694c43e0ca33 Mon Sep 17 00:00:00 2001 From: Agung Santoso Date: Wed, 31 Aug 2016 15:53:43 +0700 Subject: [PATCH 1/3] Refactoring Document.php --- Document.php | 101 ++++++++++++++++++++++++++++++++++++--------------- User.php | 78 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+), 29 deletions(-) create mode 100644 User.php diff --git a/Document.php b/Document.php index 7803df6..ff30680 100644 --- a/Document.php +++ b/Document.php @@ -1,49 +1,92 @@ + * @copyright 2016-2017 Foo Inc. + * @license Foo License + * @link Link + */ +/** + * The Document class. + * + * @category Category + * @package Package + * @author Display Name + * @license Foo License + * @link Link + */ +class Document +{ public $user; public $name; - public function init($name, User $user) { + public $database; + + /** + * The constructor + * + * I think constructor is better than init, + * and better to make static initialization of + * database as constructor's argument + * + * @param string $name Name parameter + * @param object $user User parameter + * @param object $database Database parameter + * + * @return void + */ + public function __construct($name, User $user, Database $database) + { assert(strlen($name) > 5); $this->user = $user; $this->name = $name; + // set database here, so it can be use throughout the class + $this->database = $database; } - public function getTitle() { - $db = Database::getInstance(); - $row = $db->query('SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'); + /** + * The getTitle function. + * + * @return string document title + */ + public function getTitle() + { + // move query to different line, so that the next line isn't too long + $sql = 'SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'; + $row = $this->database->query($sql); return $row[3]; // third column in a row } - public function getContent() { - $db = Database::getInstance(); - $row = $db->query('SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'); + /** + * The getContent function + * + * @return string document content + */ + public function getContent() + { + $sql = 'SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'; + $row = $this->database->query($sql); return $row[6]; // sixth column in a row } - public static function getAllDocuments() { + /** + * The getAllDocuments function + * + * @return array all documents + */ + public function getAllDocuments() + { // to be implemented later } -} - -class User { - - public function makeNewDocument($name) { - $doc = new Document(); - $doc->init($name, $this); - return $doc; - } - - public function getMyDocuments() { - $list = array(); - foreach (Document::getAllDocuments() as $doc) { - if ($doc->user == $this) - $list[] = $doc; - } - return $list; - } - -} +} \ No newline at end of file diff --git a/User.php b/User.php new file mode 100644 index 0000000..eb97888 --- /dev/null +++ b/User.php @@ -0,0 +1,78 @@ + + * @copyright 2016-2017 Foo Inc. + * @license Foo License + * @link Link + */ + +// Document File required to make new Document +require_once 'Document.php'; + +/** + * The User class. + * + * @category Category + * @package Package + * @author Display Name + * @license Foo License + * @link Link + */ +class User +{ + protected $document; + + /** + * The constructor + * + * Better create constructor and pass Document + * as constructor argument + * + * @param Document $document document parameter + * + * @return void + */ + public function __construct(Document $document) + { + // set document here, so it can be use troughout the class + $this->document = $document; + } + + /** + * The makeNewDocument function + * + * @param string $name Name parameter + * + * @return Document New Document + */ + public function makeNewDocument($name) + { + // use document constructor to create new document + // instead of init method + $document = new Document($name, $this, $this->document->database); + return $document; + } + + /** + * The getMyDocuments function + * + * @return array list of my documents + */ + public function getMyDocuments() + { + $list = array(); + foreach ($this->document->getAllDocuments() as $doc) { + if ($doc->user == $this) { + $list[] = $doc; + } + } + return $list; + } + +} From d7d6a055ec3511aea97997c0a469e5e141156734 Mon Sep 17 00:00:00 2001 From: Agung Santoso Date: Fri, 2 Sep 2016 21:48:18 +0700 Subject: [PATCH 2/3] Refactoring Parser.java --- FileContent.java | 90 ++++++++++++++++++++++++++++++++++++++++++++++++ Parser.java | 42 ---------------------- 2 files changed, 90 insertions(+), 42 deletions(-) create mode 100644 FileContent.java delete mode 100644 Parser.java diff --git a/FileContent.java b/FileContent.java new file mode 100644 index 0000000..3ca74ac --- /dev/null +++ b/FileContent.java @@ -0,0 +1,90 @@ + +package quiz; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * I change Parser to FileContent because + * Parser isn't an object and I avoid class + * using -er. + * + * @author Display Name (email@domain.com) + * @version $Id$ + * @since 0.0 + */ +final class FileContent { + + private static File file; + + /** + * Because we're not providing any instance members. + */ + private FileContent() { + throw new AssertionError("Instantiating utility class..."); + } + + /** + * Setting file. + * @param infile File want to be set. + */ + public static synchronized void setFile(final File infile) { + file = infile; + } + + /** + * Getting file. + * @return File. + */ + public static synchronized File getFile() { + return file; + } + + /** + * Getting file content. + * @return String file content. + * @throws IOException. + */ + public static String getContent() throws IOException { + final FileInputStream instr = new FileInputStream(file); + String output = ""; + int data; + while ((data = instr.read()) > 0) { + output += (char) data; + } + return output; + } + + /** + * Getting file content without unicode. + * @return String file content without unicode. + * @throws IOException. + */ + public static String getContentWithoutUnicode() throws IOException { + final FileInputStream instr = new FileInputStream(file); + String output = ""; + int data; + final long datalimit = 0x80; + while ((data = instr.read()) > 0) { + if (data < datalimit) { + output += (char) data; + } + } + return output; + } + + /** + * Saving file content. + * @param content File content. + * @throws IOException. + */ + public static void saveContent(final String content) + throws IOException { + final FileOutputStream outstr = new FileOutputStream(file); + for (int pos = 0; pos < content.length(); pos += 1) { + outstr.write(content.charAt(pos)); + } + } +} diff --git a/Parser.java b/Parser.java deleted file mode 100644 index d6d65d3..0000000 --- a/Parser.java +++ /dev/null @@ -1,42 +0,0 @@ -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -/** - * This class is thread safe. - */ -public class Parser { - private File file; - public synchronized void setFile(File f) { - file = f; - } - public synchronized File getFile() { - return file; - } - public String getContent() throws IOException { - FileInputStream i = new FileInputStream(file); - String output = ""; - int data; - while ((data = i.read()) > 0) { - output += (char) data; - } - return output; - } - public String getContentWithoutUnicode() throws IOException { - FileInputStream i = new FileInputStream(file); - String output = ""; - int data; - while ((data = i.read()) > 0) { - if (data < 0x80) { - output += (char) data; - } - } - return output; - } - public void saveContent(String content) throws IOException { - FileOutputStream o = new FileOutputStream(file); - for (int i = 0; i < content.length(); i += 1) { - o.write(content.charAt(i)); - } - } -} From 9164c1d90f13998e794f3c1ebbbea9fc57eccfff Mon Sep 17 00:00:00 2001 From: Agung Santoso Date: Mon, 5 Sep 2016 15:23:20 +0700 Subject: [PATCH 3/3] More refactoring --- Document.php | 12 ++--- FileContent.java | 90 ----------------------------------- ParsedFile.java | 91 ++++++++++++++++++++++++++++++++++++ User.php => UserDocument.php | 14 +++--- 4 files changed, 103 insertions(+), 104 deletions(-) delete mode 100644 FileContent.java create mode 100644 ParsedFile.java rename User.php => UserDocument.php (84%) diff --git a/Document.php b/Document.php index ff30680..d76f068 100644 --- a/Document.php +++ b/Document.php @@ -55,11 +55,11 @@ public function __construct($name, User $user, Database $database) } /** - * The getTitle function. + * Get document title. * * @return string document title */ - public function getTitle() + public function title() { // move query to different line, so that the next line isn't too long $sql = 'SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'; @@ -68,11 +68,11 @@ public function getTitle() } /** - * The getContent function + * Get document content. * * @return string document content */ - public function getContent() + public function content() { $sql = 'SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'; $row = $this->database->query($sql); @@ -80,11 +80,11 @@ public function getContent() } /** - * The getAllDocuments function + * Get all document. * * @return array all documents */ - public function getAllDocuments() + public function all() { // to be implemented later } diff --git a/FileContent.java b/FileContent.java deleted file mode 100644 index 3ca74ac..0000000 --- a/FileContent.java +++ /dev/null @@ -1,90 +0,0 @@ - -package quiz; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; - -/** - * I change Parser to FileContent because - * Parser isn't an object and I avoid class - * using -er. - * - * @author Display Name (email@domain.com) - * @version $Id$ - * @since 0.0 - */ -final class FileContent { - - private static File file; - - /** - * Because we're not providing any instance members. - */ - private FileContent() { - throw new AssertionError("Instantiating utility class..."); - } - - /** - * Setting file. - * @param infile File want to be set. - */ - public static synchronized void setFile(final File infile) { - file = infile; - } - - /** - * Getting file. - * @return File. - */ - public static synchronized File getFile() { - return file; - } - - /** - * Getting file content. - * @return String file content. - * @throws IOException. - */ - public static String getContent() throws IOException { - final FileInputStream instr = new FileInputStream(file); - String output = ""; - int data; - while ((data = instr.read()) > 0) { - output += (char) data; - } - return output; - } - - /** - * Getting file content without unicode. - * @return String file content without unicode. - * @throws IOException. - */ - public static String getContentWithoutUnicode() throws IOException { - final FileInputStream instr = new FileInputStream(file); - String output = ""; - int data; - final long datalimit = 0x80; - while ((data = instr.read()) > 0) { - if (data < datalimit) { - output += (char) data; - } - } - return output; - } - - /** - * Saving file content. - * @param content File content. - * @throws IOException. - */ - public static void saveContent(final String content) - throws IOException { - final FileOutputStream outstr = new FileOutputStream(file); - for (int pos = 0; pos < content.length(); pos += 1) { - outstr.write(content.charAt(pos)); - } - } -} diff --git a/ParsedFile.java b/ParsedFile.java new file mode 100644 index 0000000..1e527ac --- /dev/null +++ b/ParsedFile.java @@ -0,0 +1,91 @@ + +package quiz; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * I change Parser to ParsedFile because + * Parser isn't an object and I avoid class + * using -er. + * + * @author Display Name (email@domain.com) + * @version $Id$ + * @since 0.0 + */ +final class ParsedFile { + + private File file; + + /** + * Because we're not providing any instance members. + * @param fil File input. + */ + public void parsedFile(final File fil) { + this.file = fil; + } + + /** + * Getting file. + * @return File. + */ + public static synchronized File fileOf() { + return this.file; + } + + /** + * Getting content with options. + * @param isunicode Option to return ascii or unicode content. + * @return String file content. + * @throws IOException. + */ + public String content(final boolean isunicode) throws IOException { + final FileInputStream stream = new FileInputStream(this.file); + String output = ""; + int data; + final long limit = 0x80; + while ((data = stream.read()) > 0) { + if (isunicode) { + if (data < limit) { + output += (char) data; + } + } else { + output += (char) data; + } + } + return output; + } + + /** + * Getting content. + * @return String file content without unicode. + * @throws IOException. + */ + public String content() throws IOException { + return this.content(false); + } + + /** + * Getting unicode content. + * @return String file content without unicode. + * @throws IOException. + */ + public String unicodeContent() throws IOException { + return this.content(true); + } + + /** + * Saving file content. + * @param content File content. + * @throws IOException. + */ + public void save(final String content) + throws IOException { + final FileOutputStream stream = new FileOutputStream(this.file); + for (int pos = 0; pos < content.length(); pos += 1) { + stream.write(content.charAt(pos)); + } + } +} diff --git a/User.php b/UserDocument.php similarity index 84% rename from User.php rename to UserDocument.php index eb97888..50adc0b 100644 --- a/User.php +++ b/UserDocument.php @@ -24,13 +24,11 @@ * @license Foo License * @link Link */ -class User +class UserDocument { protected $document; /** - * The constructor - * * Better create constructor and pass Document * as constructor argument * @@ -45,13 +43,13 @@ public function __construct(Document $document) } /** - * The makeNewDocument function + * Create user document. * * @param string $name Name parameter * * @return Document New Document */ - public function makeNewDocument($name) + public function create($name) { // use document constructor to create new document // instead of init method @@ -60,14 +58,14 @@ public function makeNewDocument($name) } /** - * The getMyDocuments function + * List of user document. * * @return array list of my documents */ - public function getMyDocuments() + public function listOf() { $list = array(); - foreach ($this->document->getAllDocuments() as $doc) { + foreach ($this->document->all() as $doc) { if ($doc->user == $this) { $list[] = $doc; }