Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Hooks 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 Hooks, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Hooks |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Renders a page element. |
||
| 23 | * |
||
| 24 | * <pre> |
||
| 25 | * <p:pager |
||
| 26 | * count = int |
||
| 27 | * page = int |
||
| 28 | * limit = int |
||
| 29 | * with = string |
||
| 30 | * range = expression |
||
| 31 | * noarrows = boolean> |
||
| 32 | * <!-- Content: p:with-param*, template? --> |
||
| 33 | * </p:pager> |
||
| 34 | * </pre> |
||
| 35 | * |
||
| 36 | * @param array $args |
||
| 37 | * @param Engine $patron |
||
| 38 | * @param mixed $template |
||
| 39 | * |
||
| 40 | * @return string |
||
| 41 | */ |
||
| 42 | static public function markup_pager(array $args, Engine $patron, $template) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Adds a template. |
||
| 88 | * |
||
| 89 | * <pre> |
||
| 90 | * <p:template |
||
| 91 | * name = qname> |
||
| 92 | * <!-- Content: p:with-param*, template --> |
||
| 93 | * </p:template> |
||
| 94 | * </pre> |
||
| 95 | * |
||
| 96 | * The `name` attribute defines the name of the template. The content of the markup defines |
||
| 97 | * the template. |
||
| 98 | * |
||
| 99 | * @param array $args |
||
| 100 | * @param Engine $patron |
||
| 101 | * @param mixed $template |
||
| 102 | */ |
||
| 103 | static public function markup_template(array $args, Engine $patron, $template) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Calls a template. |
||
| 110 | * |
||
| 111 | * <pre> |
||
| 112 | * <p:call-template |
||
| 113 | * name = qname> |
||
| 114 | * <!-- Content: p:with-param* --> |
||
| 115 | * </p:call-template> |
||
| 116 | * </pre> |
||
| 117 | * |
||
| 118 | * @param array $args |
||
| 119 | * @param Engine $patron |
||
| 120 | * @param mixed $template |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | static public function markup_call_template(array $args, Engine $patron, $template) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Applies a template to each entries of the provided array. |
||
| 131 | * |
||
| 132 | * <pre> |
||
| 133 | * <p:foreach |
||
| 134 | * in = expression | this |
||
| 135 | * as = qname | this> |
||
| 136 | * <!-- Content: p:with-param*, template --> |
||
| 137 | * </p:foreach> |
||
| 138 | * </pre> |
||
| 139 | * |
||
| 140 | * At each turn the following variables are updated in `self`: |
||
| 141 | * |
||
| 142 | * - `count`: The number of entries. |
||
| 143 | * - `position`: The position of the current entry. |
||
| 144 | * - `left`: The number of entries left. |
||
| 145 | * - `even`: "even" if the position is even, an empty string otherwise. |
||
| 146 | * - `key`: The key of the entry. |
||
| 147 | * |
||
| 148 | * @param array $args |
||
| 149 | * @param Engine $patron |
||
| 150 | * @param mixed $template |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | static public function markup_foreach(array $args, Engine $patron, $template) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Provides a simple if-then conditionality. |
||
| 225 | * |
||
| 226 | * <pre> |
||
| 227 | * <p:if |
||
| 228 | * test = expression |
||
| 229 | * select = expression |
||
| 230 | * equals = value> |
||
| 231 | * <!-- Content: p:with-param*, template --> |
||
| 232 | * </p:if> |
||
| 233 | * </pre> |
||
| 234 | * |
||
| 235 | * Either `test` or `select` and an operator (e.g. `equals`) should be defined. |
||
| 236 | * |
||
| 237 | * Example: |
||
| 238 | * |
||
| 239 | * <pre> |
||
| 240 | * <p:if test="@has_title">This article has a title</p:if> |
||
| 241 | * <p:if test="@has_title.not()">This article has no title</p:if> |
||
| 242 | * <p:if select="@comments_count" equals="10">This article has 10 comments</p:if> |
||
| 243 | * </pre> |
||
| 244 | * |
||
| 245 | * @param array $args |
||
| 246 | * @param Engine $patron |
||
| 247 | * @param mixed $template |
||
| 248 | * |
||
| 249 | * @return string |
||
| 250 | * |
||
| 251 | * @throws \Exception when both `test` and `select` are defined. |
||
| 252 | */ |
||
| 253 | static public function markup_if(array $args, Engine $patron, $template) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Selects one among a number of possible alternatives. |
||
| 278 | * |
||
| 279 | * <pre> |
||
| 280 | * <!-- Category: instruction --> |
||
| 281 | * <p:choose> |
||
| 282 | * <!-- Content: (p:when+, p:otherwise?) --> |
||
| 283 | * </p:choose> |
||
| 284 | * |
||
| 285 | * <p:when |
||
| 286 | * test = boolean-expression> |
||
| 287 | * <!-- Content: template --> |
||
| 288 | * </p:when> |
||
| 289 | * |
||
| 290 | * <p:otherwise> |
||
| 291 | * <!-- Content: template --> |
||
| 292 | * </p:otherwise> |
||
| 293 | * </pre> |
||
| 294 | * |
||
| 295 | * It consists of a sequence of `p:when` elements followed by an optional `p:otherwise` |
||
| 296 | * element. Each `p:when` element has a single attribute, test, which specifies an expression. |
||
| 297 | * The content of the `p:when` and `p:otherwise` elements is a template. When an `p:choose` |
||
| 298 | * element is processed, each of the `p:when` elements is tested in turn, by evaluating the |
||
| 299 | * expression and converting the resulting object to a boolean as if by a call to the boolean |
||
| 300 | * function. The content of the first, and only the first, `p:when` element whose test is true |
||
| 301 | * is instantiated. If no `p:when` is true, the content of the `p:otherwise` element is |
||
| 302 | * instantiated. If no `p:when` element is true, and no `p:otherwise` element is present, |
||
| 303 | * nothing is created. |
||
| 304 | * |
||
| 305 | * @param array $args |
||
| 306 | * @param Engine $patron |
||
| 307 | * @param mixed $template |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | static public function markup_choose(array $args, Engine $patron, $template) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Binds a name to a value. |
||
| 360 | * |
||
| 361 | * <pre> |
||
| 362 | * <!-- Category: top-level-element --> |
||
| 363 | * <!-- Category: instruction --> |
||
| 364 | * <p:variable |
||
| 365 | * name = qname |
||
| 366 | * select = expression> |
||
| 367 | * <!-- Content: p:with-param*, template? --> |
||
| 368 | * </p:variable> |
||
| 369 | * |
||
| 370 | * <!-- Category: top-level-element --> |
||
| 371 | * <p:param |
||
| 372 | * name = qname |
||
| 373 | * select = expression> |
||
| 374 | * <!-- Content: template? --> |
||
| 375 | * </p:param> |
||
| 376 | * </pre> |
||
| 377 | * |
||
| 378 | * The value to which a variable is bound (the value of the variable) can be an object of any |
||
| 379 | * of the types that can be returned by expressions. There are two elements that can be used |
||
| 380 | * to bind variables: `p:variable` and `p:with-param`. The difference is that the value |
||
| 381 | * specified on the `p:with-param` variable is only a default value for the binding; when |
||
| 382 | * the template within which the `p:with-param` element occurs is invoked, parameters may |
||
| 383 | * be passed that are used in place of the default values. |
||
| 384 | * |
||
| 385 | * Both `p:variable` and `p:with-param` have a required name attribute, which specifies the |
||
| 386 | * name of the variable. The value of the name attribute is a qualified name. |
||
| 387 | * |
||
| 388 | * Example: |
||
| 389 | * |
||
| 390 | * <pre> |
||
| 391 | * <p:variable name="count" select="@comments_count" /> |
||
| 392 | * <p:variable name="count">There are #{@comments_count} comments</p:variable> |
||
| 393 | * </pre> |
||
| 394 | * |
||
| 395 | * @param array $args |
||
| 396 | * @param Engine $patron |
||
| 397 | * @param mixed $template |
||
| 398 | * |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | static public function markup_variable(array $args, Engine $patron, $template) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Parses a template with a bounded value. |
||
| 427 | * |
||
| 428 | * <pre> |
||
| 429 | * <p:with |
||
| 430 | * select = expression> |
||
| 431 | * <!-- Content: p:with-param*, template --> |
||
| 432 | * </p:with> |
||
| 433 | * </pre> |
||
| 434 | * |
||
| 435 | * Example: |
||
| 436 | * |
||
| 437 | * <pre> |
||
| 438 | * <p:with select="articles.first().comments.last()"> |
||
| 439 | * Last comment: <a href="#{@url}">#{@title}</a> |
||
| 440 | * </p:with> |
||
| 441 | * </pre> |
||
| 442 | * |
||
| 443 | * @param array $args |
||
| 444 | * @param Engine $patron |
||
| 445 | * @param mixed $template |
||
| 446 | * |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | static public function markup_with(array $args, Engine $patron, $template) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Translates and interpolates a string. |
||
| 465 | * |
||
| 466 | * <pre> |
||
| 467 | * <p:translate |
||
| 468 | * native = string> |
||
| 469 | * <!-- Content: p:with-param* --> |
||
| 470 | * </p:translate> |
||
| 471 | * </pre> |
||
| 472 | * |
||
| 473 | * The arguments for the interpolation are provided using the attributes of the markup, or the |
||
| 474 | * `with-param` construction. |
||
| 475 | * |
||
| 476 | * Example: |
||
| 477 | * |
||
| 478 | * <pre> |
||
| 479 | * <p:translate native="Posted on :date by !name"> |
||
| 480 | * <p:with-param name="date"><time datetime="#{@date}" pubdate="pubdate">#{@date.format_date()}</time></p:with-param> |
||
| 481 | * <p:with-param name="name" select="@user.name" /> |
||
| 482 | * </p:translate> |
||
| 483 | * </pre> |
||
| 484 | * |
||
| 485 | * @param array $args |
||
| 486 | * @param Engine $patron |
||
| 487 | * @param mixed $template |
||
| 488 | * |
||
| 489 | * @return string |
||
| 490 | */ |
||
| 491 | static public function markup_translate(array $args, Engine $patron, $template) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Decorates a content with a template. |
||
| 500 | * |
||
| 501 | * <pre> |
||
| 502 | * <p:decorate |
||
| 503 | * with = string> |
||
| 504 | * <!-- Content: p:with-param*, template --> |
||
| 505 | * </p:decorate> |
||
| 506 | * </pre> |
||
| 507 | * |
||
| 508 | * The content of the markup is rendered to create the component to decorate, it is then passed |
||
| 509 | * to the decorating template as the `component` variable. |
||
| 510 | * |
||
| 511 | * The name of the decorating template is specified with the `with` attribute, and is |
||
| 512 | * interpolated e.g. if "page" is specified the template name "@page" is used; if "admin/page" |
||
| 513 | * is specified the template name "admin/@page" is used. |
||
| 514 | * |
||
| 515 | * The parameters specified using `with-param` are all turned into variables. |
||
| 516 | * |
||
| 517 | * Example: |
||
| 518 | * |
||
| 519 | * <pre> |
||
| 520 | * <p:decorate with="page"> |
||
| 521 | * <p:page:content id="body" /> |
||
| 522 | * </p:decorate> |
||
| 523 | * </pre> |
||
| 524 | * |
||
| 525 | * @param array $args |
||
| 526 | * @param Engine $engine |
||
| 527 | * @param mixed $template |
||
| 528 | * |
||
| 529 | * @return string |
||
| 530 | */ |
||
| 531 | static public function markup_decorate(array $args, Engine $engine, $template) |
||
| 548 | |||
| 549 | /* |
||
| 550 | * Brickrouge |
||
| 551 | */ |
||
| 552 | |||
| 553 | /** |
||
| 554 | * CSS assets can be collected and rendered into `LINK` elements with the `p:document:css` |
||
| 555 | * element. The `href` attribute is used to add an asset to the collection. The `weight` |
||
| 556 | * attribute specifies the weight of that asset. If the `weight` attribute is not specified, |
||
| 557 | * the weight of the asset is defaulted to 100. If the `href` attribute is not specified, |
||
| 558 | * the assets are rendered. If a template is specified the collection is passed as `this`, |
||
| 559 | * otherwise the collection is rendered into an HTML string of `LINK` elements. |
||
| 560 | * |
||
| 561 | * <pre> |
||
| 562 | * <p:document:css |
||
| 563 | * href = string |
||
| 564 | * weight = int> |
||
| 565 | * <!-- Content: p:with-params, template? --> |
||
| 566 | * </p:document:css> |
||
| 567 | * </pre> |
||
| 568 | * |
||
| 569 | * Example: |
||
| 570 | * |
||
| 571 | * <pre> |
||
| 572 | * <p:document:css href="/public/page.css" /> |
||
| 573 | * <p:document:css href="/public/reset.css" weight="-100" /> |
||
| 574 | * <p:document:css /> |
||
| 575 | * </pre> |
||
| 576 | * |
||
| 577 | * will produce: |
||
| 578 | * |
||
| 579 | * <pre> |
||
| 580 | * <link href="/public/reset.css" type="text/css" rel="stylesheet" /> |
||
| 581 | * <link href="/public/page.css" type="text/css" rel="stylesheet" /> |
||
| 582 | * </pre> |
||
| 583 | * |
||
| 584 | * @param array $args |
||
| 585 | * @param Engine $engine |
||
| 586 | * @param mixed $template |
||
| 587 | * |
||
| 588 | * @return void|string |
||
| 589 | */ |
||
| 590 | View Code Duplication | static public function markup_document_css(array $args, Engine $engine, $template) |
|
| 603 | |||
| 604 | /** |
||
| 605 | * JavaScript assets can be collected and rendered into `SCRIPT` elements with the `p:document:js` |
||
| 606 | * element. The `href` attribute is used to add an asset to the collection. The `weight` |
||
| 607 | * attribute specifies the weight of that asset. If the `weight` attribute is not specified, |
||
| 608 | * the weight of the asset is defaulted to 100. If the `href` attribute is not specified, |
||
| 609 | * the assets are rendered. If a template is specified the collection is passed as `this`, |
||
| 610 | * otherwise the collection is rendered into an HTML string of `SCRIPT` elements. |
||
| 611 | * |
||
| 612 | * <pre> |
||
| 613 | * <p:document:js |
||
| 614 | * href = string |
||
| 615 | * weight = int> |
||
| 616 | * <!-- Content: p:with-params, template? --> |
||
| 617 | * </p:document:js> |
||
| 618 | * </pre> |
||
| 619 | * |
||
| 620 | * Example: |
||
| 621 | * |
||
| 622 | * <pre> |
||
| 623 | * <p:document:js href="/public/page.js" /> |
||
| 624 | * <p:document:js href="/public/reset.js" weight="-100" /> |
||
| 625 | * <p:document:js /> |
||
| 626 | * </pre> |
||
| 627 | * |
||
| 628 | * will produce: |
||
| 629 | * |
||
| 630 | * <pre> |
||
| 631 | * <script src="/public/reset.css" type="text/javascript"></script> |
||
| 632 | * <script src="/public/page.css" type="text/javascript"></script> |
||
| 633 | * </pre> |
||
| 634 | * |
||
| 635 | * @param array $args |
||
| 636 | * @param Engine $engine |
||
| 637 | * @param mixed $template |
||
| 638 | * |
||
| 639 | * @return void|string |
||
| 640 | */ |
||
| 641 | View Code Duplication | static public function markup_document_js(array $args, Engine $engine, $template) |
|
| 654 | |||
| 655 | /* |
||
| 656 | * ICanBoogie |
||
| 657 | */ |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Synthesizes the "patron.markups" config. |
||
| 661 | * |
||
| 662 | * @param array $fragments |
||
| 663 | * |
||
| 664 | * @return array |
||
| 665 | */ |
||
| 666 | View Code Duplication | static public function synthesize_markups_config(array $fragments) |
|
| 682 | |||
| 683 | /** |
||
| 684 | * Synthesizes the "patron.functions" config. |
||
| 685 | * |
||
| 686 | * @param array $fragments |
||
| 687 | * |
||
| 688 | * @return array |
||
| 689 | */ |
||
| 690 | View Code Duplication | static public function synthesize_functions_config(array $fragments) |
|
| 706 | |||
| 707 | /** |
||
| 708 | * Attaches event hooks to `MarkupCollection::alter` and `FunctionCollection::alter` in order |
||
| 709 | * to add the markups and functions defined in the `patron.markups` and `patron.function` |
||
| 710 | * configs. |
||
| 711 | * |
||
| 712 | * @param Core\BootEvent $event |
||
| 713 | * @param Core $app |
||
| 714 | */ |
||
| 715 | static public function on_core_boot(Core\BootEvent $event, Core $app) |
||
| 735 | } |
||
| 736 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.