Class: PHP::Function

Inherits:
PHP::Expression show all
Defined in:
lib/php/syntax/function.rb

Overview

See Also:

Instance Attribute Summary

Instance Method Summary

Methods inherited from PHP::Node

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

Constructor Details

- (Function) initialize(name = nil, parameters = [], *body, &block)

A new instance of Function

Parameters:

  • (Symbol, #to_s) name (defaults to: nil)
  • (Array<Symbol>) parameters (defaults to: [])
  • (Array<Block>) body


9
10
11
12
13
14
15
16
17
18
# File 'lib/php/syntax/function.rb', line 9

def initialize(name = nil, parameters = [], *body, &block)
  @name       = Identifier.new(name).to_sym rescue nil
  @parameters = parameters

  if block_given?
    # TODO
  else
    @children = body.compact.map { |exp| Block.for(exp) }
  end
end

Instance Attribute Details

- (Symbol) name

Returns:

  • (Symbol)


22
23
24
# File 'lib/php/syntax/function.rb', line 22

def name
  @name
end

Instance Method Details

- (Boolean) anonymous? Also known as: unnamed?

Returns true if this is an anonymous function.

Returns:

  • (Boolean)


36
37
38
# File 'lib/php/syntax/function.rb', line 36

def anonymous?
  name.nil?
end

- (Integer) arity

Returns the arity of this function.

Returns:

  • (Integer)


46
47
48
# File 'lib/php/syntax/function.rb', line 46

def arity
  parameters.size
end

- (Boolean) named?

Returns true if this is a named function.

Returns:

  • (Boolean)


28
29
30
# File 'lib/php/syntax/function.rb', line 28

def named?
  !anonymous?
end

- (Array<Symbol>) parameters

Returns the parameters, if any, of this function.

Returns:

  • (Array<Symbol>)


54
55
56
# File 'lib/php/syntax/function.rb', line 54

def parameters
  @parameters || []
end

- (String) to_php

Returns the PHP representation of this function.

Returns:

  • (String)


62
63
64
65
66
67
68
69
# File 'lib/php/syntax/function.rb', line 62

def to_php
  body = children? ? ('{ ' << children.map(&:to_php).join('; ') << ' }') : '{}'
  if anonymous?
    "function(#{parameters.join(', ')}) " << body # FIXME
  else
    "function #{name}(#{parameters.join(', ')}) " << body # FIXME
  end
end