Complex classes like Taster often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Taster, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class Taster |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * End-of-line constants |
||
| 45 | */ |
||
| 46 | const EOL_UNIX = 'lf'; |
||
| 47 | const EOL_TRS80 = 'cr'; |
||
| 48 | const EOL_WINDOWS = 'crlf'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * ASCII character codes for "invisibles" |
||
| 52 | */ |
||
| 53 | const HORIZONTAL_TAB = 9; |
||
| 54 | const LINE_FEED = 10; |
||
| 55 | const CARRIAGE_RETURN = 13; |
||
| 56 | const SPACE = 32; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Data types -- Used within the lickQuotingStyle method |
||
| 60 | */ |
||
| 61 | const DATA_NONNUMERIC = 'nonnumeric'; |
||
| 62 | const DATA_SPECIAL = 'special'; |
||
| 63 | const DATA_UNKNOWN = 'unknown'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Placeholder strings -- hold the place of newlines and delimiters contained |
||
| 67 | * within quoted text so that the explode method doesn't split incorrectly |
||
| 68 | */ |
||
| 69 | const PLACEHOLDER_NEWLINE = '[__NEWLINE__]'; |
||
| 70 | const PLACEHOLDER_DELIM = '[__DELIM__]'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Recommended data sample size |
||
| 74 | */ |
||
| 75 | const SAMPLE_SIZE = 2500; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Column data types -- used within the lickHeader method to determine |
||
| 79 | * whether the first row contains different types of data than the rest of |
||
| 80 | * the rows (and thus, is likely a header row) |
||
| 81 | */ |
||
| 82 | // +-987 |
||
| 83 | const TYPE_NUMBER = 'number'; |
||
| 84 | // +-12.387 |
||
| 85 | const TYPE_DOUBLE = 'double'; |
||
| 86 | // I am a string. I can contain all kinds of stuff. |
||
| 87 | const TYPE_STRING = 'string'; |
||
| 88 | // 10-Jul-15, 9/1/2007, April 1st, 2006, etc. |
||
| 89 | const TYPE_DATE = 'date'; |
||
| 90 | // 10:00pm, 5pm, 13:08, etc. |
||
| 91 | const TYPE_TIME = 'time'; |
||
| 92 | // $98.96, ¥12389, £6.08, €87.00 |
||
| 93 | const TYPE_CURRENCY = 'currency'; |
||
| 94 | // 12ab44m1n2_asdf |
||
| 95 | const TYPE_ALNUM = 'alnum'; |
||
| 96 | // abababab |
||
| 97 | const TYPE_ALPHA = 'alpha'; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var CSVelte\Contract\Readable The source of data to examine |
||
| 101 | * @access protected |
||
| 102 | */ |
||
| 103 | protected $input; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Sample of CSV data to use for tasting (determining CSV flavor) |
||
| 107 | * @var string |
||
| 108 | */ |
||
| 109 | protected $sample; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Class constructor--accepts a CSV input source |
||
| 113 | * |
||
| 114 | * @param \CSVelte\Contract\Readable The source of CSV data |
||
| 115 | * @return void |
||
|
|
|||
| 116 | * @access public |
||
| 117 | * @todo It may be a good idea to skip the first line or two for the sample |
||
| 118 | * so that the header line(s) don't throw things off (with the exception |
||
| 119 | * of lickHeader() obviously) |
||
| 120 | */ |
||
| 121 | 15 | public function __construct(Readable $input) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Examine the input source and determine what "Flavor" of CSV it contains. |
||
| 129 | * The CSV format, while having an RFC (https://tools.ietf.org/html/rfc4180), |
||
| 130 | * doesn't necessarily always conform to it. And it doesn't provide meta such as the delimiting character, quote character, or what types of data are quoted. |
||
| 131 | * such as the delimiting character, quote character, or what types of data are quoted. |
||
| 132 | * are quoted. |
||
| 133 | * |
||
| 134 | * @return \CSVelte\Flavor The metadata that the CSV format doesn't provide |
||
| 135 | * @access public |
||
| 136 | * @todo Implement a lickQuote method for when lickQuoteAndDelim method fails |
||
| 137 | * @todo Should there bea lickEscapeChar method? the python module that inspired |
||
| 138 | * this library doesn't include one... |
||
| 139 | * @todo This should cache the results and only regenerate if $this->sample |
||
| 140 | * changes (or $this->input) |
||
| 141 | */ |
||
| 142 | 7 | public function lick() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Replaces all quoted columns with a blank string. I was using this method |
||
| 163 | * to prevent explode() from incorrectly splitting at delimiters and newlines |
||
| 164 | * within quotes when parsing a file. But this was before I wrote the |
||
| 165 | * replaceQuotedSpecialChars method which (at least to me) makes more sense. |
||
| 166 | * |
||
| 167 | * @param string The string to replace quoted strings within |
||
| 168 | * @return string The input string with quoted strings removed |
||
| 169 | * @access protected |
||
| 170 | * @todo Replace code that uses this method with the replaceQuotedSpecialChars |
||
| 171 | * method instead. I think it's cleaner. |
||
| 172 | */ |
||
| 173 | 7 | protected function removeQuotedStrings($data) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Examine the input source to determine which character(s) are being used |
||
| 180 | * as the end-of-line character |
||
| 181 | * |
||
| 182 | * @return string The end-of-line char for the input data |
||
| 183 | * @access protected |
||
| 184 | * @credit pulled from stackoverflow thread *tips hat to username "Harm"* |
||
| 185 | * @todo This should throw an exception if it cannot determine the line ending |
||
| 186 | * @todo I probably will make this method protected when I'm done with testing... |
||
| 187 | * @todo If there is any way for this method to fail (for instance if a file ) |
||
| 188 | * is totally empty or contains no line breaks), then it needs to throw |
||
| 189 | * a relevant TasterException |
||
| 190 | * @todo Use replaceQuotedSpecialChars rather than removeQuotedStrings() |
||
| 191 | */ |
||
| 192 | 7 | protected function lickLineEndings() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * The best way to determine quote and delimiter characters is when columns |
||
| 215 | * are quoted, often you can seek out a pattern of delim, quote, stuff, quote, delim |
||
| 216 | * but this only works if you have quoted columns. If you don't you have to |
||
| 217 | * determine these characters some other way... (see lickDelimiter) |
||
| 218 | * |
||
| 219 | * @return array A two-row array containing quotechar, delimchar |
||
| 220 | * @access protected |
||
| 221 | * @todo make protected |
||
| 222 | * @todo This should throw an exception if it cannot determine the delimiter |
||
| 223 | * this way. |
||
| 224 | * @todo This should check for any line endings not just \n |
||
| 225 | */ |
||
| 226 | 7 | protected function lickQuoteAndDelim() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Take a list of likely delimiter characters and find the one that occurs |
||
| 266 | * the most consistent amount of times within the provided data. |
||
| 267 | * |
||
| 268 | * @param string The character(s) used for newlines |
||
| 269 | * @return string One of four Flavor::QUOTING_* constants |
||
| 270 | * @see \CSVelte\Flavor for possible quote style constants |
||
| 271 | * @access protected |
||
| 272 | * @todo Refactor this method--It needs more thorough testing against a wider |
||
| 273 | * variety of CSV data to be sure it works reliably. And I'm sure there |
||
| 274 | * are many performance and logic improvements that could be made. This |
||
| 275 | * is essentially a first draft. |
||
| 276 | * @todo Use replaceQuotedSpecialChars rather than removeQuotedStrings |
||
| 277 | */ |
||
| 278 | 3 | protected function lickDelimiter($eol = "\n") |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Determine the "style" of data quoting. The CSV format, while having an RFC |
||
| 319 | * (https://tools.ietf.org/html/rfc4180), doesn't necessarily always conform |
||
| 320 | * to it. And it doesn't provide metadata such as the delimiting character, |
||
| 321 | * quote character, or what types of data are quoted. So this method makes a |
||
| 322 | * logical guess by finding which columns have been quoted (if any) and |
||
| 323 | * examining their data type. Most often, CSV files will only use quotes |
||
| 324 | * around columns that contain special characters such as the dilimiter, |
||
| 325 | * the quoting character, newlines, etc. (we refer to this style as ) |
||
| 326 | * QUOTE_MINIMAL), but some quote all columns that contain nonnumeric data |
||
| 327 | * (QUOTE_NONNUMERIC). Then there are CSV files that quote all columns |
||
| 328 | * (QUOTE_ALL) and those that quote none (QUOTE_NONE). |
||
| 329 | * |
||
| 330 | * @param string The data to examime for "quoting style" |
||
| 331 | * @param string The type of quote character being used (single or double) |
||
| 332 | * @param string The character used as the column delimiter |
||
| 333 | * @param string The character used for newlines |
||
| 334 | * @return string One of four "QUOTING_" constants defined above--see this |
||
| 335 | * method's description for more info. |
||
| 336 | * @access protected |
||
| 337 | * @todo Refactor this method--It needs more thorough testing against a wider |
||
| 338 | * variety of CSV data to be sure it works reliably. And I'm sure there |
||
| 339 | * are many performance and logic improvements that could be made. This |
||
| 340 | * is essentially a first draft. |
||
| 341 | */ |
||
| 342 | 7 | protected function lickQuotingStyle($quote, $delim, $eol) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Remove quotes around a piece of text (if there are any) |
||
| 406 | * |
||
| 407 | * @param string The data to "unquote" |
||
| 408 | * @return string The data passed in, only with quotes stripped (off the edges) |
||
| 409 | * @access protected |
||
| 410 | */ |
||
| 411 | 12 | protected function unQuote($data) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Determine whether a particular string of data has quotes around it. |
||
| 418 | * |
||
| 419 | * @param string The data to check |
||
| 420 | * @return boolean Whether the data is quoted or not |
||
| 421 | * @access protected |
||
| 422 | */ |
||
| 423 | 7 | protected function isQuoted($data) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Determine what type of data is contained within a variable |
||
| 430 | * Possible types: |
||
| 431 | * - nonnumeric - only numbers |
||
| 432 | * - special - contains characters that could potentially need to be quoted (possible delimiter characters) |
||
| 433 | * - unknown - everything else |
||
| 434 | * This method is really only used within the "lickQuotingStyle" method to |
||
| 435 | * help determine whether a particular column has been quoted due to it being |
||
| 436 | * nonnumeric or because it has some special character in it such as a delimiter |
||
| 437 | * or newline or quote. |
||
| 438 | * |
||
| 439 | * @param string The data to determine the type of |
||
| 440 | * @return string The type of data (one of the "DATA_" constants above) |
||
| 441 | * @access protected |
||
| 442 | * @todo I could probably eliminate this method and use an anonymous function |
||
| 443 | * instead. It isn't used anywhere else and its name could be misleading. |
||
| 444 | * Especially since I also have a lickType method that is used within the |
||
| 445 | * lickHeader method. |
||
| 446 | */ |
||
| 447 | 7 | protected function lickDataType($data) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Replace all instances of newlines and whatever character you specify (as |
||
| 461 | * the delimiter) that are contained within quoted text. The replacements are |
||
| 462 | * simply a special placeholder string. This is done so that I can use the |
||
| 463 | * very unsmart "explode" function and not have to worry about it exploding |
||
| 464 | * on delimiters or newlines within quotes. Once I have exploded, I typically |
||
| 465 | * sub back in the real characters before doing anything else. Although |
||
| 466 | * currently there is no dedicated method for doing so I just use str_replace |
||
| 467 | * |
||
| 468 | * @param string The string to do the replacements on |
||
| 469 | * @param string The delimiter character to replace |
||
| 470 | * @return string The data with replacements performed |
||
| 471 | * @access protected |
||
| 472 | * @todo I could probably pass in (maybe optionally) the newline character I |
||
| 473 | * want to replace as well. I'll do that if I need to. |
||
| 474 | */ |
||
| 475 | protected function replaceQuotedSpecialChars($data, $delim) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Determine the "type" of a particular string of data. Used for the lickHeader |
||
| 486 | * method to assign a type to each column to try to determine whether the |
||
| 487 | * first for is different than a consistent column type. |
||
| 488 | * |
||
| 489 | * @todo As I'm writing this method I'm beginning ot realize how expensive |
||
| 490 | * the lickHeader method is going to end up being since it has to apply all |
||
| 491 | * these regexes (potentially) to every column. I may end up writing a much |
||
| 492 | * simpler type-checking method than this if it proves to be too expensive |
||
| 493 | * to be practical. |
||
| 494 | * |
||
| 495 | * @param string The string of data to check the type of |
||
| 496 | * @return string One of the TYPE_ string constants above |
||
| 497 | * @access protected |
||
| 498 | * @uses \Carbon\Carbon date/time ilbrary/class |
||
| 499 | */ |
||
| 500 | 12 | protected function lickType($data) |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Examines the contents of the CSV data to make a determination of whether |
||
| 542 | * or not it contains a header row. To make this determination, it creates |
||
| 543 | * an array of each column's (in each row)'s data type and length and then |
||
| 544 | * compares them. If all of the rows except the header look similar, it will |
||
| 545 | * return true. This is only a guess though. There is no programmatic way to |
||
| 546 | * determine 100% whether a CSV file has a header. The format does not |
||
| 547 | * provide metadata such as that. |
||
| 548 | * |
||
| 549 | * @param string The CSV data to examine (only 20 rows will be examined so ) |
||
| 550 | * there is no need to provide any more data than that) |
||
| 551 | * @param string The CSV data's quoting char (either double or single quote) |
||
| 552 | * @param string The CSV data's delimiting char (can be a variety of chars but) |
||
| 553 | * typically is either a comma or a tab, sometimes a pipe) |
||
| 554 | * @param string The CSV data's end-of-line char(s) (\n \r or \r\n) |
||
| 555 | * @return boolean True if the data (most likely) contains a header row |
||
| 556 | * @access public |
||
| 557 | * @todo This method needs a total refactor. It's not necessary to loop twice |
||
| 558 | * You could get away with one loop and that would allow for me to do |
||
| 559 | * something like only examining enough rows to get to a particular |
||
| 560 | * "hasHeader" score (+-100 for instance) & then just return true|false |
||
| 561 | * @todo Also, break out of the first loop after a certain (perhaps even a |
||
| 562 | * configurable) amount of lines (you only need to examine so much data ) |
||
| 563 | * to reliably make a determination and this is an expensive method) |
||
| 564 | * @todo Because the header isn't actually part of the "flavor", |
||
| 565 | * I could remove the need for quote, delim, and eol by "licking" the |
||
| 566 | * data sample provided in the first argument. Also, I could actually |
||
| 567 | * create a Reader object to read the data here. |
||
| 568 | */ |
||
| 569 | 13 | public function lickHeader($quote, $delim, $eol) |
|
| 607 | } |
||
| 608 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.