Last active
August 29, 2015 14:20
-
-
Save rheinardkorf/e4f9a67daff16f8fcd38 to your computer and use it in GitHub Desktop.
Searches a PHP Class for declared properties, uses and implicit properties. Not overly elegant, but scratched an annoying itch!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
declared_properties = [] | |
properties = [] | |
implicit_properties = [] | |
if ARGV[0].to_s.strip.length == 0 | |
puts 'No filename specified.' | |
exit | |
end | |
if ! File.exists? ARGV[0] | |
puts 'File does not exist.' | |
exit | |
end | |
begin | |
file = File.new( ARGV[0] , "r") | |
while (line = file.gets) | |
# Declared properties | |
matches = line .match /((public\s*|private\s*|protected\s*|var\s*)\s*|static\s*)(\$[a-zA-Z0-9\_]*)/ | |
if matches | |
if( ! declared_properties.include? matches[3] ) | |
declared_properties << matches[3] | |
end | |
end | |
# Match uses of properties | |
matches = line.match /(\$this-\>|self::)([a-zA-Z0-9_\-]*)[\s,+-=*\/{]/ | |
if matches | |
if( ! properties.include? "$#{matches[2]}" ) | |
properties << "$#{matches[2]}" | |
end | |
end | |
end | |
file.close | |
properties.each do |property| | |
if( ! declared_properties.include? property ) | |
implicit_properties << property | |
end | |
end | |
puts "== Declared Properties ==" | |
puts declared_properties | |
puts "\n== Discovered Properties ==" | |
puts properties | |
puts "\n== Implicit Properties ==" | |
puts implicit_properties | |
rescue => err | |
puts "Exception: #{err}" | |
err | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment