/build/ext/phing/classes/phing/types/selectors/MajoritySelector.php

http://github.com/alexgorbatchev/SyntaxHighlighter · PHP · 92 lines · 37 code · 11 blank · 44 comment · 7 complexity · 81983fa11e306264a8d81d7311fafa80 MD5 · raw file

  1. <?php
  2. /*
  3. * $Id: MajoritySelector.php 123 2006-09-14 20:19:08Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. /**
  22. * This selector is here just to shake up your thinking a bit. Don't get
  23. * too caught up in boolean, there are other ways you can evaluate a
  24. * collection of selectors. This one takes a vote of the selectors it
  25. * contains, and majority wins. You could also have an "all-but-one"
  26. * selector, a "weighted-average" selector, and so on. These are left
  27. * as exercises for the reader (as are the usecases where this would
  28. * be necessary).
  29. *
  30. * @author Hans Lellelid <hans@xmpl.org> (Phing)
  31. * @author Bruce Atherton <bruce@callenish.com> (Ant)
  32. * @package phing.types.selectors
  33. */
  34. class MajoritySelector extends BaseSelectorContainer {
  35. private $allowtie = true;
  36. public function toString() {
  37. $buf = "";
  38. if ($this->hasSelectors()) {
  39. $buf .= "{majorityselect: ";
  40. $buf .= parent::toString();
  41. $buf .= "}";
  42. }
  43. return $buf;
  44. }
  45. public function setAllowtie($tiebreaker) {
  46. $this->allowtie = $tiebreaker;
  47. }
  48. /**
  49. * Returns true (the file is selected) if most of the other selectors
  50. * agree. In case of a tie, go by the allowtie setting. That defaults
  51. * to true, meaning in case of a tie, the file is selected.
  52. *
  53. * @param basedir the base directory the scan is being done from
  54. * @param filename is the name of the file to check
  55. * @param file is a PhingFile object for the filename that the selector
  56. * can use
  57. * @return whether the file should be selected or not
  58. */
  59. public function isSelected(PhingFile $basedir, $filename, PhingFile $file) {
  60. $this->validate();
  61. $yesvotes = 0;
  62. $novotes = 0;
  63. $selectors = $this->selectorElements();
  64. for($i=0,$size=count($selectors); $i < $size; $i++) {
  65. $result = $selectors[$i]->isSelected($basedir,$filename,$file);
  66. if ($result) {
  67. $yesvotes = $yesvotes + 1;
  68. } else {
  69. $novotes = $novotes + 1;
  70. }
  71. }
  72. if ($yesvotes > $novotes) {
  73. return true;
  74. }
  75. else if ($novotes > $yesvotes) {
  76. return false;
  77. }
  78. // At this point, we know we have a tie.
  79. return $this->allowtie;
  80. }
  81. }