Class: PHP::Statement::If

Inherits:
PHP::Statement show all
Defined in:
lib/php/syntax/statement.rb

Instance Attribute Summary

Instance Method Summary

Methods inherited from PHP::Node

#<<, #<=>, #children?, #each, #indent, #indent=, #to_s

Constructor Details

- (If) initialize(condition, then_branch, else_branch = nil)

A new instance of If

Parameters:



56
57
58
59
60
# File 'lib/php/syntax/statement.rb', line 56

def initialize(condition, then_branch, else_branch = nil)
  @condition   = condition
  @then_branch = Block.for(then_branch)
  @else_branch = Block.for(else_branch)
end

Instance Attribute Details

- (Object) condition

Returns the value of attribute condition



48
49
50
# File 'lib/php/syntax/statement.rb', line 48

def condition
  @condition
end

- (Object) else_branch

Returns the value of attribute else_branch



50
51
52
# File 'lib/php/syntax/statement.rb', line 50

def else_branch
  @else_branch
end

- (Object) then_branch

Returns the value of attribute then_branch



49
50
51
# File 'lib/php/syntax/statement.rb', line 49

def then_branch
  @then_branch
end

Instance Method Details

- (Boolean) else_branch?

Returns true if this statement has an else branch.

Returns:

  • (Boolean)


74
75
76
# File 'lib/php/syntax/statement.rb', line 74

def else_branch?
  !@else_branch.nil?
end

- (Boolean) then_branch?

Returns true if this statement has a then branch.

Returns:

  • (Boolean)


66
67
68
# File 'lib/php/syntax/statement.rb', line 66

def then_branch?
  !@then_branch.nil?
end

- (String) to_php

Returns the PHP representation of this if/then/else statement.

Returns:

  • (String)


82
83
84
85
86
87
88
89
90
91
# File 'lib/php/syntax/statement.rb', line 82

def to_php
  case
    when else_branch?
      "if (#{condition}) { #{then_branch} } else { #{else_branch} }"
    when then_branch?
      "if (#{condition}) { #{then_branch} }"
    else
      "if (#{condition}) {}"
  end
end